Skip to main content
The oneOf keyword validates an instance against exactly one schema from its array value. This enforces mutual exclusivity between schema alternatives.

Syntax

The value must be a non-empty array where each item is a valid JSON Schema.

Behavior

An instance validates successfully against oneOf if it validates successfully against exactly one schema in the array.
  • All subschemas are evaluated independently
  • Exactly one subschema must succeed
  • If zero or more than one subschema succeeds, the entire oneOf fails
  • This enforces mutual exclusivity between alternatives

Examples

Discriminated Types

Require objects with mutually exclusive type indicators:
Valid instances:
Invalid:
(Would match both schemas if we used anyOf)

Exclusive Number Ranges

Require numbers in one of several non-overlapping ranges:
Valid: 5, 15, 25
Invalid: 10 (matches two schemas), -5, 35

Version-Specific Schemas

Enforce different schemas based on version field:
Valid instances:

Payment Method Selection

Require exactly one payment method:
Valid: Object with exactly one payment method
Invalid: Object with zero or multiple payment methods

Boolean XOR Logic

Implement exclusive-OR behavior:
Valid:
Invalid:

Common Use Cases

  • Discriminated unions: Enforce mutually exclusive object types
  • Non-overlapping ranges: Validate values in exclusive ranges
  • Single selection: Require exactly one option from a set
  • Version enforcement: Apply different schemas based on version
  • API variants: Support different but exclusive API formats

Comparison with Other Keywords

Notes

  • Unlike anyOf, validation fails if more than one subschema matches
  • This can catch unintended overlaps in schema design
  • Performance consideration: all subschemas must be evaluated to count matches
  • Nested oneOf can lead to exponential evaluation paths
  • Empty arrays are not permitted
  • Use discriminator fields (like type or version) to make schemas truly exclusive