I have an unordered array of JSON items. According to the specification https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03#section-5.5 the json schema below will only validate if the objects in the array appear IN THAT ORDER. I don't want to specify an order, just validate the objects within the array, regardless of order or number of objects. From the spec I can't seem to understand how this is done.
"transactions" : { "type" : "array", "items" : [ { "type" : "object", "properties" : { "type" : { "type" : "string", "enum" : ["BUILD", "REASSIGN"] } } }, { "type" : "object", "properties" : { "type" : { "type" : "string", "enum" : ["BREAK"] } } } ] }
Arrays are used for ordered elements. In JSON, each element in an array may be of a different type.
A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.
JSON Schema ExampleYou will use this to give a title to your schema. A little description of the schema. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. Defines various keys and their value types, minimum and maximum values to be used in JSON file.
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.
I asked this same question on the JSON schema google group, and it was answered quickly. User fge asked that I post his response here:
Hello,
The current specification is draft v4, not draft v3. More specifically, the validation specification is here:
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00
The web site is not up to date, I don't know why... I'll submit a pull request.
With draft v4 you can use this:
{ "type": "array", "items": { "oneOf": [ {"first": [ "schema", "here" ] }, {"other": [ "schema": "here" ] } ] } }
For instance, this is a schema for an array where items can be either strings or integers (it can be written in a more simple way though):
{ "type": "array", "items": { "oneOf": [ {"type": "string"}, {"type": "integer"} ] } }
This is the correct answer. My corrected schema now includes:
"transactions" : { "type" : "array", "items" : { "oneOf" : [ { "type" : "object", "properties" : { "type" : { "type" : "string", "enum" : ["BUILD", "REASSIGN"] } } }, { "type" : "object", "properties" : { "type" : { "type" : "string", "enum" : ["BREAK"] } } } ] } }
I've been looking into this for quite a while too. But haven't been able to find a working solution. It works fine if you have only one schema eg.
"transactions" : { "type" : "array", "items" : { "type" : "object", "properties" : { "type" : { "type" : "string", "enum" : ["BREAK"] }, } }
Then you just skip the array brackets, and use an object. However if you want to do what you are doing, there seems to be no solid answer. This is the only thing that I've found so far: http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html
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