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 Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
Kind Applicator
Applies To Any
Base Dialect Draft 7
Changed In None
Introduced In Draft 4
Vocabulary Validation
Specification https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.7.2
Metaschema http://json-schema.org/draft-07/schema#
Official Tests draft7/anyOf.json
Default None
Annotation None
Affected By None
Affects None
Also See

The anyOf keyword restricts instances to validate against at least one (but potentially multiple) of the given subschemas. This keyword represents a logical disjunction (OR) operation, as instances are valid if they satisfy the constraints of one or more subschemas (the union of the constraints).

This keyword is equivalent to the || operator found in most programming languages. For example:

bool valid = A || B || C;

As a reference, the following boolean truth table considers the evaluation result of this keyword given 3 subschemas: A, B, and C.

anyOf Subschema A Subschema B Subschema C
Invalid Invalid Invalid Invalid
Valid Invalid Invalid Valid
Valid Invalid Valid Invalid
Valid Invalid Valid Valid
Valid Valid Invalid Invalid
Valid Valid Invalid Valid
Valid Valid Valid Invalid
Valid Valid Valid Valid

Examples

A schema that constrains object instances to require at least one of the given properties Schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "anyOf": [
    { "required": [ "foo" ] },
    { "required": [ "bar" ] }
  ]
}
Valid A value that only matches the first subschema is valid Instance
{ "foo": 1 }
Valid A value that only matches the second subschema is valid Instance
{ "bar": 2 }
Valid A value that matches every subschema is valid Instance
{ "foo": 1, "bar": 2 }
Invalid A value that does not match any of the subschemas is invalid Instance
{ "extra": 4 }