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.

Kind Applicator
Applies To Any
Dialect 2020-12
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
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.

  • The value of this keyword must be a non-empty array.
  • Each item of the array must be a valid JSON Schema.

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" ]
    }
  ]
}
An instance conforming to at least one subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
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" ]
    }
  ]
}
An instance conforming to at least one of the subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
An instance that does not conform to any of the subschemas of 'anyOf' is invalid Instance
{ "foo": 33, "bar": "bar" }
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" ]
    }
  ]
}
An instance conforming to the second subschema of 'anyOf' is valid Instance
{ "foo": "foo" }
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" ]
    }
  ]
}
An instance conforming to all the subschemas of 'anyOf' is valid Instance
{ "foo": "foo" }
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 }
      ]
    }
  ]
}
An instance conforming to all the subschemas including the nested 'anyOf' is valid Instance
25
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.