-
-
Notifications
You must be signed in to change notification settings - Fork 886
/
Copy pathjson-schema.html
424 lines (421 loc) · 118 KB
/
json-schema.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>JSON Schema | Ajv JSON schema validator</title>
<meta name="generator" content="VuePress 1.9.10">
<link rel="icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/img/apple-touch-icon.png">
<meta name="description" content="The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)">
<meta charset="utf-8">
<meta property="og:title" content="Ajv JSON schema validator">
<meta property="og:description" content="The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)">
<meta property="og:image" content="https://ajv.js.org/img/ajv.png">
<meta itemprop="image" content="https://ajv.js.org/img/ajv.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Ajv JSON schema validator">
<meta name="twitter:image:src" content="https://ajv.js.org/img/ajv.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="preload" href="/assets/css/0.styles.7f8cbd9f.css" as="style"><link rel="preload" href="/assets/js/app.bb2a0f22.js" as="script"><link rel="preload" href="/assets/js/2.53bb977e.js" as="script"><link rel="preload" href="/assets/js/1.6a0e5398.js" as="script"><link rel="preload" href="/assets/js/31.0d5db059.js" as="script"><link rel="preload" href="/assets/js/62.17eb9b44.js" as="script"><link rel="preload" href="/assets/js/4.eb19df75.js" as="script"><link rel="preload" href="/assets/js/6.af23b107.js" as="script"><link rel="prefetch" href="/assets/js/10.2daf032a.js"><link rel="prefetch" href="/assets/js/11.a815211e.js"><link rel="prefetch" href="/assets/js/12.02b59a12.js"><link rel="prefetch" href="/assets/js/13.d7ad60db.js"><link rel="prefetch" href="/assets/js/14.d239147b.js"><link rel="prefetch" href="/assets/js/15.2968b1c8.js"><link rel="prefetch" href="/assets/js/16.2b81cdeb.js"><link rel="prefetch" href="/assets/js/17.0bd06c00.js"><link rel="prefetch" href="/assets/js/18.93d6b8b2.js"><link rel="prefetch" href="/assets/js/19.1c7f3cdf.js"><link rel="prefetch" href="/assets/js/20.0105b7b9.js"><link rel="prefetch" href="/assets/js/21.0b8b9d8c.js"><link rel="prefetch" href="/assets/js/22.6bf94e96.js"><link rel="prefetch" href="/assets/js/23.5161123f.js"><link rel="prefetch" href="/assets/js/24.2547ea08.js"><link rel="prefetch" href="/assets/js/25.d5f09093.js"><link rel="prefetch" href="/assets/js/26.4f21deb9.js"><link rel="prefetch" href="/assets/js/27.1927cbbb.js"><link rel="prefetch" href="/assets/js/28.351022dc.js"><link rel="prefetch" href="/assets/js/29.7ab70007.js"><link rel="prefetch" href="/assets/js/3.6e15e07e.js"><link rel="prefetch" href="/assets/js/30.494f3f73.js"><link rel="prefetch" href="/assets/js/32.d0b77cdc.js"><link rel="prefetch" href="/assets/js/33.ccd2d1e9.js"><link rel="prefetch" href="/assets/js/34.dd55990b.js"><link rel="prefetch" href="/assets/js/35.d1c57952.js"><link rel="prefetch" href="/assets/js/36.493ef13c.js"><link rel="prefetch" href="/assets/js/37.a208b6e1.js"><link rel="prefetch" href="/assets/js/38.7676b23b.js"><link rel="prefetch" href="/assets/js/39.235d5dca.js"><link rel="prefetch" href="/assets/js/40.298e5e8f.js"><link rel="prefetch" href="/assets/js/41.1cc1a790.js"><link rel="prefetch" href="/assets/js/42.befafe98.js"><link rel="prefetch" href="/assets/js/43.120a3999.js"><link rel="prefetch" href="/assets/js/44.eb7d5064.js"><link rel="prefetch" href="/assets/js/45.38b67184.js"><link rel="prefetch" href="/assets/js/46.452b30ba.js"><link rel="prefetch" href="/assets/js/47.e3997d4d.js"><link rel="prefetch" href="/assets/js/48.a6fa3c8a.js"><link rel="prefetch" href="/assets/js/49.633e50de.js"><link rel="prefetch" href="/assets/js/5.d186db21.js"><link rel="prefetch" href="/assets/js/50.6fb28fbe.js"><link rel="prefetch" href="/assets/js/51.d223469b.js"><link rel="prefetch" href="/assets/js/52.431b7581.js"><link rel="prefetch" href="/assets/js/53.34897aa3.js"><link rel="prefetch" href="/assets/js/54.34a853ca.js"><link rel="prefetch" href="/assets/js/55.6cf21dac.js"><link rel="prefetch" href="/assets/js/56.b43e8f41.js"><link rel="prefetch" href="/assets/js/57.1bf25d53.js"><link rel="prefetch" href="/assets/js/58.8bcb567a.js"><link rel="prefetch" href="/assets/js/59.84baa59d.js"><link rel="prefetch" href="/assets/js/60.63e5562f.js"><link rel="prefetch" href="/assets/js/61.0d386b18.js"><link rel="prefetch" href="/assets/js/63.fff784a9.js"><link rel="prefetch" href="/assets/js/64.a839483e.js"><link rel="prefetch" href="/assets/js/65.f8d0b252.js"><link rel="prefetch" href="/assets/js/66.75dea31f.js"><link rel="prefetch" href="/assets/js/67.14f577d9.js"><link rel="prefetch" href="/assets/js/68.39d52c84.js"><link rel="prefetch" href="/assets/js/69.fb6c89c0.js"><link rel="prefetch" href="/assets/js/7.ef23a276.js"><link rel="prefetch" href="/assets/js/70.41c11761.js"><link rel="prefetch" href="/assets/js/71.debaead8.js"><link rel="prefetch" href="/assets/js/72.dccf1359.js"><link rel="prefetch" href="/assets/js/73.db898e46.js"><link rel="prefetch" href="/assets/js/74.90e9d3b3.js"><link rel="prefetch" href="/assets/js/75.cf55aad8.js"><link rel="prefetch" href="/assets/js/76.d89e5f67.js"><link rel="prefetch" href="/assets/js/77.b87bd756.js"><link rel="prefetch" href="/assets/js/78.a60bd479.js"><link rel="prefetch" href="/assets/js/79.2a264ec7.js"><link rel="prefetch" href="/assets/js/80.080d1ad8.js"><link rel="prefetch" href="/assets/js/81.a55d6327.js"><link rel="prefetch" href="/assets/js/82.2e8a4613.js"><link rel="prefetch" href="/assets/js/83.ae8c92d7.js"><link rel="prefetch" href="/assets/js/84.83ff6efb.js"><link rel="prefetch" href="/assets/js/85.f97f6785.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.1c8a58cd.js">
<link rel="stylesheet" href="/assets/css/0.styles.7f8cbd9f.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/ajv.svg" alt="Ajv JSON schema validator" class="logo"> <span class="site-name can-hide">Ajv JSON schema validator</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Guide" class="dropdown-title"><span class="title">Guide</span> <span class="arrow down"></span></button> <button type="button" aria-label="Guide" class="mobile-dropdown-title"><span class="title">Guide</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/why-ajv.html" class="nav-link">
Why use Ajv
</a></li><li class="dropdown-item"><!----> <a href="/guide/getting-started.html" class="nav-link">
Getting started
</a></li><li class="dropdown-item"><!----> <a href="/guide/typescript.html" class="nav-link">
Using with TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/guide/schema-language.html" class="nav-link">
Choosing schema language
</a></li><li class="dropdown-item"><!----> <a href="/guide/managing-schemas.html" class="nav-link">
Managing schemas
</a></li><li class="dropdown-item"><!----> <a href="/guide/combining-schemas.html" class="nav-link">
Combining schemas
</a></li><li class="dropdown-item"><!----> <a href="/guide/formats.html" class="nav-link">
Format validation
</a></li><li class="dropdown-item"><!----> <a href="/guide/modifying-data.html" class="nav-link">
Modifying data
</a></li><li class="dropdown-item"><!----> <a href="/guide/user-keywords.html" class="nav-link">
User-defined keywords
</a></li><li class="dropdown-item"><!----> <a href="/guide/async-validation.html" class="nav-link">
Asynchronous validation
</a></li><li class="dropdown-item"><!----> <a href="/guide/environments.html" class="nav-link">
Execution environments
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Reference" class="dropdown-title"><span class="title">Reference</span> <span class="arrow down"></span></button> <button type="button" aria-label="Reference" class="mobile-dropdown-title"><span class="title">Reference</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/api.html" class="nav-link">
API Reference
</a></li><li class="dropdown-item"><!----> <a href="/options.html" class="nav-link">
Ajv options
</a></li><li class="dropdown-item"><!----> <a href="/json-schema.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
JSON Schema
</a></li><li class="dropdown-item"><!----> <a href="/json-type-definition.html" class="nav-link">
JSON Type Definition
</a></li><li class="dropdown-item"><!----> <a href="/strict-mode.html" class="nav-link">
Strict mode
</a></li><li class="dropdown-item"><!----> <a href="/standalone.html" class="nav-link">
Standalone validation code
</a></li><li class="dropdown-item"><!----> <a href="/keywords.html" class="nav-link">
User defined keywords
</a></li><li class="dropdown-item"><!----> <a href="/coercion.html" class="nav-link">
Type coercion rules
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Learn more" class="dropdown-title"><span class="title">Learn more</span> <span class="arrow down"></span></button> <button type="button" aria-label="Learn more" class="mobile-dropdown-title"><span class="title">Learn more</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
Extending Ajv
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/packages/" class="nav-link">
Extending Ajv
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-cli.html" class="nav-link">
ajv-cli
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-errors.html" class="nav-link">
ajv-errors
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-formats.html" class="nav-link">
ajv-formats
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-i18n.html" class="nav-link">
ajv-i18n
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-keywords.html" class="nav-link">
ajv-keywords
</a></li></ul></li><li class="dropdown-item"><h4>
Contributors
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/contributing.html" class="nav-link">
Contributing guide
</a></li><li class="dropdown-subitem"><a href="/codegen.html" class="nav-link">
Code generation design
</a></li><li class="dropdown-subitem"><a href="/components.html" class="nav-link">
Code components
</a></li><li class="dropdown-subitem"><a href="/code_of_conduct.html" class="nav-link">
Code of Conduct
</a></li></ul></li><li class="dropdown-item"><h4>
Information
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/news/" class="nav-link">
News
</a></li><li class="dropdown-subitem"><a href="/faq.html" class="nav-link">
FAQ
</a></li><li class="dropdown-subitem"><a href="/security.html" class="nav-link">
Security
</a></li><li class="dropdown-subitem"><a href="/v6-to-v8-migration.html" class="nav-link">
Migrate from v6
</a></li><li class="dropdown-subitem"><a href="/testimonials.html" class="nav-link">
What users say
</a></li><li class="dropdown-subitem"><a href="/license.html" class="nav-link">
License
</a></li></ul></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Guide" class="dropdown-title"><span class="title">Guide</span> <span class="arrow down"></span></button> <button type="button" aria-label="Guide" class="mobile-dropdown-title"><span class="title">Guide</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/why-ajv.html" class="nav-link">
Why use Ajv
</a></li><li class="dropdown-item"><!----> <a href="/guide/getting-started.html" class="nav-link">
Getting started
</a></li><li class="dropdown-item"><!----> <a href="/guide/typescript.html" class="nav-link">
Using with TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/guide/schema-language.html" class="nav-link">
Choosing schema language
</a></li><li class="dropdown-item"><!----> <a href="/guide/managing-schemas.html" class="nav-link">
Managing schemas
</a></li><li class="dropdown-item"><!----> <a href="/guide/combining-schemas.html" class="nav-link">
Combining schemas
</a></li><li class="dropdown-item"><!----> <a href="/guide/formats.html" class="nav-link">
Format validation
</a></li><li class="dropdown-item"><!----> <a href="/guide/modifying-data.html" class="nav-link">
Modifying data
</a></li><li class="dropdown-item"><!----> <a href="/guide/user-keywords.html" class="nav-link">
User-defined keywords
</a></li><li class="dropdown-item"><!----> <a href="/guide/async-validation.html" class="nav-link">
Asynchronous validation
</a></li><li class="dropdown-item"><!----> <a href="/guide/environments.html" class="nav-link">
Execution environments
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Reference" class="dropdown-title"><span class="title">Reference</span> <span class="arrow down"></span></button> <button type="button" aria-label="Reference" class="mobile-dropdown-title"><span class="title">Reference</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/api.html" class="nav-link">
API Reference
</a></li><li class="dropdown-item"><!----> <a href="/options.html" class="nav-link">
Ajv options
</a></li><li class="dropdown-item"><!----> <a href="/json-schema.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
JSON Schema
</a></li><li class="dropdown-item"><!----> <a href="/json-type-definition.html" class="nav-link">
JSON Type Definition
</a></li><li class="dropdown-item"><!----> <a href="/strict-mode.html" class="nav-link">
Strict mode
</a></li><li class="dropdown-item"><!----> <a href="/standalone.html" class="nav-link">
Standalone validation code
</a></li><li class="dropdown-item"><!----> <a href="/keywords.html" class="nav-link">
User defined keywords
</a></li><li class="dropdown-item"><!----> <a href="/coercion.html" class="nav-link">
Type coercion rules
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Learn more" class="dropdown-title"><span class="title">Learn more</span> <span class="arrow down"></span></button> <button type="button" aria-label="Learn more" class="mobile-dropdown-title"><span class="title">Learn more</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
Extending Ajv
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/packages/" class="nav-link">
Extending Ajv
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-cli.html" class="nav-link">
ajv-cli
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-errors.html" class="nav-link">
ajv-errors
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-formats.html" class="nav-link">
ajv-formats
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-i18n.html" class="nav-link">
ajv-i18n
</a></li><li class="dropdown-subitem"><a href="/packages/ajv-keywords.html" class="nav-link">
ajv-keywords
</a></li></ul></li><li class="dropdown-item"><h4>
Contributors
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/contributing.html" class="nav-link">
Contributing guide
</a></li><li class="dropdown-subitem"><a href="/codegen.html" class="nav-link">
Code generation design
</a></li><li class="dropdown-subitem"><a href="/components.html" class="nav-link">
Code components
</a></li><li class="dropdown-subitem"><a href="/code_of_conduct.html" class="nav-link">
Code of Conduct
</a></li></ul></li><li class="dropdown-item"><h4>
Information
</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/news/" class="nav-link">
News
</a></li><li class="dropdown-subitem"><a href="/faq.html" class="nav-link">
FAQ
</a></li><li class="dropdown-subitem"><a href="/security.html" class="nav-link">
Security
</a></li><li class="dropdown-subitem"><a href="/v6-to-v8-migration.html" class="nav-link">
Migrate from v6
</a></li><li class="dropdown-subitem"><a href="/testimonials.html" class="nav-link">
What users say
</a></li><li class="dropdown-subitem"><a href="/license.html" class="nav-link">
License
</a></li></ul></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Guide</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Reference</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/api.html" class="sidebar-link">API Reference</a></li><li><a href="/options.html" class="sidebar-link">Ajv options</a></li><li><a href="/json-schema.html" aria-current="page" class="active sidebar-link">JSON Schema</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/json-schema.html#json-schema-versions" class="sidebar-link">JSON Schema versions</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#openapi-support" class="sidebar-link">OpenAPI support</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#json-data-type" class="sidebar-link">JSON data type</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#keywords-for-numbers" class="sidebar-link">Keywords for numbers</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#keywords-for-strings" class="sidebar-link">Keywords for strings</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#keywords-for-arrays" class="sidebar-link">Keywords for arrays</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#keywords-for-objects" class="sidebar-link">Keywords for objects</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#keywords-for-all-types" class="sidebar-link">Keywords for all types</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#compound-keywords" class="sidebar-link">Compound keywords</a></li><li class="sidebar-sub-header"><a href="/json-schema.html#metadata-keywords" class="sidebar-link">Metadata keywords</a></li></ul></li><li><a href="/json-type-definition.html" class="sidebar-link">JSON Type Definition</a></li><li><a href="/strict-mode.html" class="sidebar-link">Strict mode</a></li><li><a href="/standalone.html" class="sidebar-link">Standalone validation code</a></li><li><a href="/keywords.html" class="sidebar-link">User defined keywords</a></li><li><a href="/coercion.html" class="sidebar-link">Type coercion rules</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Extending Ajv</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Contributors</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Information</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="json-schema"><a href="#json-schema" class="header-anchor">#</a> JSON Schema</h1> <p>In a simple way, JSON Schema is an object with validation keywords.</p> <p>The keywords and their values define what rules the data should satisfy to be valid.</p> <p></p><div class="table-of-contents"><ul><li><a href="#json-schema-versions">JSON Schema versions</a><ul><li><a href="#draft-07">draft-07 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>default</span></a></li><li><a href="#draft-2019-09">draft-2019-09 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW</span></a></li><li><a href="#draft-2020-12">draft-2020-12 <span class="badge warning" style="vertical-align:top;" data-v-6eb0829b>BREAKING</span></a></li><li><a href="#draft-06">draft-06</a></li><li><a href="#draft-04">draft-04</a></li></ul></li><li><a href="#openapi-support">OpenAPI support</a></li><li><a href="#json-data-type">JSON data type</a><ul><li><a href="#type">type</a></li><li><a href="#nullable">nullable <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>OpenAPI</span></a></li></ul></li><li><a href="#keywords-for-numbers">Keywords for numbers</a><ul><li><a href="#maximum-minimum-and-exclusivemaximum-exclusiveminimum">maximum / minimum and exclusiveMaximum / exclusiveMinimum</a></li><li><a href="#multipleof">multipleOf</a></li></ul></li><li><a href="#keywords-for-strings">Keywords for strings</a><ul><li><a href="#maxlength-minlength">maxLength / minLength</a></li><li><a href="#pattern">pattern</a></li><li><a href="#format">format</a></li></ul></li><li><a href="#keywords-for-arrays">Keywords for arrays</a><ul><li><a href="#maxitems-minitems">maxItems / minItems</a></li><li><a href="#uniqueitems">uniqueItems</a></li><li><a href="#items">items</a><ul><li><a href="#items-in-draft-04-06-07-and-2019-09">items in draft-04, -06, -07 and -2019-09</a></li><li><a href="#items-in-draft-2020-12">items in draft-2020-12 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW</span></a></li></ul></li><li><a href="#prefixitems">prefixItems <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2020-12</span></a></li><li><a href="#additionalitems">additionalItems</a></li><li><a href="#contains">contains</a></li><li><a href="#maxcontains-mincontains">maxContains / minContains <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></a></li><li><a href="#unevaluateditems">unevaluatedItems <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></a></li></ul></li><li><a href="#keywords-for-objects">Keywords for objects</a><ul><li><a href="#maxproperties-minproperties">maxProperties / minProperties</a></li><li><a href="#required">required</a></li><li><a href="#properties">properties</a></li><li><a href="#patternproperties">patternProperties</a></li><li><a href="#additionalproperties">additionalProperties</a></li><li><a href="#dependencies">dependencies <span class="badge warning" style="vertical-align:top;" data-v-6eb0829b>deprecated in draft 2019-09</span></a></li><li><a href="#dependentrequired">dependentRequired <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></a></li><li><a href="#dependentschemas">dependentSchemas <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></a></li><li><a href="#propertynames">propertyNames</a></li><li><a href="#unevaluatedproperties">unevaluatedProperties <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></a></li><li><a href="#discriminator">discriminator <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: OpenAPI</span></a></li></ul></li><li><a href="#keywords-for-all-types">Keywords for all types</a><ul><li><a href="#enum">enum</a></li><li><a href="#const">const</a></li></ul></li><li><a href="#compound-keywords">Compound keywords</a><ul><li><a href="#not">not</a></li><li><a href="#oneof">oneOf</a></li><li><a href="#anyof">anyOf</a></li><li><a href="#allof">allOf</a></li><li><a href="#if-then-else">if/then/else</a></li></ul></li><li><a href="#metadata-keywords">Metadata keywords</a></li></ul></div><p></p> <h2 id="json-schema-versions"><a href="#json-schema-versions" class="header-anchor">#</a> JSON Schema versions</h2> <h3 id="draft-07"><a href="#draft-07" class="header-anchor">#</a> draft-07 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>default</span></h3> <p>This version is provided as default export:</p> <!----> <div class="custom-block tip"><p class="custom-block-title">draft-07 has better performance</p> <p>Unless you need the new features of later versions, you would have more efficient generated code with this draft.</p></div> <h3 id="draft-2019-09"><a href="#draft-2019-09" class="header-anchor">#</a> draft-2019-09 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW</span></h3> <p>Ajv supports all new keywords of JSON Schema draft-2019-09:</p> <ul><li><a href="#unevaluatedproperties">unevaluatedProperties</a></li> <li><a href="#unevaluateditems">unevaluatedItems</a></li> <li><a href="#dependentrequired">dependentRequired</a></li> <li><a href="#dependentschemas">dependentSchemas</a></li> <li><a href="#maxcontains--mincontains">maxContains/minContains</a></li> <li><a href="/guide/combining-schemas.html#extending-recursive-schemas">$recursiveAnchor/$recursiveRef</a></li></ul> <p>To use draft-2019-09 schemas you need to import a different Ajv class:</p> <!----> <p>You can use draft-07 schemas with this Ajv instance as well, draft-2019-09 is backwards compatible. If your schemas use <code>$schema</code> keyword, you need to add draft-07 meta-schema to Ajv instance:</p> <!----> <h3 id="draft-2020-12"><a href="#draft-2020-12" class="header-anchor">#</a> draft-2020-12 <span class="badge warning" style="vertical-align:top;" data-v-6eb0829b>BREAKING</span></h3> <div class="custom-block warning"><p class="custom-block-title">draft-2020-12 is not backwards compatible</p> <p>You cannot use draft-2020-12 and previous JSON Schema versions in the same Ajv instance.</p></div> <p>Ajv supports all keywords of JSON Schema draft-2020-12:</p> <ul><li><a href="#prefixItems">prefixItems</a> that replaced array form of items keyword</li> <li>changed <a href="#items-in-draft-2020-12">items</a> keyword that combined parts of functionality of items and additionalItems</li> <li><a href="/guide/combining-schemas.html#extending-recursive-schemas">$dynamicAnchor/$dynamicRef</a></li></ul> <p>To use draft-2020-12 schemas you need to import a different Ajv class:</p> <!----> <h3 id="draft-06"><a href="#draft-06" class="header-anchor">#</a> draft-06</h3> <p>You can use JSON Schema draft-06 schemas with Ajv v7/8. If your schemas use <code>$schema</code> keyword, you need to add draft-06 meta-schema to Ajv instance. This example shows how to support both draft-06 and draft-07 schemas:</p> <!----> <h3 id="draft-04"><a href="#draft-04" class="header-anchor">#</a> draft-04</h3> <p>You can use JSON Schema draft-04 schemas with Ajv from v8.5.0 and the additional package <a href="https://github.com/ajv-validator/ajv-draft-04" target="_blank" rel="noopener noreferrer">ajv-draft-04<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> (both ajv and ajv-draft-04 should be installed).</p> <!----> <div class="custom-block warning"><p class="custom-block-title">Ajv cannot combine multiple JSON Schema versions</p> <p>You can only use this import with JSON Schema draft-04, you cannot combine multiple JSON Schema versions in this ajv instance.</p></div> <h2 id="openapi-support"><a href="#openapi-support" class="header-anchor">#</a> OpenAPI support</h2> <p>Ajv supports these additional <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md" target="_blank" rel="noopener noreferrer">OpenAPI specification<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> keywords:</p> <ul><li><a href="#nullable">nullable</a> - to avoid using <code>type</code> keyword with array of types.</li> <li><a href="#discriminator">discriminator</a> - to optimize validation and error reporting of tagged unions</li></ul> <h2 id="json-data-type"><a href="#json-data-type" class="header-anchor">#</a> JSON data type</h2> <h3 id="type"><a href="#type" class="header-anchor">#</a> <code>type</code></h3> <p><code>type</code> keyword requires that the data is of certain type (or some of types). Its value can be a string (the allowed type) or an array of strings (multiple allowed types).</p> <p>Type can be: <code>number</code>, <code>integer</code>, <code>string</code>, <code>boolean</code>, <code>array</code>, <code>object</code> or <code>null</code>.</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "number"}</code></p> <p><em>valid</em>: <code>1</code>, <code>1.5</code></p> <p><em>invalid</em>: <code>"abc"</code>, <code>"1"</code>, <code>[]</code>, <code>{}</code>, <code>null</code>, <code>true</code></p></li></ol> <ol start="2"><li><p><em>schema</em>: <code>{type: "integer"}</code></p> <p><em>valid</em>: <code>1</code>, <code>2</code></p> <p><em>invalid</em>: <code>"abc"</code>, <code>"1"</code>, <code>1.5</code>, <code>[]</code>, <code>{}</code>, <code>null</code>, <code>true</code></p></li></ol> <ol start="3"><li><p><em>schema</em>: <code>{type: ["number", "string"]}</code></p> <p><em>valid</em>: <code>1</code>, <code>1.5</code>, <code>"abc"</code>, <code>"1"</code></p> <p><em>invalid</em>: <code>[]</code>, <code>{}</code>, <code>null</code>, <code>true</code></p></li></ol> <p>All examples above are JSON Schemas that only require data to be of certain type to be valid.</p> <p>Most other keywords apply only to a particular type of data. If the data is of different type, the keyword will not apply and the data will be considered valid.</p> <p>In v7 Ajv introduced <a href="/strict-mode.html#strict-types">Strict types</a> mode that makes these mistakes less likely by requiring that types are constrained with type keyword whenever another keyword that applies to specific type is used.</p> <h3 id="nullable"><a href="#nullable" class="header-anchor">#</a> nullable <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>OpenAPI</span></h3> <p>This keyword can be used to allow <code>null</code> value in addition to the defined <code>type</code>.</p> <p>Ajv supports it by default, without additional options. These two schemas are equivalent, but the first one is better supported by some tools and is also compatible with <code>strictTypes</code> option (see <a href="/strict-mode.html#strict-types">Strict types</a>)</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">,</span>
<span class="token property">"nullable"</span><span class="token operator">:</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
</code></pre></div><p>and</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">"type"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"string"</span><span class="token punctuation">,</span> <span class="token string">"null"</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="custom-block warning"><p class="custom-block-title">nullable does not extend enum and const</p> <p>If you use <a href="#enum">enum</a> or <a href="#const">const</a> keywords, <code>"nullable": true</code> would not extend the list of allowed values - <code>null</code> value has to be explicitly added to <code>enum</code> (and <code>const</code> would fail, unless it is <code>"const": null</code>)</p> <p>This is different from how <code>nullable</code> is defined in <a href="/json-type-definition.html">JSON Type Definition</a>, where <code>"nullable": true</code> allows <code>null</code> value in addition to any data defined by the schema.</p></div> <h2 id="keywords-for-numbers"><a href="#keywords-for-numbers" class="header-anchor">#</a> Keywords for numbers</h2> <h3 id="maximum-minimum-and-exclusivemaximum-exclusiveminimum"><a href="#maximum-minimum-and-exclusivemaximum-exclusiveminimum" class="header-anchor">#</a> <code>maximum</code> / <code>minimum</code> and <code>exclusiveMaximum</code> / <code>exclusiveMinimum</code></h3> <p>The value of keyword <code>maximum</code> (<code>minimum</code>) should be a number. This value is the maximum (minimum) allowed value for the data to be valid.</p> <p>The value of keyword <code>exclusiveMaximum</code> (<code>exclusiveMinimum</code>) should be a number. This value is the exclusive maximum (minimum) allowed value for the data to be valid (the data equal to this keyword value is invalid).</p> <div class="custom-block warning"><p class="custom-block-title">NO support for boolean keyword values</p> <p>Boolean values for keywords <code>exclusiveMaximum</code> (<code>exclusiveMinimum</code>) are not supported.</p></div> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "number", maximum: 5}</code></p> <p><em>valid</em>: <code>4</code>, <code>5</code></p> <p><em>invalid</em>: <code>6</code>, <code>7</code></p></li> <li><p><em>schema</em>: <code>{type: "number", minimum: 5}</code></p> <p><em>valid</em>: <code>5</code>, <code>6</code></p> <p><em>invalid</em>: <code>4</code>, <code>4.5</code></p></li> <li><p><em>schema</em>: <code>{type: "number", exclusiveMinimum: 5}</code></p> <p><em>valid</em>: <code>6</code>, <code>7</code></p> <p><em>invalid</em>: <code>4.5</code>, <code>5</code></p></li></ol> <h3 id="multipleof"><a href="#multipleof" class="header-anchor">#</a> <code>multipleOf</code></h3> <p>The value of the keyword should be a number. The data to be valid should be a multiple of the keyword value (i.e. the result of division of the data on the value should be integer).</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "number", multipleOf: 5}</code></p> <p><em>valid</em>: <code>5</code>, <code>10</code></p> <p><em>invalid</em>: <code>1</code>, <code>4</code></p></li></ol> <ol start="2"><li><p><em>schema</em>: <code>{type: "number", multipleOf: 2.5}</code></p> <p><em>valid</em>: <code>2.5</code>, <code>5</code>, <code>7.5</code></p> <p><em>invalid</em>: <code>1</code>, <code>4</code></p></li></ol> <h2 id="keywords-for-strings"><a href="#keywords-for-strings" class="header-anchor">#</a> Keywords for strings</h2> <h3 id="maxlength-minlength"><a href="#maxlength-minlength" class="header-anchor">#</a> <code>maxLength</code> / <code>minLength</code></h3> <div class="custom-block warning"><p class="custom-block-title">Grapheme clusters will count as multiple characters</p> <p>Certain charsets have characters that are made up of multiple Unicode code points. These <a href="https://www.unicode.org/reports/tr29/tr29-17.html#Grapheme_Cluster_Boundaries" target="_blank" rel="noopener noreferrer">grapheme clusters<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> are counted as multiple in length calculations.</p></div> <p>The value of the keywords should be a number. The data to be valid should have length satisfying this rule. Unicode pairs are counted as a single character.</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "string", maxLength: 5}</code></p> <p><em>valid</em>: <code>"abc"</code>, <code>"abcde"</code></p> <p><em>invalid</em>: <code>"abcdef"</code></p></li></ol> <ol start="2"><li><p><em>schema</em>: <code>{type: "string", minLength: 2}</code></p> <p><em>valid</em>: <code>"ab"</code>, <code>"😀😀"</code></p> <p><em>invalid</em>: <code>"a"</code>, <code>"😀"</code></p></li></ol> <h3 id="pattern"><a href="#pattern" class="header-anchor">#</a> <code>pattern</code></h3> <p>The value of the keyword should be a string. The data to be valid should match the regular expression defined by the keyword value.</p> <p>Ajv uses <code>new RegExp(value, "u")</code> to create the regular expression that will be used to test data.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "string", pattern: "[abc]+"}</code></p> <p><em>valid</em>: <code>"a"</code>, <code>"abcd"</code>, <code>"cde"</code></p> <p><em>invalid</em>: <code>"def"</code>, <code>""</code></p> <h3 id="format"><a href="#format" class="header-anchor">#</a> <code>format</code></h3> <p>The value of the keyword should be a string. The data to be valid should match the format with this name.</p> <p>Ajv does not include any formats, they can be added with <a href="https://github.com/ajv-validator/ajv-formats" target="_blank" rel="noopener noreferrer">ajv-formats<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> plugin.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "string", format: "ipv4"}</code></p> <p><em>valid</em>: <code>"192.168.0.1"</code></p> <p><em>invalid</em>: <code>"abc"</code></p> <h2 id="keywords-for-arrays"><a href="#keywords-for-arrays" class="header-anchor">#</a> Keywords for arrays</h2> <h3 id="maxitems-minitems"><a href="#maxitems-minitems" class="header-anchor">#</a> <code>maxItems</code> / <code>minItems</code></h3> <p>The value of the keywords should be a number. The data array to be valid should not have more (less) items than the keyword value.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "array", maxItems: 3}</code></p> <p><em>valid</em>: <code>[]</code>, <code>[1]</code>, <code>["1", 2, "3"]</code></p> <p><em>invalid</em>: <code>[1, 2, 3, 4]</code></p> <h3 id="uniqueitems"><a href="#uniqueitems" class="header-anchor">#</a> <code>uniqueItems</code></h3> <p>The value of the keyword should be a boolean. If the keyword value is <code>true</code>, the data array to be valid should have unique items.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "array", uniqueItems: true}</code></p> <p><em>valid</em>: <code>[]</code>, <code>[1]</code>, <code>["1", 2, "3"]</code></p> <p><em>invalid</em>: <code>[1, 2, 1]</code>, <code>[{a: 1, b: 2}, {b: 2, a: 1}]</code></p> <h3 id="items"><a href="#items" class="header-anchor">#</a> <code>items</code></h3> <h4 id="items-in-draft-04-06-07-and-2019-09"><a href="#items-in-draft-04-06-07-and-2019-09" class="header-anchor">#</a> <code>items</code> in draft-04, -06, -07 and -2019-09</h4> <div class="custom-block warning"><p class="custom-block-title">items keyword changed in JSON Schema draft-2020-12</p> <p>This section describes <code>items</code> keyword in all JSON Schema versions prior to draft-2020-12.</p></div> <p>The value of the keyword should be a schema or an array of schemas.</p> <p>If the keyword value is a schema, then for the data array to be valid each item of the array should be valid according to the schema. In this case the <code>additionalItems</code> keyword is ignored.</p> <p>If the keyword value is an array, then items with indices less than the number of items in the keyword should be valid according to the schemas with the same indices. Whether additional items are valid will depend on <code>additionalItems</code> keyword.</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "array", items: {type: "integer"}}</code></p> <p><em>valid</em>: <code>[1,2,3]</code>, <code>[]</code></p> <p><em>invalid</em>: <code>[1,"abc"]</code></p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1]</code>, <code>[1, "abc"]</code>, <code>[1, "abc", 2]</code>, <code>[]</code></p> <p><em>invalid</em>: <code>["abc", 1]</code>, <code>["abc"]</code></p></li></ol> <p>The schema in example 2 will log warning by default (see <code>strictTuples</code> option), because it defines unconstrained tuple. To define a tuple with exactly 2 elements use <code>minItems</code> and <code>additionalItems</code> keywords (see example 1 in <code>additionalItems</code>).</p> <h4 id="items-in-draft-2020-12"><a href="#items-in-draft-2020-12" class="header-anchor">#</a> <code>items</code> in draft-2020-12 <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW</span></h4> <div class="custom-block warning"><p class="custom-block-title">items keyword changed in JSON Schema draft-2020-12</p> <p>This section describes <code>items</code> keyword in JSON draft-2020-12.</p></div> <p>The value of the keyword must be a schema.</p> <p>For the data array to be valid:</p> <ul><li>if <a href="#prefixItems">prefixItems</a> keyword is not used in the schema, then each item of the array must be valid according to the schema in <code>items</code>.</li> <li>if <a href="#prefixItems">prefixItems</a> keyword is used in the schema, then each item with the index starting from the size of <code>prefixItems</code> schema must be valid according to the schema in <code>items</code></li></ul> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>: <code>{type: "array", items: {type: "integer"}}</code></p> <p><em>valid</em>: <code>[1,2,3]</code>, <code>[]</code></p> <p><em>invalid</em>: <code>[1,"abc"]</code></p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">prefixItems</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">minItems</span><span class="token operator">:</span> <span class="token number">2</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1, 2]</code></p> <p><em>invalid</em>: <code>[]</code>, <code>[1]</code>, <code>[1, 2, 3]</code>, <code>[1, "abc"]</code> (any wrong number of items or wrong type)</p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">prefixItems</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[]</code>, <code>[1, 2]</code>, <code>[1, 2, "abc"]</code></p> <p><em>invalid</em>: <code>["abc"]</code>, <code>[1, 2, 3]</code></p> <div class="language- extra-class"><pre class="language-text"><code>
_valid_: `[1]`, `[1, "abc"]`, `[1, "abc", 2]`, `[]`
_invalid_: `["abc", 1]`, `["abc"]`
</code></pre></div></li></ol> <p>The schema in example 3 will log warning by default (see <code>strictTuples</code> option), because it defines unconstrained tuple. To define a tuple with exactly 2 elements use <code>minItems</code> and <code>items</code> keywords (see example 2).</p> <h3 id="prefixitems"><a href="#prefixitems" class="header-anchor">#</a> <code>prefixItems</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2020-12</span></h3> <p>The value of the keyword must be an array of schemas.</p> <p>For the data array to be valid, the items with indices less than the number of schemas in this keyword must be valid according to the schemas with the same indices. Whether additional items are valid will depend on <code>items</code> keyword.</p> <p><strong>Examples</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">prefixItems</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1]</code>, <code>[1, "abc"]</code>, <code>[1, "abc", 2]</code>, <code>[]</code></p> <p><em>invalid</em>: <code>["abc", 1]</code>, <code>["abc"]</code></p> <p>The schema in example will log warning by default (see <code>strictTuples</code> option), because it defines unconstrained tuple. To define a tuple with exactly 2 elements use <a href="#minitems">minItems</a> and <a href="#items-in-draft-2020-12">items</a> keywords (see example 2 in <a href="#items-in-draft-2020-12">items</a>).</p> <h3 id="additionalitems"><a href="#additionalitems" class="header-anchor">#</a> <code>additionalItems</code></h3> <div class="custom-block warning"><p class="custom-block-title">additionalItems is not supported in JSON Schema draft-2020-12</p> <p>To create and equivalent schema in draft-2020-12 use keywords <a href="#prefixItems">prefixItems</a> and the new <a href="#items-in-draft-2020-12">items</a> keyword</p></div> <p>The value of the keyword should be a boolean or an object.</p> <p><code>additionalItems</code> keyword is ignored if <code>items</code> keyword is not present or is an object. By default Ajv will throw exception in this case - see <a href="/strict-mode.html">Strict mode</a></p> <p><code>additionalItems</code> keyword is ignored if <code>items</code> keyword has more elements than data array.</p> <p>If the data array has more elements than the <code>items</code> keyword value then the result of the validation depends on the value of <code>additionalItems</code> keyword:</p> <ul><li><code>false</code>: data is invalid</li> <li><code>true</code>: data is valid</li> <li>an object: data is valid if all additional items (i.e. items with indices greater or equal than "items" keyword value length) are valid according to the schema in "additionalItems" keyword.</li></ul> <p>The schemas in examples 2-3 log warning by default, use option <code>strictTuples: false</code> to allow)</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">minItems</span><span class="token operator">:</span> <span class="token number">2</span>
<span class="token literal-property property">additionalItems</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1, 2]</code></p> <p><em>invalid</em>: <code>[]</code>, <code>[1]</code>, <code>[1, 2, 3]</code>, <code>[1, "abc"]</code> (any wrong number of items or wrong type)</p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">additionalItems</span><span class="token operator">:</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[]</code>, <code>[1, 2]</code>, <code>[1, 2, 3]</code>, <code>[1, 2, "abc"]</code></p> <p><em>invalid</em>: <code>["abc"]</code>, <code>[1, "abc", 3]</code></p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">additionalItems</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[]</code>, <code>[1, 2]</code>, <code>[1, 2, "abc"]</code></p> <p><em>invalid</em>: <code>["abc"]</code>, <code>[1, 2, 3]</code></p></li></ol> <h3 id="contains"><a href="#contains" class="header-anchor">#</a> <code>contains</code></h3> <p>The value of the keyword is a JSON Schema. The array is valid if it contains at least one item that is valid according to this schema.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "array", contains: {type: "integer"}}</code></p> <p><em>valid</em>: <code>[1]</code>, <code>[1, "foo"]</code>, any array with at least one integer</p> <p><em>invalid</em>: <code>[]</code>, <code>["foo", "bar"]</code>, any array without integers</p> <h3 id="maxcontains-mincontains"><a href="#maxcontains-mincontains" class="header-anchor">#</a> <code>maxContains</code> / <code>minContains</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></h3> <p>The value of these keywords should be an integer.</p> <p>Without <code>contains</code> keyword they are ignored (logs error or throws exception in ajv <a href="/strict-mode.html">strict mode</a>).</p> <p>The array is valid if it contains at least <code>minContains</code> items and no more than <code>maxContains</code> items that are valid against the schema in <code>contains</code> keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">contains</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">minContains</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
<span class="token literal-property property">maxContains</span><span class="token operator">:</span> <span class="token number">3</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1, 2]</code>, <code>[1, 2, 3, "foo"]</code>, any array with 2 or 3 integers</p> <p><em>invalid</em>: <code>[]</code>, <code>[1, "foo"]</code>, <code>[1, 2, 3, 4]</code>, any array with fewer than 2 or more than 3 integers</p> <h3 id="unevaluateditems"><a href="#unevaluateditems" class="header-anchor">#</a> <code>unevaluatedItems</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></h3> <p>The value of this keyword is a JSON Schema (can be a boolean).</p> <p>This schema will be applied to all array items that were not evaluated by other keywords for items (<code>items</code>, <code>additionalItems</code> and <code>contains</code>) in the current schema and all sub-schemas that were valid for this data instance. It includes:</p> <ul><li>all subschemas schemas in <code>allOf</code> and <code>$ref</code> keywords</li> <li>valid sub-schemas in <code>oneOf</code> and <code>anyOf</code> keywords</li> <li>sub-schema in <code>if</code> keyword</li> <li>sub-schemas in <code>then</code> or <code>else</code> keywords that were applied based on the validation result by <code>if</code> keyword.</li></ul> <p>The only scenario when this keyword would be applied to some items is when <code>items</code> keyword value is an array of schemas and <code>additionalItems</code> was not present (or did not apply, in case it was present in some invalid subschema).</p> <p>Some user-defined keywords can also make items "evaluated".</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"array"</span><span class="token punctuation">,</span>
<span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">unevaluatedItems</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token literal-property property">anyOf</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span><span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">items</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"boolean"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>[1, 2, 3]</code>, <code>[1, 2, true]</code></p> <p><em>invalid</em>:</p> <ul><li><code>[1, 2]</code> - the third item is not present</li> <li><code>[1, 2, "3"]</code> - the third item is "unevaluated"</li></ul> <p>See <a href="https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2019-09/unevaluatedItems.json" target="_blank" rel="noopener noreferrer">tests<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> for <code>unevaluatedItems</code> keyword for other examples.</p> <h2 id="keywords-for-objects"><a href="#keywords-for-objects" class="header-anchor">#</a> Keywords for objects</h2> <h3 id="maxproperties-minproperties"><a href="#maxproperties-minproperties" class="header-anchor">#</a> <code>maxProperties</code> / <code>minProperties</code></h3> <p>The value of the keywords should be a number. The data object to be valid should have not more (less) properties than the keyword value.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "object", maxProperties: 2 }</code></p> <p><em>valid</em>: <code>{}</code>, <code>{a: 1}</code>, <code>{a: "1", b: 2}</code></p> <p><em>invalid</em>: <code>{a: 1, b: 2, c: 3}</code></p> <h3 id="required"><a href="#required" class="header-anchor">#</a> <code>required</code></h3> <p>The value of the keyword should be an array of unique strings. The data object to be valid should contain all properties with names equal to the elements in the keyword value.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "object", required: ["a", "b"]}</code></p> <p><em>valid</em>: <code>{a: 1, b: 2}</code>, <code>{a: 1, b: 2, c: 3}</code></p> <p><em>invalid</em>: <code>{}</code>, <code>{a: 1}</code>, <code>{c: 3, d: 4}</code></p> <h3 id="properties"><a href="#properties" class="header-anchor">#</a> <code>properties</code></h3> <p>The value of the keyword should be a map with keys equal to data object properties. Each value in the map should be a JSON Schema. For data object to be valid the corresponding values in data object properties should be valid according to these schemas.</p> <div class="custom-block warning"><p class="custom-block-title">Properties are not required</p> <p><code>properties</code> keyword does not require that the properties mentioned in it are present in the object (see examples).</p></div> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">,</span>
<span class="token literal-property property">minimum</span><span class="token operator">:</span> <span class="token number">2</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: "a"}</code>, <code>{foo: "a", bar: 2}</code></p> <p><em>invalid</em>: <code>{foo: 1}</code>, <code>{foo: "a", bar: 1}</code></p> <h3 id="patternproperties"><a href="#patternproperties" class="header-anchor">#</a> <code>patternProperties</code></h3> <p>The value of this keyword should be a map where keys should be regular expressions and the values should be JSON Schemas. For data object to be valid the values in data object properties that match regular expression(s) should be valid according to the corresponding schema(s).</p> <p>When the value in data object property matches multiple regular expressions it should be valid according to all the schemas for all matched regular expressions.</p> <div class="custom-block warning"><p class="custom-block-title">Unexpected validation results</p> <ol><li><code>patternProperties</code> keyword does not require that properties matching patterns are present in the object (see examples).</li> <li>By default, Ajv does not allow schemas where patterns in <code>patternProperties</code> match any property name in <code>properties</code> keyword - that leads to unexpected validation results. It can be allowed with option <code>allowMatchingProperties</code>. See <a href="/strict-mode.html">Strict mode</a></li></ol></div> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">patternProperties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"^fo.*$"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string-property property">"^ba.*$"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: "a"}</code>, <code>{foo: "a", bar: 1}</code></p> <p><em>invalid</em>: <code>{foo: 1}</code>, <code>{foo: "a", bar: "b"}</code></p> <h3 id="additionalproperties"><a href="#additionalproperties" class="header-anchor">#</a> <code>additionalProperties</code></h3> <p>The value of the keyword should be either a boolean or a JSON Schema.</p> <p>If the value is <code>true</code> the keyword is ignored.</p> <p>If the value is <code>false</code> the data object to be valid should not have "additional properties" (i.e. properties other than those used in "properties" keyword and those that match patterns in "patternProperties" keyword).</p> <p>If the value is a schema for the data object to be valid the values in all "additional properties" should be valid according to this schema.</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">patternProperties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"^.*r$"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">additionalProperties</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code></p> <p><em>invalid</em>: <code>{a: 3}</code>, <code>{foo: 1, baz: 3}</code></p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">patternProperties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"^.*r$"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">additionalProperties</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{a: "b"}</code>, <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code>, <code>{foo: 1, bar: 2, a: "b"}</code></p> <p><em>invalid</em>: <code>{a: 3}</code>, <code>{foo: 1, baz: 3}</code></p></li> <li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">additionalProperties</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token literal-property property">anyOf</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">baz</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: 1}</code></p> <p><em>invalid</em>: <code>{bar: 2}</code>, <code>{baz: 3}</code>, <code>{foo: 1, bar: 2}</code>, etc.</p></li></ol> <h3 id="dependencies"><a href="#dependencies" class="header-anchor">#</a> <code>dependencies</code> <span class="badge warning" style="vertical-align:top;" data-v-6eb0829b>deprecated in draft 2019-09</span></h3> <p>This keyword is deprecated. The same functionality is available with keywords <code>dependentRequired</code> and <code>dependentSchemas</code>.</p> <p>The value of the keyword is a map with keys equal to data object properties. Each value in the map should be either an array of unique property names ("property dependency" - see <a href="#%60dependentrequired%60"><code>dependentRequired</code></a> keyword) or a JSON Schema ("schema dependency" - see <a href="#%60dependentschemas%60"><code>dependentSchemas</code></a> keyword).</p> <p>For property dependency, if the data object contains a property that is a key in the keyword value, then to be valid the data object should also contain all properties from the array of properties.</p> <p>For schema dependency, if the data object contains a property that is a key in the keyword value, then to be valid the data object itself (NOT the property value) should be valid according to the schema.</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema (property dependency)</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">dependencies</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"bar"</span><span class="token punctuation">,</span> <span class="token string">"baz"</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{foo: 1, bar: 2, baz: 3}</code>, <code>{}</code>, <code>{a: 1}</code></p> <p><em>invalid</em>: <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code>, <code>{foo: 1, baz: 3}</code></p></li> <li><p><em>schema (schema dependency)</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">dependencies</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code>, <code>{a: 1}</code></p> <p><em>invalid</em>: <code>{foo: 1, bar: "a"}</code></p></li></ol> <h3 id="dependentrequired"><a href="#dependentrequired" class="header-anchor">#</a> <code>dependentRequired</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></h3> <p>The value of this keyword should be a map with keys equal to data object properties. Each value in the map should be an array of unique property names.</p> <p>If the data object contains a property that is a key in the keyword value, then to be valid the data object should also contain all properties from the corresponding array of properties in this keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">dependentRequired</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"bar"</span><span class="token punctuation">,</span> <span class="token string">"baz"</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{foo: 1, bar: 2, baz: 3}</code>, <code>{}</code>, <code>{a: 1}</code></p> <p><em>invalid</em>: <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code>, <code>{foo: 1, baz: 3}</code></p> <h3 id="dependentschemas"><a href="#dependentschemas" class="header-anchor">#</a> <code>dependentSchemas</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></h3> <p>The value of the keyword should be a map with keys equal to data object properties. Each value in the map should be a JSON Schema.</p> <p>If the data object contains a property that is a key in the keyword value, then to be valid the data object itself (NOT the property value) should be valid according to the corresponding schema in this keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">dependentSchemas</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{}</code>, <code>{foo: 1}</code>, <code>{foo: 1, bar: 2}</code>, <code>{a: 1}</code></p> <p><em>invalid</em>: <code>{foo: 1, bar: "a"}</code></p> <h3 id="propertynames"><a href="#propertynames" class="header-anchor">#</a> <code>propertyNames</code></h3> <p>The value of this keyword is a JSON Schema.</p> <p>For data object to be valid each property name in this object should be valid according to this schema.</p> <p><strong>Example</strong></p> <p><em>schema</em> (requires <code>email</code> format from <a href="https://github.com/ajv-validator/ajv-formats" target="_blank" rel="noopener noreferrer">ajv-formats<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>):</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">propertyNames</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">format</span><span class="token operator">:</span> <span class="token string">"email"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{"foo@bar.com": "any", "bar@bar.com": "any"}</code></p> <p><em>invalid</em>: <code>{foo: "any value"}</code></p> <h3 id="unevaluatedproperties"><a href="#unevaluatedproperties" class="header-anchor">#</a> <code>unevaluatedProperties</code> <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: draft 2019-09</span></h3> <p>The value of this keyword is a JSON Schema (can be a boolean).</p> <p>This schema will be applied to all properties that were not evaluated by other keywords for properties (<code>properties</code>, <code>patternProperties</code> and <code>additionalProperties</code>) in the current schema and all sub-schemas that were valid for this data instance. It includes:</p> <ul><li>all subschemas schemas in <code>allOf</code> and <code>$ref</code> keywords</li> <li>valid sub-schemas in <code>oneOf</code> and <code>anyOf</code> keywords</li> <li>sub-schema in <code>if</code> keyword</li> <li>sub-schemas in <code>then</code> or <code>else</code> keywords that were applied based on the validation result by <code>if</code> keyword.</li></ul> <p>Some user-defined keywords can also make properties "evaluated".</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"foo"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">unevaluatedProperties</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token literal-property property">anyOf</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"bar"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"baz"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">baz</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{foo: 1, bar: 2}</code>, <code>{foo: 1, baz: 2}</code>, <code>{foo: 1, bar: 2, baz: 3}</code></p> <p><em>invalid</em>:</p> <ul><li><code>{foo: 1}</code> - neither <code>bar</code> nor <code>baz</code> are present</li> <li><code>{foo: 1, bar: 2, boo: 3}</code> - <code>boo</code> is unevaluated</li> <li><code>{foo: 1, bar: 2, baz: "3"}</code> - not valid against the 2nd subschema, so <code>baz</code> is "unevaluated".</li></ul> <p>See <a href="https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2019-09/unevaluatedProperties.json" target="_blank" rel="noopener noreferrer">tests<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> for <code>unevaluatedProperties</code> keyword for other examples.</p> <h3 id="discriminator"><a href="#discriminator" class="header-anchor">#</a> discriminator <span class="badge tip" style="vertical-align:top;" data-v-6eb0829b>NEW: OpenAPI</span></h3> <p>Ajv has a limited support for <code>discriminator</code> keyword: to optimize validation, error handling, and <a href="/guide/modifying-data.html">modifying data</a> with <a href="#oneof">oneOf</a> keyword.</p> <p>Its value should be an object with a property <code>propertyName</code> - the name of the property used to discriminate between union members.</p> <p>When using discriminator keyword only one subschema in <code>oneOf</code> will be used, determined by the value of discriminator property.</p> <div class="custom-block warning"><p class="custom-block-title">Use option discriminator</p> <p>To use <code>discriminator</code> keyword you have to use option <code>discriminator: true</code> with Ajv constructor - it is not enabled by default.</p></div> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">discriminator</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">propertyName</span><span class="token operator">:</span> <span class="token string">"foo"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"foo"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">oneOf</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token keyword">const</span><span class="token operator">:</span> <span class="token string">"x"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"a"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token keyword">enum</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"y"</span><span class="token punctuation">,</span> <span class="token string">"z"</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"b"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{foo: "x", a: "any"}</code>, <code>{foo: "y", b: "any"}</code>, <code>{foo: "z", b: "any"}</code></p> <p><em>invalid</em>:</p> <ul><li><code>{}</code>, <code>{foo: 1}</code> - discriminator tag must be string</li> <li><code>{foo: "bar"}</code> - discriminator tag value must be in oneOf subschema</li> <li><code>{foo: "x", b: "b"}</code>, <code>{foo: "y", a: "a"}</code> - invalid object</li></ul> <p>From the perspective of validation result <code>discriminator</code> is defined as no-op (that is, removing discriminator will not change the validity of the data), but errors reported in case of invalid data will be different.</p> <p>There are following requirements and limitations of using <code>discriminator</code> keyword:</p> <ul><li><code>mapping</code> in discriminator object is not supported.</li> <li><a href="#oneof">oneOf</a> keyword must be present in the same schema.</li> <li>discriminator property should be <a href="#required">required</a> either on the top level, as in the example, or in all <code>oneOf</code> subschemas.</li> <li>each <code>oneOf</code> subschema must have <a href="#properties">properties</a> keyword with discriminator property. The subschemas should be either inlined or included as direct references (only <code>$ref</code> keyword without any extra keywords is allowed).</li> <li>schema for discriminator property in each <code>oneOf</code> subschema must be <a href="#const">const</a> or <a href="#enum">enum</a>, with unique values across all subschemas.</li></ul> <p>Not meeting any of these requirements would fail schema compilation.</p> <h2 id="keywords-for-all-types"><a href="#keywords-for-all-types" class="header-anchor">#</a> Keywords for all types</h2> <h3 id="enum"><a href="#enum" class="header-anchor">#</a> <code>enum</code></h3> <p>The value of the keyword should be an array of unique items of any types. The data is valid if it is deeply equal to one of items in the array.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{enum: [2, "foo", {foo: "bar" }, [1, 2, 3]]}</code></p> <p><em>valid</em>: <code>2</code>, <code>"foo"</code>, <code>{foo: "bar"}</code>, <code>[1, 2, 3]</code></p> <p><em>invalid</em>: <code>1</code>, <code>"bar"</code>, <code>{foo: "baz"}</code>, <code>[1, 2, 3, 4]</code>, any value not in enum</p> <h3 id="const"><a href="#const" class="header-anchor">#</a> <code>const</code></h3> <p>The value of this keyword can be anything. The data is valid if it is deeply equal to the value of the keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{const: "foo"}</code></p> <p><em>valid</em>: <code>"foo"</code></p> <p><em>invalid</em>: any other value</p> <p>The same can be achieved with <code>enum</code> keyword using the array with one item. But <code>const</code> keyword is more than just a syntax sugar for <code>enum</code>. In combination with the <a href="/guide/combining-schemas.html#data-reference">$data reference</a> it allows to define equality relations between different parts of the data. This cannot be achieved with <code>enum</code> keyword even with <code>$data</code> reference because <code>$data</code> cannot be used in place of one item - it can only be used in place of the whole array in <code>enum</code> keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token keyword">const</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$data</span><span class="token operator">:</span> <span class="token string">"1/foo"</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>{foo: 1, bar: 1}</code>, <code>{}</code></p> <p><em>invalid</em>: <code>{foo: 1}</code>, <code>{bar: 1}</code>, <code>{foo: 1, bar: 2}</code></p> <h2 id="compound-keywords"><a href="#compound-keywords" class="header-anchor">#</a> Compound keywords</h2> <h3 id="not"><a href="#not" class="header-anchor">#</a> <code>not</code></h3> <p>The value of the keyword should be a JSON Schema. The data is valid if it is invalid according to this schema.</p> <p><strong>Example</strong></p> <p><em>schema</em>: <code>{type: "number", not: {minimum: 3}}</code></p> <p><em>valid</em>: <code>1</code>, <code>2</code></p> <p><em>invalid</em>: <code>3</code>, <code>4</code></p> <h3 id="oneof"><a href="#oneof" class="header-anchor">#</a> <code>oneOf</code></h3> <p>The value of the keyword should be an array of JSON Schemas. The data is valid if it matches exactly one JSON Schema from this array. Validators have to validate data against all schemas to establish validity according to this keyword.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">,</span>
<span class="token literal-property property">oneOf</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">maximum</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>1.5</code>, <code>2.5</code>, <code>4</code>, <code>5</code></p> <p><em>invalid</em>: <code>2</code>, <code>3</code>, <code>4.5</code>, <code>5.5</code></p> <h3 id="anyof"><a href="#anyof" class="header-anchor">#</a> <code>anyOf</code></h3> <p>The value of the keyword should be an array of JSON Schemas. The data is valid if it is valid according to one or more JSON Schemas in this array. Validators only need to validate data against schemas in order until the first schema matches (or until all schemas have been tried). For this reason validating against this keyword is faster than against "oneOf" keyword in most cases.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">,</span>
<span class="token literal-property property">anyOf</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">maximum</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>1.5</code>, <code>2</code>, <code>2.5</code>, <code>3</code>, <code>4</code>, <code>5</code></p> <p><em>invalid</em>: <code>4.5</code>, <code>5.5</code></p> <h3 id="allof"><a href="#allof" class="header-anchor">#</a> <code>allOf</code></h3> <p>The value of the keyword should be an array of JSON Schemas. The data is valid if it is valid according to all JSON Schemas in this array.</p> <p><strong>Example</strong></p> <p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"number"</span><span class="token punctuation">,</span>
<span class="token literal-property property">allOf</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">maximum</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>2</code>, <code>3</code></p> <p><em>invalid</em>: <code>1.5</code>, <code>2.5</code>, <code>4</code>, <code>4.5</code>, <code>5</code>, <code>5.5</code></p> <h3 id="if-then-else"><a href="#if-then-else" class="header-anchor">#</a> <code>if</code>/<code>then</code>/<code>else</code></h3> <p>These keywords allow to implement conditional validation. Their values should be valid JSON Schemas (object or boolean).</p> <p>If <code>if</code> keyword is absent, the validation succeeds.</p> <p>If the data is valid against the sub-schema in <code>if</code> keyword, then the validation result is equal to the result of data validation against the sub-schema in <code>then</code> keyword (if <code>then</code> is absent, the validation succeeds).</p> <p>If the data is invalid against the sub-schema in <code>if</code> keyword, then the validation result is equal to the result of data validation against the sub-schema in <code>else</code> keyword (if <code>else</code> is absent, the validation succeeds).</p> <p><strong>Examples</strong></p> <ol><li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
<span class="token keyword">if</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">minimum</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">then</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"bar"</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token keyword">else</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"baz"</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>:</p> <ul><li><code>{foo: 10, bar: true }</code></li> <li><code>{}</code></li> <li><code>{foo: 1, baz: true }</code></li></ul> <p><em>invalid</em>:</p> <ul><li><code>{foo: 10}</code> (<code>bar</code> is required)</li> <li><code>{foo: 10, baz: true }</code> (<code>bar</code> is required)</li> <li><code>{foo: 1}</code> (<code>baz</code> is required)</li></ul></li></ol> <ol start="2"><li><p><em>schema</em>:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"integer"</span><span class="token punctuation">,</span>
<span class="token literal-property property">minimum</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token literal-property property">maximum</span><span class="token operator">:</span> <span class="token number">1000</span><span class="token punctuation">,</span>
<span class="token keyword">if</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">minimum</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">then</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">multipleOf</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token keyword">else</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">minimum</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">then</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">multipleOf</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><em>valid</em>: <code>1</code>, <code>5</code>, <code>10</code>, <code>20</code>, <code>50</code>, <code>100</code>, <code>200</code>, <code>500</code>, <code>1000</code></p> <p><em>invalid</em>:</p> <ul><li><code>-1</code>, <code>0</code> (<1)</li> <li><code>2000</code> (>1000)</li> <li><code>11</code>, <code>57</code>, <code>123</code> (any integer with more than one non-zero digit)</li> <li>non-integers</li></ul></li></ol> <h2 id="metadata-keywords"><a href="#metadata-keywords" class="header-anchor">#</a> Metadata keywords</h2> <p>JSON Schema specification defines several metadata keywords that describe the schema itself but do not perform any validation.</p> <ul><li><code>title</code> and <code>description</code>: information about the data represented by that schema</li> <li><code>$comment</code>: information for developers. With option <code>$comment</code> Ajv logs or passes the comment string to the user-supplied function. See <a href="/api.html#options">Options</a>.</li> <li><code>default</code>: a default value of the data instance, see <a href="/guide/modifying-data.html#assigning-defaults">Assigning defaults</a>.</li> <li><code>examples</code>: an array of data instances. Ajv does not check the validity of these instances against the schema.</li> <li><code>readOnly</code> and <code>writeOnly</code>: marks data-instance as read-only or write-only in relation to the source of the data (database, api, etc.).</li> <li><code>contentEncoding</code>: <a href="https://tools.ietf.org/html/rfc2045#section-6.1" target="_blank" rel="noopener noreferrer">RFC 2045<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>, e.g., "base64".</li> <li><code>contentMediaType</code>: <a href="https://datatracker.ietf.org/doc/rfc2046/" target="_blank" rel="noopener noreferrer">RFC 2046<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>, e.g., "image/png".</li></ul> <div class="custom-block warning"><p class="custom-block-title">Ignored keywords</p> <p>Ajv does not implement validation of the keywords <code>examples</code>, <code>contentEncoding</code> and <code>contentMediaType</code> but it reserves them. If you want to create a plugin that implements any of them, it should remove these keywords from the instance.</p></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/ajv-validator/ajv/edit/master/docs/json-schema.md" target="_blank" rel="noopener noreferrer">Edit this page</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/options.html" class="prev">
Ajv options
</a></span> <span class="next"><a href="/json-type-definition.html">
JSON Type Definition
</a>
→
</span></p></div> </main></div><div class="global-ui"></div></div>
<script src="/assets/js/app.bb2a0f22.js" defer></script><script src="/assets/js/2.53bb977e.js" defer></script><script src="/assets/js/1.6a0e5398.js" defer></script><script src="/assets/js/31.0d5db059.js" defer></script><script src="/assets/js/62.17eb9b44.js" defer></script><script src="/assets/js/4.eb19df75.js" defer></script><script src="/assets/js/6.af23b107.js" defer></script>
</body>
</html>