1

When using the following schema

{
    "$schema": "http://json-schema.org/schema#",
    "definitions":{
        "entity": {
            "type": "object",
            "properties": {
                "parent": {"type": ["null","string"]},
                "exclude": {"type": "boolean"},
                "count": {"type": ["null","integer"]},
                "EntityType": {"type": "string"},
                "Children": {
                    "type": "array",
                    "items": {"$ref":"#/definitions/entity"}
                }
            }
        }
    },
    "required": ["parent","EntityType","count"]
}

On this provided body of JSON

{
    "parent": "null",
    "EntityType": "test",
    "count": "null",
    "Children": [
        {
            "EntityType": "test",
            "count": 3
        },
        {
            "EntityType": "test"
        }
    ],
    "Extra": "somevalue"
}

It should be returning that I have provided an invalid Json object, however it does not seem to be doing so.

That said, if I were to have the root node not succeed (by removing one of the required fields) the validation works and says that I haven't provided a required field. Is there a reason that I am not able to validate the json recursively?

gregsdennis
  • 7,218
  • 3
  • 38
  • 71
Ryan Lynar
  • 13
  • 2

1 Answers1

2

It looks like you want parent, EntityType, and count to be required properties of the entity definition. However they're only required at the root, not the entity level. I would suggest that you move the required keyword into the entity definition, then reference the definition as part of an allOf to ensure the root is compliant.

{
    "$schema": "http://json-schema.org/schema#",
    "definitions":{
        "entity": {
            "type": "object",
            "properties": {
                "parent": {"type": ["null","string"]},
                "exclude": {"type": "boolean"},
                "count": {"type": ["null","integer"]},
                "EntityType": {"type": "string"},
                "Children": {
                    "type": "array",
                    "items": {"$ref":"#/definitions/entity"}
                }
            },
            "required": ["parent","EntityType","count"]
        }
    },
    "allOf": [{"$ref": "#/definitions/entity"}]
}
gregsdennis
  • 7,218
  • 3
  • 38
  • 71