Search code examples
jsonyamlwildcardjsonschemakey-value

How to define path with wildcard properties/keys in JSON schema


I have YAML files that look like this...

material: iron_leggings
displayName: "Plated Leggings"
rare:
  effects:
    entry0:
      slot: legs
      name: life_steal

And I want to validate (using a JSON schema) that the slot property contains legs if the materials property contains the word leggings.

Problem: The rare key can change name [common, uncommon, rare] and the entry0 property can also take any name. I want a schema that can path into: [common, uncommon, rare] -> effects -> (wildcard) -> slot

This is part of the schema I have so far, but I don't want to copy it for all combinations of possible keys:

      "if": { "properties": { "material": { "pattern": "(leggings)" } } },
      "then": {
        "properties": {
          "rare": {
            "properties": {
              "effects": {
                "properties": {
                  "entry0": { "properties": { "slot": { "pattern": "legs" } } } } } } } } } }

Is this possible?


Solution

  • Use additionalProperties in place of properties with a name.

    {
      "if": {
        "properties": {
          "material": {
            "pattern": "(leggings)"
          }
        }
      },
      "then": {
        "properties": {
          "rare": {
            "properties": {
              "effects": {
                "additionalProperties": {       // <- this
                  "properties": {
                    "slot": {
                      "pattern": "legs"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }