This is because you have (accidentally) used "tuple typing". This is enabled when the value of "items" is an array, and it matches schemas to specific positions in the array.
If you change "items" (in your schema) to be simply a schema (not an array of schemas), then it will validate all items the same way.
This is because you have (accidentally) used "tuple typing". This is enabled when the value of "items" is an array, and it matches schemas to specific positions in the array.
If you change "items" (in your schema) to be simply a schema (not an array of schemas), then it will validate all items the same way.
Kudos to @cloudfeet 's answer, I was struggling with this issue until I saw his answer. To be more clear, the [] around items should be removed.
{
"title":"myCollection",
"properties":{
"things":{
"type":"array",
"items":**[**{
"title":"thingObj",
"type":"object",
.
.
.
"required":["value"]
}**]**,
"additionalProperties":false
}]
}
}
}
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
You can achieve this using the anyOf keyword and definitions/$ref to avoid duplication.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"assetMetadata": {
"anyOf": [
{ "$ref": "#/definitions/assetMetaData" },
{
"type": "array",
"description": "...",
"items": { "$ref": "#/definitions/assetMetaData" }
}
]
}
},
"definitions": {
"assetMetadata": {
"type": "object",
"additionalProperties": false,
"properties": { ... }
}
}
}
The accepted answer was not working for me in the JSON schema validator.
The arrays were not being accepted.
I made some tweaks and changes to make it work, here is an example schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"anyOf": [
{
"$ref": "#/definitions/commentObject"
},
{
"type": "array",
"description": "Array of Comment objects",
"items": {
"$ref": "#/definitions/commentObject"
}
}
],
"definitions": {
"commentObject": {
"properties": {
"number": {
"type": "integer",
"minLength": 0,
"maxLength": 256
},
"comment": {
"type": "string",
"minLength": 0,
"maxLength": 256
}
},
"required": [
"number",
"comment"
],
"type": "object"
}
}
}
Object used to test the validation:
{
"number": 47,
"comment": "This is a comment",
}
Arrays of objects used to test the validation:
[
{
"number": 47,
"comment": "This is a comment"
},
{
"number": 11,
"comment": "This is other comment"
}
]
JSON Schema Validator for object
JSON Schema Validator for array (of the same objects)