Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FeatureRequest] Support plymorphism with descriptor as well, not just by tag #73

Closed
nicloay opened this issue Dec 12, 2023 · 1 comment

Comments

@nicloay
Copy link

nicloay commented Dec 12, 2023

I have many configuration files, and then I generate Json chemas for them, it's more convenient when you have autocompletion in the editor, and it also allow to validate incoming files using packages like JsonSchema (paid) or NJsonSchema (free)

At the moment before deserialization, I replace descriptor like this content.Replace("- type: !", "- !");

Here is a small JSON schema from my project, it uses the constant field to describe different types
{
  "definitions": {
    "ModApplyDamage": {
      "type": "object",
      "properties": {
        "value": {
          "type": "integer"
        },
        "type": {
          "const": "!damage"
        }
      },
      "required": [
        "value",
        "type"
      ]
    },
    "ModCriticalHit": {
      "type": "object",
      "properties": {
        "chance": {
          "type": "number"
        },
        "mult": {
          "type": "number"
        },
        "type": {
          "const": "!crit"
        }
      },
      "required": [
        "chance",
        "mult",
        "type"
      ]
    },
    "ModMaxDistance": {
      "type": "object",
      "properties": {
        "value": {
          "type": "number"
        },
        "type": {
          "const": "!dist"
        }
      },
      "required": [
        "value",
        "type"
      ]
    },
    "ModSpeed": {
      "type": "object",
      "properties": {
        "value": {
          "type": "number"
        },
        "type": {
          "const": "!speed"
        }
      },
      "required": [
        "value",
        "type"
      ]
    },
    "ProjectileConfig": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "prefabId": {
          "type": "string",
          "enum": [
            "bolt",
            "roundHead"
          ]
        },
        "modifiers": {
          "type": "array",
          "items": {
            "oneOf": [
              {
                "$ref": "#/definitions/ModApplyDamage"
              },
              {
                "$ref": "#/definitions/ModSpeed"
              },
              {
                "$ref": "#/definitions/ModCriticalHit"
              },
              {
                "$ref": "#/definitions/ModMaxDistance"
              }
            ]
          }
        }
      },
      "required": [
        "prefabId",
        "modifiers"
      ]
    }
  },
  "type": "object",
  "properties": {
    "id": {
      "type": [
        "string",
        "null"
      ]
    },
    "icon": {
      "type": "string",
      "enum": [
        "none",
        "renegade",
        "cyclone",
        "cascade",
        "firestone",
        "bullsEye"
      ]
    },
    "prefabId": {
      "type": "string",
      "enum": [
        "none",
        "doubleGun"
      ]
    },
    "chargeTime": {
      "type": "integer"
    },
    "coolDownTime": {
      "type": "integer"
    },
    "projectileCount": {
      "type": "integer"
    },
    "projectile": {
      "$ref": "#/definitions/ProjectileConfig"
    },
    "radarMaxRadius": {
      "type": "number"
    }
  },
  "required": [
    "id",
    "icon",
    "prefabId",
    "chargeTime",
    "coolDownTime",
    "projectileCount",
    "projectile",
    "radarMaxRadius"
  ]
}
and this is my yaml file
id: renegadeMK2
icon: renegade
prefabId: doubleGun
chargeTime: 3
coolDownTime: 2
projectileCount: 1
radarMaxRadius: 25
projectile:
  prefabId: bolt
  modifiers:
    - type: !dist
      value: 12
    - type: !crit
      chance: 0 
      mult: 0
    - type: !speed
      value: 0
    - type: !damage
      value: 33
@hadashiA
Copy link
Owner

hadashiA commented May 2, 2024

In short, is it correct that you want to define the schema definition separately from the data, e.g. in a JSON Schema?

If so, there are currently no plans to support this in VYaml itself.

VYaml is a library that provides serialisation to C#, in which case it would be good to be able to use C# type definitions as schemas.

Also, I personally think that Json Schema itself is prehistoric. It is a schema definition using only what is accepted as a web standard. Alternatives to json schema include Google's cue and Apple's pkl.

@hadashiA hadashiA closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants