Reputation: 811
How do I validate JSON, with jsonschema, that within an array of objects, a specific key in each object must be unique? For example, validating the uniqueness of each Name k-v pair should fail:
"test_array": [
{
"Name": "name1",
"Description": "unique_desc_1"
},
{
"Name": "name1",
"Description": "unique_desc_2"
}
]
Using uniqueItems on test_array won't work because of the unique Description keys.
Upvotes: 6
Views: 3226
Reputation: 811
I found the alternative method of using a schema that allows arbitrary properties. The only caveat is that JSON allows duplicate object keys, but duplicates will override their previous instances. The array of objects with the key "Name" can be converted to an object with arbitrary properties:
For example, the following JSON:
"test_object": {
"name1": {
"Desc": "Description 1"
},
"name2": {
"Desc": "Description 2"
}
}
would have the following schema:
{
"type": "object",
"properties": {
"test_object": {
"type": "object",
"patternProperties": {
"^.*$": {
"type": "object",
"properties": {
"Desc": {"type" : "string"}
},
"required": ["Desc"]
}
},
"minProperties": 1,
"additionalProperties": false
}
},
"required": ["test_object"]
}
Upvotes: 2