anyOf : Array<Schema>
anyOf
Array<Schema>An instance validates successfully against this keyword if it validates successfully against at least one schema defined by this keyword’s value.
Value | This keyword must be set to a non-empty array, where each item is a valid JSON Schema |
---|---|
Kind | Applicator |
Applies To | Any |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 4 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.1.2 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/anyOf.json |
Default |
As if it was set to the (invalid) value:
[]
|
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The anyOf
keyword in JSON Schema is used to specify that an instance must validate against at least one of the schemas provided in an array. It allows you to define multiple schemas, and if the data validates against any one of them, the validation passes.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"anyOf": [
{
"properties": {
"foo": { "type": "string" }
},
"required": [ "foo" ]
}
]
}
{ "foo": "foo" }
{ "foo": [ "foo" ] }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"anyOf": [
{
"properties": {
"foo": { "type": "string" }
},
"required": [ "foo" ]
},
{
"properties": {
"bar": { "type": "number" }
},
"required": [ "bar" ]
}
]
}
{ "foo": "foo" }
{ "foo": 33, "bar": "bar" }
{ "foo": "foo", "bar": 33 }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
false,
{
"properties": {
"foo": { "type": "string" }
},
"required": [ "foo" ]
}
]
}
{ "foo": "foo" }
{ "foo": false }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
true,
{
"properties": {
"foo": { "type": "string" }
},
"required": [ "foo" ]
}
]
}
{ "foo": "foo" }
{ "foo": true }
- Remember, if any subschema within the
anyOf
keyword passes validation or has a booleantrue
value, the overall result ofanyOf
is considered valid.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{
"anyOf": [
{ "type": "number" }
]
},
{
"anyOf": [
{ "minimum": 18 }
]
}
]
}
25
10
-
For the first instance above, validation passes against the first subschema within
anyOf
, thereby making theanyOf
keyword valid, regardless of the validation result against the second subschema. -
Similarly, for the second instance above, validation passes against the first subschema within
anyOf
. Even though the instance does not conform to the second subschema, validation does not proceed to validate against it, as it has already been successfully validated against the first subschema. Thus, the validation stops at that point, rendering theanyOf
valid, despite the instance not conforming to the second subschema within theanyOf
.
Common Pitfall
Important note on performance and annotations:
- When running validation only, a JSON Schema implementation may stop validating
anyOf
when it encounters a successful one. However, when collecting annotations, it must evaluate all of them. - The process of collecting annotations can impact runtime performance. For instance, if you collect annotations on a JSON Schema utilizing the
anyOf
applicator, the implementation is forced to evaluate the instance against every disjunction in theanyOf
applicator. Conversely, if annotations are not collected, implementations may stop evaluation as soon as oneanyOf
subschema successfully validates against the instance. - In the interest of runtime efficiency, we recommend collecting annotations only if your specific use case demands it.