I am trying to validate an object that can have arbitrary keys whose values are either an object that looks like:
{ "href": "some string" }
OR an array containing object's matching the above.
Here is what I currently have and DOES NOT WORK:
{
"$schema": "http://json-schema.org/schema#",
"id": "https://turnstyle.io/schema/links.json",
"type": "object",
"additionalProperties": {
"oneOf": [
{
"type": "object",
"required": "href"
},
{
"type": "array",
"items": {
"type": "object",
"required": "href"
}
}
]
}
}
Passing example:
{
"customer": { "href": "/customer/1" },
"products": [
{ "href": "/product/1" },
{ "href": "/product/2" }
]
}
Failing example:
{
"customer": { "wrongKey": "/customer/1" },
"products": "aString"
}
Is it possible, and if so what is the proper syntax?
My assumption is that this will not work because the passing schema(s) in oneOf|anyOf|allOf
of additionalProperties
must apply to ALL keys falling under additionalProperties
.
By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .
The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.
allOf – the value validates against all the subschemas. anyOf – the value validates against any of the subschemas. oneOf – the value validates against exactly one of the subschemas.
Difference Between anyOf and oneOfoneOf matches exactly one subschema, and anyOf can match one or more subschemas.
"required" should be an array of the properties which are mandatory in v4.
Or "required": true (or false) as part of the property in v3.
Try this:
{
"$schema": "http://json-schema.org/schema#",
"id": "https://turnstyle.io/schema/links.json",
"type": "object",
"additionalProperties": {
"oneOf": [
{
"type": "object",
"properties": {
"href": {"type": "string"}
},
"required": ["href"]
},
{
"type": "array",
"items": {
"type": "object",
"properties": {
"href": {"type": "string"}
},
"required": ["href"]
}
}
]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With