JsonSchema validation passes, even if the element is repeating itself. The schema tries to prevent the same using "additionalProperties" as false, and "maxProperties" as 1.
I have the following json file with one of the field repeating.
{
"stats": "56989",
"stats": "56989"
}
I tried validating this against the below schema:-
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net#",
"type": "object",
"additionalProperties": false,
"maxProperties": 1,
"properties": {
"stats": {
"id": "http://jsonschema.net/stats#",
"type": "string",
"maxLength": 5,
"minLength": 5,
"additionalProperties": false,
"maxProperties": 1
}
},
"required": [
"stats"
]
}
The json validates successfully against the schema. Though I am giving maxProperties=1, the validation passes.
Please let me know how to check repeating elements in Json Schema.
The problem is that your test JSON is invalid. Repeated properties in an object is undefined in JSON. The second occurrence of
stats
would either be ignored or it would override the first occurrence. But, it in end, there would only be one occurrence ofstats
. When your test data is read into memory, it looks like{ "stats": "56989" }
and therefore validates successfully.In order to test
additionalProperties
and/ormaxProperties
, your test data needs to include unique keys. For example{ "stats", "56989", "foo": "bar" }
would be an appropriate test case.