| layout | default |
|---|---|
| title | Validation |
| parent | Features |
| nav_order | 4 |
YAML Schema provides various validation constraints that can be applied to types. This page covers enums, constants, and other validation features.
The enum keyword restricts values to a specific set of allowed values.
# Schema
enum:
- red
- amber
- greenValid examples:
redgreenInvalid examples:
blueEnums can contain values of different types:
# Schema
enum:
- red
- amber
- green
- null
- 42Valid examples:
rednull42Invalid examples:
0You can combine enum with a type constraint:
# Schema
type: object
properties:
version:
type: integer
enum: [1]Valid examples:
version: 1Invalid examples:
version: 2# Schema
type: array
prefixItems:
- type: number
- type: string
- enum:
- Street
- Avenue
- Boulevard
- enum:
- NW
- NE
- SW
- SEValid examples:
- 1600
- Pennsylvania
- Avenue
- NWInvalid examples:
- 24
- Sussex
- Drive# Schema
type: integer
enum: [1, 10, 100]Valid examples:
110100Invalid examples:
101# Schema
type: number
enum: [-1.0, 0.0, 1.0]Valid examples:
-1.00.01.0Invalid examples:
3.14The const keyword requires the value to be exactly equal to a specific constant value.
# Schema
type: object
properties:
country:
const: United States of AmericaValid examples:
country: United States of AmericaInvalid examples:
country: CanadaConstants are often used with composition operators like oneOf:
# 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: string
minLength: 2
maxLength: 3Valid examples:
"AB""ABC"Invalid examples:
"A""ABCD"# Schema
type: string
pattern: "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"Valid examples:
"555-1212""(888)555-1212"Invalid examples:
"(888)555-1212 ext. 532""(800)FLOWERS"# Schema
type: number
multipleOf: 10Valid examples:
01020Invalid examples:
23# Schema
type: number
minimum: 0
exclusiveMaximum: 100Valid examples:
01099Invalid examples:
-1100101Available range keywords:
minimum- Inclusive minimum valuemaximum- Inclusive maximum valueexclusiveMinimum- Exclusive minimum valueexclusiveMaximum- Exclusive maximum value
The contains keyword requires that at least one item in the array matches the schema:
# Schema
type: array
contains:
type: numberValid examples:
- life
- universe
- everything
- 42- 1
- 2
- 3
- 4
- 5Invalid examples:
- life
- universe
- everything
- forty-twoSee Types for minItems, maxItems, uniqueItems, and minContains / maxContains with contains.
# Schema
type: object
properties:
name:
type: string
email:
type: string
required:
- name
- emailValid examples:
name: William Shakespeare
email: bill@stratford-upon-avon.co.ukInvalid examples:
name: William Shakespeare
address: Henley Street, Stratford-upon-Avon, Warwickshire, Englandname: William Shakespeare
email: nullNote: A property with a null value is considered not being present.
# Schema
type: object
minProperties: 2
maxProperties: 3Valid examples:
a: 0
b: 1a: 0
b: 1
c: 2Invalid examples:
{}a: 0a: 0
b: 1
c: 2
d: 3# Schema
type: object
propertyNames:
pattern: "^[A-Za-z_][A-Za-z0-9_]*$"Valid examples:
_a_proper_token_001: "value"Invalid examples:
-001 invalid: "value"You can add descriptions to schemas for documentation purposes. Descriptions don't affect validation:
# Schema
type: string
description: "First name"# Schema
description: A string or a number
anyOf:
- type: string
- type: number# Schema
type: number
description: The description