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.

Answer from cloudfeet on Stack Overflow
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
Like with items, if you set ... in the array. ... Here, all the values are evaluated. The schema passes validation. ... But here, the schema fails validation because "unevaluatedItems": false specifies that no extra values should exist. ... Note that items doesn't "see inside" any instances of allOf, anyOf, or oneOf ...
🌐
Ajv
ajv.js.org › json-schema.html
JSON Schema | Ajv JSON schema validator
The data is valid if it is valid according to one or more JSON Schemas in this array. Validators only need to validate data against schemas in order until the first schema matches (or until all schemas have been tried). For this reason validating against this keyword is faster than against "oneOf" keyword in most cases. Example · schema: { type: "number", anyOf: [{maximum: 3}, {type: "integer"}] } valid: 1.5, 2, 2.5, 3, 4, 5 ·
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › anyof
anyOf (2020-12)
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "anyOf": [ { "required": [ "foo" ] }, { "required": [ "bar" ] } ] }
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › combining
JSON Schema - Boolean JSON Schema combination
anyOf: (OR) Must be valid against any of the subschemas · oneOf: (XOR) Must be valid against exactly one of the subschemas · not: (NOT) Must not be valid against the given schema · All of these keywords must be set to an array, where each item is a schema.
🌐
Cswr
cswr.github.io › JsonSchema › spec › multiple_types
Multiple Types - JSON Schema
{ "anyOf": [ { "type": "integer", "minimum": 2 }, { "type": "string", "minLength": 4 } ] } In general, when multiple (or no) types are defined in the schema, the rule for restrictions is as follows: each keyword restriction only applies when validating documents that are of the type that is compatible with the keyword. As another example, the following schema specifies JSON documents that are either arrays, strings, integers, numbers, boolean or nulls, and also objects that have the property "I_apply_only_to_objects": { "required": ["I_apply_only_to_objects"] } This is again equivalent to ·
🌐
Riverbed
support.riverbed.com › apis › steelscript › reschema › jsonschema.html
14.1.1.5. JSON Schema primer — steelscript documentation
Referencing the full_name schema ...pis/types/1.0#/types/full_name/properties/first' The anyOf keyword takes an array of schemas as a value and may be combined with other composite types as well as a base type....
🌐
GitHub
github.com › ajv-validator › ajv › issues › 134
Multiple array items type validation with oneOf, anyOf · Issue #134 · ajv-validator/ajv
March 1, 2016 - Multiple array items type validation with oneOf, anyOf#134 · Copy link · Labels · duplicateusage · mgendre · opened · on Mar 1, 2016 · Issue body actions · Hi everyone, I've encountered a problem to validate multiple items type in an array. We have an array with 2 items types. We try to validate each item to 2 different schemas types. Here is the schema: { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "transfer": { "type": "object", "properties": { "modes": { "type": "array", "minItems": 1, "additionalItems": false, "items": { "oneOf": [ { "typ
Author   mgendre
🌐
Opis
opis.io › json-schema › 2.x › multiple-subschemas.html
Applying multiple subschemas | Opis JSON Schema
An instance is valid against this keyword if is valid against at least one schema defined by the value of this keyword. The value of this keyword must be an array of valid JSON schemas (objects or booleans). ... The array is valid if contains 0 or "ok". Am empty array is not valid. Examples · Please pay attention when using anyOf!
Find elsewhere
🌐
Cswr
cswr.github.io › JsonSchema › spec › arrays
Arrays - JSON Schema
In this case, we are asking that every element of the array must be an integer. For example, this array validates against the schema ... The second way of restricting the elements is to specify a JSON Schema for each element in the array.
🌐
Liquid Technologies
liquid-technologies.com › Reference › XmlStudio › JsonEditorNotation_XXXOf.html
AllOf, AnyOf, OneOf
AllOf : All of the contained schemas must validate against the instance value. AnyOf : One or more of the contained schemas must validate against the instance value.
Top answer
1 of 5
73

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"]
                  }
               }
            }
        ]
    }
}
2 of 5
7

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

🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › conditionals
JSON Schema - Conditional schema validation
{ "type": "object", "properties": { "restaurantType": { "enum": ["fast-food", "sit-down"] }, "total": { "type": "number" }, "tip": { "type": "number" } }, "anyOf": [ { "not": { "properties": { "restaurantType": { "const": "sit-down" } }, "required": ["restaurantType"] } }, { "required": ["tip"] } ]}
🌐
JSON Schema
json-schema.org › blog › posts › applicability-json-schema-fundamentals-part-1
It all starts with applicability - JSON Schema Fundamentals part 1
March 20, 2022 - anyOf - If "any of" the assertions are true, then the combined assertion is true, otherwise false. But what about oneOf? The boolean logic used for that keyword is an exclusive OR... sort of. "XOR" for short is often used with electronics but doesn't translate exactly to "one and only one can ...
🌐
Swagger
swagger.io › docs › specification › v3_0 › data-models › oneof-anyof-allof-not
oneOf, anyOf, allOf, not | Swagger Docs
The following JSON object is valid ... and extend model definitions using the allOf keyword. allOf takes an array of object definitions that are used for independent validation but together compose a single object....
🌐
GitHub
github.com › java-json-tools › json-schema-validator › issues › 46
Using JSON schema with an array of mixed object types · Issue #46 · java-json-tools/json-schema-validator
March 6, 2013 - as far as I understand - this should be a valid schema: { "type" : "array", "items" : { "type" : [ { "type" : "object", "properties" : { "name" : { "type" : "string" }}}, { "type" : "object", "properties" : { "price" : { "type" : "number...
Author   ettig
🌐
JSON Forms community
jsonforms.discourse.group › general
How can I show some properties of an array which contains anyOf? - General - JSON Forms community
March 2, 2021 - Hi, I am trying to write uischema. I have an array which includes anyof and I need to show only some properties in the array. For example, x and y exist in the array. I will show only x. I write below uischema but I got subschema findIndex error and it’s related to usage of anyOf.
🌐
Postman
community.postman.com › help hub
Validating schema with an array - Help Hub - Postman Community
January 3, 2021 - Hi everyone. Happy New Year! I need some help on validating a JSON schema with an array. The array has an object with properties that I would like to validate. JSON example below. [ { “id”: “05265cbe-e811-4033-af3a-d927fb87235b”, “name”: “Bickley Towers”, “type”: null, “description”: “Rebuild of Bickley Towers”, “location”: { “addressLine1”: “58 Grove street”, “addressLine2”: null, “city”: “Bromley”, “stateOrCounty”: “Greater London”, “postcodeOrZipcode”: “BR1 7LP”, “country”: “UK” ...