| layout | default |
|---|---|
| title | Types |
| parent | Features |
| nav_order | 2 |
YAML Schema supports validation for all YAML data types. This page covers string, number, integer, boolean, null, array, and object types.
The string type validates text values.
# Schema
type: stringValid examples:
"Déjà vu""""42"Invalid examples:
42true# Schema
type: string
minLength: 2
maxLength: 3Valid examples:
"AB""ABC"Invalid examples:
"A""ABCD"minLength and maxLength count Unicode scalar values (code points), not UTF-8 bytes. For example, with maxLength: 2, "αβ" is valid and "αβγ" is too long.
For dates, emails, URIs, and other standard string shapes, see String formats.
# 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"The number type validates both integers and floating-point numbers.
# Schema
type: numberValid examples:
423.14Invalid examples:
"I'm a string"The integer type validates whole numbers. Note that 1.0 is considered an integer.
# Schema
type: integerValid examples:
42-11.0Invalid examples:
3.1415926"42"# Schema
type: number
multipleOf: 10Valid examples:
01020Invalid examples:
23# Schema
type: number
minimum: 0
exclusiveMaximum: 100Valid examples:
01099Invalid examples:
-1100101Note: minimum is inclusive, while exclusiveMaximum is exclusive.
The boolean type validates true/false values.
# Schema
type: booleanValid examples:
truefalseInvalid examples:
"true"The null type validates only null values.
# Schema
type: nullValid examples:
nullInvalid examples:
false0""The array type validates ordered lists of values.
# Schema
type: arrayValid examples:
- 1
- 2
- 3
- 4
- 5- 3
- different
- types: "of values"Invalid examples:
Not: "an array"# Schema
type: array
items:
type: numberValid examples:
- 1
- 2
- 3
- 4
- 5[]Invalid examples:
- 1
- 2
- "3"
- 4
- 5Note: A single non-matching item causes the entire array to be invalid. Empty arrays are always valid.
# Schema
type: array
prefixItems:
- type: number
- type: string
- enum:
- Street
- Avenue
- Boulevard
- enum:
- NW
- NE
- SW
- SEValid examples:
- 1600
- Pennsylvania
- Avenue
- NW- 10
- Downing
- Street- 1600
- Pennsylvania
- Avenue
- NW
- WashingtonInvalid examples:
- 24
- Sussex
- Drive- Palais de l'ÉlyséeBy default, additional items beyond prefixItems are allowed. You can restrict this:
# Schema
type: array
prefixItems:
- type: number
- type: string
items: falseValid examples:
- 1600
- Pennsylvania
- Avenue
- NW- 1600
- Pennsylvania
- AvenueInvalid examples:
- 1600
- Pennsylvania
- Avenue
- NW
- WashingtonYou can also specify a schema for additional items:
# Schema
type: array
prefixItems:
- type: number
- type: string
items:
type: stringValid examples:
- 1600
- Pennsylvania
- Avenue
- NW
- WashingtonInvalid examples:
- 1600
- Pennsylvania
- Avenue
- NW
- 20500The 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-two# Schema
type: array
minItems: 2Too few elements (including []) fails validation. maxItems rejects arrays longer than the limit; an empty array is always allowed when minItems is not set.
Combined with items:
type: array
minItems: 2
maxItems: 4
items:
type: numberWhen uniqueItems is true, no two elements may be equal (including strings). false allows duplicates. Empty and single-element arrays are always valid.
type: array
uniqueItems: trueWith contains, minContains (default 1) is the minimum number of elements that must match the contains schema; maxContains caps how many may match.
type: array
contains:
type: number
minContains: 2At least two numbers are required somewhere in the array.
type: array
contains:
type: number
maxContains: 3At most three elements may be numbers that satisfy contains.
Setting minContains: 0 means the contains constraint is satisfied even when no element matches (the array may have zero matches).
The object type validates key-value mappings.
# Schema
type: objectValid examples:
key: value
another_key: another_valueSun: 1.9891e30
Jupiter: 1.8986e270.01: cm
1: m
1000: kmInvalid examples:
"Not an object"["An", "array", "not", "an", "object"]Note: Unlike JSON, YAML allows numeric keys, which are treated as strings.
# Schema
type: object
properties:
number:
type: number
street_name:
type: string
street_type:
enum: [Street, Avenue, Boulevard]Valid examples:
number: 1600
street_name: Pennsylvania
street_type: Avenuenumber: 1600
street_name: Pennsylvania{}number: 1600
street_name: Pennsylvania
street_type: Avenue
direction: NWInvalid examples:
number: "1600"
street_name: Pennsylvania
street_type: AvenueNote: By default, leaving out properties is valid, and providing additional properties is also valid.
# Schema
type: object
patternProperties:
^S_:
type: string
^I_:
type: integerValid examples:
S_25: This is a stringI_0: 42keyword: valueInvalid examples:
S_0: 42I_42: This is a stringBy default, additional properties are allowed. You can restrict this:
# Schema
type: object
properties:
number:
type: number
street_name:
type: string
additionalProperties: falseValid examples:
number: 1600
street_name: PennsylvaniaInvalid examples:
number: 1600
street_name: Pennsylvania
direction: NWYou can also specify a schema for additional properties:
# Schema
type: object
properties:
number:
type: number
additionalProperties:
type: stringValid examples:
number: 1600
direction: NWInvalid examples:
number: 1600
office_number: 201# Schema
type: object
properties:
name:
type: string
email:
type: string
required:
- name
- emailValid examples:
name: William Shakespeare
email: bill@stratford-upon-avon.co.ukname: William Shakespeare
email: bill@stratford-upon-avon.co.uk
address: Henley Street, Stratford-upon-Avon, Warwickshire, England
authorship: in questionInvalid examples:
name: William Shakespeare
address: Henley Street, Stratford-upon-Avon, Warwickshire, Englandname: William Shakespeare
address: Henley Street, Stratford-upon-Avon, Warwickshire, England
email: nullNote: A property with a null value is considered not being present.
# Schema
type: object
propertyNames:
pattern: "^[A-Za-z_][A-Za-z0-9_]*$"Valid examples:
_a_proper_token_001: "value"Invalid examples:
-001 invalid: "value"Error: [1:1] .: Property name '-001 invalid' does not match pattern '^[A-Za-z_][A-Za-z0-9_]*$'
# 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