| layout | default |
|---|---|
| title | Composition |
| parent | Features |
| nav_order | 5 |
YAML Schema provides several composition operators that allow you to combine multiple schemas: allOf, anyOf, oneOf, and not.
The allOf keyword requires that the value validates against all of the provided schemas.
# Schema
allOf:
- type: string
minLength: 1
- type: string
maxLength: 5Valid examples:
"short"Invalid examples:
"""too long"The value must satisfy both constraints: it must be a string with length between 1 and 5 characters.
The anyOf keyword requires that the value validates against at least one of the provided schemas.
# Schema
anyOf:
- type: string
maxLength: 5
- type: number
minimum: 0Valid examples:
"short"12Invalid examples:
"too long"-5trueThe value can be either a short string (5 characters or less) OR a non-negative number.
# Schema
description: A string or a number
anyOf:
- type: string
- type: numberValid examples:
"I am a string"42Invalid examples:
trueThe oneOf keyword requires that the value validates against exactly one of the provided schemas (not zero, not multiple).
# Schema
oneOf:
- type: number
multipleOf: 5
- type: number
multipleOf: 3Valid examples:
109Invalid examples:
215The value 15 is rejected because it's a multiple of both 5 and 3, which means it matches multiple schemas. The value 2 is rejected because it matches none of the schemas.
# Schema
oneOf:
- type: object
properties:
type:
const: "integer"
minimum:
type: integer
maximum:
type: integer
required:
- type
- type: object
properties:
type:
const: "string"
required:
- typeValid examples:
type: integertype: integer
minimum: 1
maximum: 10type: stringInvalid examples:
type: boolean# Schema
type: object
properties:
child:
oneOf:
- type: null
- type: object
properties:
name:
type: string
required:
- name
additionalProperties: falseValid examples:
child: nullchild:
name: JohnInvalid examples:
name: JohnThis schema allows child to be either null or an object with a required name property.
# Schema
type: object
properties:
name:
type: string
github:
type: object
properties:
environments:
type: object
patternProperties:
"^[a-zA-Z][a-zA-Z0-9_-]*$":
type: object
properties:
reviewers:
oneOf:
- type: null
- type: array
items:
type: stringValid examples:
name: test
github:
environments:
development:
reviewers: nullname: test
github:
environments:
production:
reviewers:
- alice
- bobInvalid examples:
name: test
github:
environments:
development:
reviewers: true# Schema
type: object
patternProperties:
^[a-zA-Z0-9]+$:
oneOf:
- type: null
- type: object
properties:
name:
type: stringValid examples:
a1b:
name: JohnThe not keyword requires that the value does not validate against the provided schema.
# Schema
not:
type: stringValid examples:
42key: valueInvalid examples:
"I am a string"# Schema
not:
type: number
multipleOf: 2Valid examples:
1-13Invalid examples:
2-2This schema accepts any value that is NOT an even number.
Composition operators can be nested and combined to create complex validation rules:
# Schema
type: object
properties:
child:
oneOf:
- type: null
- type: object
properties:
name:
type: string
required:
- name
additionalProperties: false# Schema
type: object
properties:
github:
type: object
properties:
environments:
type: object
patternProperties:
"^[a-zA-Z][a-zA-Z0-9_-]*$":
type: object
properties:
reviewers:
oneOf:
- type: null
- type: array
items:
type: stringThese examples show how oneOf can be used within object properties and pattern properties to create flexible schemas that allow multiple valid forms.