0
votes

I am using this json schema validator: https://www.jsonschemavalidator.net/ to validate some json. It suprised me that it validates the schema even when a property is missing in the json.

Schema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
      "test": {
        "type": "array"
      }
    }
}

Should validate

{
    "test": []
}

Should not validate (but validates!)

{}

Why is this considered valid and how can I validate the json so that the property test must be part of the json?

1
You have to set "additionalProperties" to false - Pedro
Thanks @Pedro, seems my question was not clear enough. Your comment helped me find the solution of using required. - span

1 Answers

2
votes

There is a required attribute that can be done. It seems the validation uses required: false for all properties by default.

This validates and forces the property to be present:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
      "test": {
        "type": "array"
      }
    },
    "required": ["test"]
}