1

I want to define a JsonSchema with a customProperty in it, this property follows some rules, so in order to validate those, I need to define a JsonSchema that will validate it.

So far I've managed to describe it properly, but it only works for the first level of attributes, and I want it to be recursive...

From my comprehension it should work, I probably made a mistake I'm unable to see and at this point I don't know if it's a bug, impossible, or stupid...

I believe redefining every type should be a possibility , but obviously I'd rather not.

Here is an example of the Json I want to validate

{
  "title": "TheObject",
  "type": "object",
  "properties": {
    "aString": {
      "type": "string",
      "myCustomProperty": {}
    },
    "anObjet": {
      "type": "object",
      "myCustomProperty": {},
      "properties": {
        "anotherObject": {
          "type": "object",
          "myCustomProperty": {}, //if this line is removed it still validates which I don't want
          "properties": {}
        }
      }
    }
  }
}

and here is the JsonSchema I've done so far:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "title": {"type": "string"},
    "type": {"type": "string","enum": ["object"]},
    "properties": {
      "type": "object",
      "patternProperties": {
        ".*": {
          "$ref": "#/definitions/Field"
        }
      }
    }
  },
  "definitions": {
    "Field": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "myCustomProperty": {
          "$ref": "#/definitions/myCustomProperty"
        },
        "patternProperties": {
          "^(?!myCustomProperty).*": {
            "$ref": "#/definitions/Field"
          }
        }
      },
      "required": [
        "type",
        "myCustomProperty"
      ]
    },
    "myCustomProperty": {
        //Some rules
    }
  }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

I found a solution, I wasn't far from what I wanted after all.

In my definition of "Field", I am describing an object that defines an object, and I was missing the "properties" field. In which I had to put my recursive reference.

The right jsonSchema is the following :

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "title": {
      "type": "string"
    },
    "type": {
      "type": "string",
      "enum": [
        "object"
      ]
    },
    "properties": {
      "type": "object",
      "patternProperties": {
        ".*": {
          "$ref": "#/definitions/Field"
        }
      }
    }
  },
  "definitions": {
    "Field": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "myCustomProperty": {
          "$ref": "#/definitions/myCustomProperty"
        },
        "properties": {            <====================  here
          "type": "object",
          "patternProperties": {
            ".*": {
              "$ref": "#/definitions/Field"
            }
          }
        }
      },
      "required": [
        "type",
        "myCustomProperty"
      ]
    },
    "myCustomProperty": {/*rules*/}
  }
}

So far it works as expected, but if someone has a more elegant suggestion please share!