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 with 'anyOf' keyword containing only one subschema Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "anyOf": [
    {
      "properties": {
        "foo": { "type": "string" }
      },
      "required": [ "foo" ]
    }
  ]
}
Valid An instance conforming to at least one subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
Invalid The value of 'foo' must be a string Instance
{ "foo": [ "foo" ] }
Schema with 'anyOf' keyword containing multiple subschemas Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "anyOf": [
    {
      "properties": {
        "foo": { "type": "string" }
      },
      "required": [ "foo" ]
    },
    {
      "properties": {
        "bar": { "type": "number" }
      },
      "required": [ "bar" ]
    }
  ]
}
Valid An instance conforming to at least one of the subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
Invalid An instance that does not conform to any of the subschemas of 'anyOf' is invalid Instance
{ "foo": 33, "bar": "bar" }
Valid An instance conforming to all the subschemas of 'anyOf' is also valid Instance
{ "foo": "foo", "bar": 33 }
Schema with 'anyOf' keyword containing some boolean subschemas Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "anyOf": [
    false,
    {
      "properties": {
        "foo": { "type": "string" }
      },
      "required": [ "foo" ]
    }
  ]
}
Valid An instance conforming to the second subschema of 'anyOf' is valid Instance
{ "foo": "foo" }
Invalid An instance not conforming to the second subschema of 'anyOf' is invalid Instance
{ "foo": false }
Schema with 'anyOf' keyword containing some boolean subschemas Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "anyOf": [
    true,
    {
      "properties": {
        "foo": { "type": "string" }
      },
      "required": [ "foo" ]
    }
  ]
}
Valid An instance conforming to all the subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
Valid An instance not conforming to the second subschema of 'anyOf' is also valid Instance
{ "foo": true }
  • Remember, if any subschema within the anyOf keyword passes validation or has a boolean true value, the overall result of anyOf is considered valid.
Schema with nested 'anyOf' Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "anyOf": [
    {
      "anyOf": [
        { "type": "number" }
      ]
    },
    {
      "anyOf": [
        { "minimum": 18 }
      ]
    }
  ]
}
Valid An instance conforming to all the subschemas including the nested 'anyOf' is valid Instance
25
Valid An instance not conforming to the second subschema of top-level 'anyOf' is also valid Instance
10
  • For the first instance above, validation passes against the first subschema within anyOf, thereby making the anyOf 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 the anyOf valid, despite the instance not conforming to the second subschema within the anyOf.