-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathjsonschema-core.xml
4396 lines (4226 loc) · 229 KB
/
jsonschema-core.xml
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc [
<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC3986 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3986.xml">
<!ENTITY RFC3987 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3987.xml">
<!ENTITY RFC6596 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6596.xml">
<!ENTITY RFC6839 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6839.xml">
<!ENTITY RFC6901 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6901.xml">
<!ENTITY RFC7049 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.7049.xml">
<!ENTITY RFC8259 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.8259.xml">
<!ENTITY RFC7231 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.7231.xml">
<!ENTITY RFC8288 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.8288.xml">
<!ENTITY ldp SYSTEM "https://xml2rfc.tools.ietf.org/public/rfc/bibxml4/reference.W3C.REC-ldp-20150226.xml">
<!ENTITY fragid-best-practices SYSTEM "https://xml2rfc.tools.ietf.org/public/rfc/bibxml4/reference.W3C.WD-fragid-best-practices-20121025.xml">
<!ENTITY xptr-framework SYSTEM "https://bib.ietf.org/public/rfc/bibxml4/reference.W3C.REC-xptr-framework-20030325.xml">
]>
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc strict="no"?>
<?rfc rfcedstyle="yes"?>
<?rfc comments="yes"?>
<?rfc inline="yes" ?>
<rfc category="info" docName="draft-bhutton-json-schema-01" ipr="trust200902" submissionType="IETF">
<front>
<title abbrev="JSON Schema">JSON Schema: A Media Type for Describing JSON Documents</title>
<author fullname="Austin Wright" initials="A" surname="Wright" role="editor">
<address>
<email>aaa@bzfx.net</email>
</address>
</author>
<author fullname="Henry Andrews" initials="H" surname="Andrews" role="editor">
<address>
<email>andrews_henry@yahoo.com</email>
</address>
</author>
<author fullname="Ben Hutton" initials="B" surname="Hutton" role="editor">
<organization>Postman</organization>
<address>
<email>ben@jsonschema.dev</email>
<uri>https://jsonschema.dev</uri>
</address>
</author>
<author fullname="Greg Dennis" initials="G" surname="Dennis">
<address>
<email>gregsdennis@yahoo.com</email>
<uri>https://github.com/gregsdennis</uri>
</address>
</author>
<date year="2023"/>
<workgroup>Internet Engineering Task Force</workgroup>
<keyword>JSON</keyword>
<keyword>Schema</keyword>
<keyword>Hyper Schema</keyword>
<keyword>Hypermedia</keyword>
<abstract>
<t>
JSON Schema defines the media type "application/schema+json", a JSON-based format
for describing the structure of JSON data.
JSON Schema asserts what a JSON document must look like,
ways to extract information from it,
and how to interact with it.
The "application/schema-instance+json" media type provides additional
feature-rich integration with "application/schema+json" beyond what can be offered
for "application/json" documents.
</t>
</abstract>
<note title="Note to Readers">
<t>
The issues list for this draft can be found at
<eref target="https://github.com/json-schema-org/json-schema-spec/issues"/>.
</t>
<t>
For additional information, see <eref target="https://json-schema.org/"/>.
</t>
<t>
To provide feedback, use this issue tracker, the communication methods listed on the
homepage, or email the document editors.
</t>
</note>
</front>
<middle>
<section title="Introduction">
<t>
JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema
is intended to define validation, documentation, hyperlink navigation, and interaction
control of JSON data.
</t>
<t>
This specification defines JSON Schema core terminology and mechanisms, including
pointing to another JSON Schema by reference,
dereferencing a JSON Schema reference,
specifying the dialect being used,
specifying a dialect's vocabulary requirements,
and defining the expected output.
</t>
<t>
Other specifications define the vocabularies that perform assertions about validation,
linking, annotation, navigation, and interaction.
</t>
</section>
<section title="Conventions and Terminology">
<t>
<!-- The text in this section has been copied from the official boilerplate,
and should not be modified.-->
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
interpreted as described in <xref target="RFC2119">RFC 2119</xref>.
</t>
<t>
The terms "JSON", "JSON text", "JSON value", "member", "element", "object", "array",
"number", "string", "boolean", "true", "false", and "null" in this document are to
be interpreted as defined in <xref target="RFC8259">RFC 8259</xref>.
</t>
</section>
<section title="Overview">
<t>
This document proposes a new media type "application/schema+json" to identify a JSON
Schema for describing JSON data.
It also proposes a further optional media type, "application/schema-instance+json",
to provide additional integration features.
JSON Schemas are themselves JSON documents.
This, and related specifications, define keywords allowing authors to describe JSON
data in several ways.
</t>
<t>
JSON Schema uses keywords to assert constraints on JSON instances or annotate those
instances with additional information. Additional keywords are used to apply
assertions and annotations to more complex JSON data structures, or based on
some sort of condition.
</t>
<t>
To facilitate re-use, keywords can be organized into vocabularies. A vocabulary
consists of a list of keywords, together with their syntax and semantics.
A dialect is defined as a set of vocabularies and their required support
identified in a meta-schema.
</t>
<t>
JSON Schema can be extended either by defining additional vocabularies,
or less formally by defining additional keywords outside of any vocabulary.
Unrecognized individual keywords simply have their values collected as annotations,
while the behavior with respect to an unrecognized vocabulary can be controlled
when declaring which vocabularies are in use.
</t>
<t>
This document defines a core vocabulary that MUST be supported by any
implementation, and cannot be disabled. Its keywords are each prefixed
with a "$" character to emphasize their required nature. This vocabulary
is essential to the functioning of the "application/schema+json" media
type, and is used to bootstrap the loading of other vocabularies.
</t>
<t>
Additionally, this document defines a RECOMMENDED vocabulary of keywords
for applying subschemas conditionally, and for applying subschemas to
the contents of objects and arrays. Either this vocabulary or one very
much like it is required to write schemas for non-trivial JSON instances,
whether those schemas are intended for assertion validation, annotation,
or both. While not part of the required core vocabulary, for maximum
interoperability this additional vocabulary is included in this document
and its use is strongly encouraged.
</t>
<t>
Further vocabularies for purposes such as structural validation or
hypermedia annotation are defined in other documents. These other
documents each define a dialect collecting the standard sets of
vocabularies needed to write schemas for that document's purpose.
</t>
</section>
<section title="Definitions">
<section title="JSON Document">
<t>
A JSON document is an information resource (series of octets) described by the
application/json media type.
</t>
<t>
In JSON Schema, the terms "JSON document", "JSON text", and "JSON value" are
interchangeable because of the data model it defines in <xref target="data-model" />.
</t>
<t>
JSON Schema is only defined over JSON documents. However, any document or memory
structure that can be parsed into or processed according to the JSON Schema data
model can be interpreted against a JSON Schema, including media types like
<xref target="RFC7049">CBOR</xref>.
</t>
</section>
<section title="Instance">
<t>
A JSON document to which a schema is applied is known as an "instance".
</t>
<t>
JSON Schema is defined over "application/json" or compatible documents,
including media types with the "+json" structured syntax suffix.
</t>
<t>
Among these, this specification defines the "application/schema-instance+json"
media type which defines handling for fragments in the IRI.
</t>
<section title="Instance Data Model" anchor="data-model">
<t>
JSON Schema interprets documents according to a data model. A JSON value
interpreted according to this data model is called an "instance".
</t>
<t>
An instance has one of six primitive types, and a range of possible values
depending on the type:
<dl>
<dt>null</dt><dd>A JSON "null" value</dd>
<dt>boolean</dt><dd>A "true" or "false" value, from the JSON "true" or "false" value</dd>
<dt>object</dt><dd>An unordered set of properties mapping a string to an instance, from the JSON "object" value</dd>
<dt>array</dt><dd>An ordered list of instances, from the JSON "array" value</dd>
<dt>number</dt><dd>An arbitrary-precision, base-10 decimal number value, from the JSON "number" value</dd>
<dt>string</dt><dd>A string of Unicode code points, from the JSON "string" value</dd>
</dl>
</t>
<t>
Whitespace and formatting concerns, including different lexical
representations of numbers that are equal within the data model, are thus
outside the scope of JSON Schema. JSON Schema
<xref target="vocabulary">vocabularies</xref> that wish
to work with such differences in lexical representations SHOULD define
keywords to precisely interpret formatted strings within the data model
rather than relying on having the original JSON representation Unicode
characters available.
</t>
<t>
Since an object cannot have two properties with the same key, behavior for a
JSON document that tries to define two properties with
the same key in a single object is undefined.
</t>
<t>
Note that JSON Schema vocabularies are free to define their own extended
type system. This should not be confused with the core data model types
defined here. As an example, "integer" is a reasonable type for a
vocabulary to define as a value for a keyword, but the data model
makes no distinction between integers and other numbers.
</t>
</section>
<section title="Instance Equality">
<t>
Two JSON instances are said to be equal if and only if they are of the same type
and have the same value according to the data model. Specifically, this means:
<ul>
<li>both are null; or</li>
<li>both are true; or</li>
<li>both are false; or</li>
<li>both are strings, and are the same codepoint-for-codepoint; or</li>
<li>both are numbers, and have the same mathematical value; or</li>
<li>both are arrays, and have an equal value item-for-item; or</li>
<li>both are objects, and each property in one has exactly one property with
a key equal to the other's, and that other property has an equal
value.</li>
</ul>
</t>
<t>
Implied in this definition is that arrays must be the same length,
objects must have the same number of members,
properties in objects are unordered,
there is no way to define multiple properties with the same key,
and mere formatting differences (indentation, placement of commas, trailing
zeros) are insignificant.
</t>
</section>
<section title="Non-JSON Instances">
<t>
It is possible to use JSON Schema with a superset of the JSON Schema data model,
where an instance may be outside any of the six JSON data types.
</t>
<t>
In this case, annotations still apply; but most validation keywords will not be useful,
as they will always pass or always fail.
</t>
<t>
A custom vocabulary may define support for a superset of the core data model.
The schema itself may only be expressible in this superset;
for example, to make use of the "const" keyword.
</t>
</section>
</section>
<section title="JSON Schema Documents" anchor="schema-document">
<t>
A JSON Schema document, or simply a schema, is a JSON document used to describe
an instance.
A schema can itself be interpreted as an instance, but SHOULD always be given
the media type "application/schema+json" rather than
"application/schema-instance+json". The "application/schema+json" media
type is defined to offer a superset of the
fragment identifier syntax and semantics provided by
"application/schema-instance+json".
</t>
<t>
A JSON Schema MUST be an object or a boolean.
</t>
<section title="JSON Schema Objects and Keywords">
<t>
Object properties that are applied to the instance are called keywords,
or schema keywords. Broadly speaking, keywords fall into one
of five categories:
</t>
<dl>
<dt>identifiers</dt>
<dd>
control schema identification through setting a IRI
for the schema and/or changing how the base IRI is determined
</dd>
<dt>assertions</dt>
<dd>
produce a boolean result when applied to an instance
</dd>
<dt>annotations</dt>
<dd>
attach information to an instance for application use
</dd>
<dt>applicators</dt>
<dd>
apply one or more subschemas to a particular location
in the instance, and combine or modify their results
</dd>
<dt>reserved locations</dt>
<dd>
do not directly affect results, but reserve a place
for a specific purpose to ensure interoperability
</dd>
</dl>
<t>
Keywords may fall into multiple categories, although applicators
SHOULD only produce assertion results based on their subschemas'
results. They should not define additional constraints independent
of their subschemas.
</t>
<t>
Keywords which are properties within the same schema object are referred to as adjacent keywords.
</t>
<t>
Extension keywords, meaning those defined outside of this document
and its companions, are free to define other behaviors as well.
</t>
<t>
A JSON Schema MAY contain properties which are not schema keywords or are not recognized as schema keywords.
The behavior of such keywords is governed by <xref target="unrecognized"></xref>.
</t>
<t>
An empty schema is a JSON Schema with no properties.
</t>
</section>
<section title="Boolean JSON Schemas">
<t>
The boolean schema values "true" and "false" are trivial schemas that
always produce themselves as assertion results, regardless of the
instance value. They never produce annotation results.
</t>
<t>
These boolean schemas exist to clarify schema author intent and
facilitate schema processing optimizations. They behave identically
to the following schema objects (where "not" is part of the
subschema application vocabulary defined in this document).
</t>
<dl>
<dt>true</dt>
<dd>
Always passes validation, as if the empty schema {}
</dd>
<dt>false</dt>
<dd>
Always fails validation, as if the schema { "not": {} }
</dd>
</dl>
<t>
While the empty schema object is unambiguous, there are many
possible equivalents to the "false" schema. Using the boolean
values ensures that the intent is clear to both human readers
and implementations.
</t>
</section>
<section title="Schema Vocabularies">
<t>
A schema vocabulary, or simply a vocabulary, is a set of keywords,
their syntax, and their semantics. A vocabulary is generally organized
around a particular purpose. Different uses of JSON Schema, such
as validation, hypermedia, or user interface generation, will
involve different sets of vocabularies.
</t>
<t>
Vocabularies are the primary unit of re-use in JSON Schema, as schema
authors can indicate what vocabularies are required or optional in
order to process the schema. Since vocabularies are identified by IRIs
in the meta-schema, generic implementations can load extensions to support
previously unknown vocabularies. While keywords can be supported outside
of any vocabulary, there is no analogous mechanism to indicate individual
keyword usage.
</t>
<t>
A schema vocabulary can be defined by anything from an informal description
to a standards proposal, depending on the audience and interoperability
expectations. In particular, in order to facilitate vocabulary use within
non-public organizations, a vocabulary specification need not be published
outside of its scope of use.
</t>
</section>
<section title="Meta-Schemas">
<t>
A schema that itself describes a schema is called a meta-schema.
Meta-schemas are used to validate JSON Schemas and specify which vocabularies
they are using.
</t>
<t>
Typically, a meta-schema will specify a set of vocabularies, and validate
schemas that conform to the syntax of those vocabularies. However, meta-schemas
and vocabularies are separate in order to allow meta-schemas to validate
schema conformance more strictly or more loosely than the vocabularies'
specifications call for. Meta-schemas may also describe and validate
additional keywords that are not part of a formal vocabulary.
</t>
</section>
<section title="Root Schema and Subschemas and Resources" anchor="root">
<t>
A JSON Schema resource is a schema which is
<xref target="RFC6596">canonically</xref> identified by an
<xref target="RFC3987">absolute IRI</xref>. Schema resources MAY
also be identified by IRIs, including IRIs with fragments,
if the resulting secondary resource (as defined by
<xref target="RFC3986">section 3.5 of RFC 3986</xref>) is identical
to the primary resource. This can occur with the empty fragment,
or when one schema resource is embedded in another. Any such IRIs
with fragments are considered to be non-canonical.
</t>
<t>
The root schema is the schema that comprises the entire JSON document
in question. The root schema is always a schema resource, where the
IRI is determined as described in <xref target="initial-base"></xref>.
<cref>
Note that documents that embed schemas in another format will not
have a root schema resource in this sense. Exactly how such usages
fit with the JSON Schema document and resource concepts will be
clarified in a future draft.
</cref>
</t>
<t>
Some keywords take schemas themselves, allowing JSON Schemas to be nested:
</t>
<sourcecode type="json"><![CDATA[
{
"title": "root",
"items": {
"title": "array item"
}
}
]]></sourcecode>
<t>
In this example document, the schema titled "array item" is a subschema,
and the schema titled "root" is the root schema.
</t>
<t>
As with the root schema, a subschema is either an object or a boolean.
</t>
<t>
As discussed in <xref target="id-keyword"></xref>, a JSON Schema document
can contain multiple JSON Schema resources. When used without qualification,
the term "root schema" refers to the document's root schema. In some
cases, resource root schemas are discussed. A resource's root schema
is its top-level schema object, which would also be a document root schema
if the resource were to be extracted to a standalone JSON Schema document.
</t>
<t>
Whether multiple schema resources are embedded or linked with a reference,
they are processed in the same way, with the same available behaviors.
</t>
</section>
</section>
</section>
<section title="Fragment Identifiers" anchor="fragments">
<t>
In accordance with section 3.1 of <xref target="RFC6839">RFC 6839</xref>,
the syntax and semantics of fragment identifiers specified for
any +json media type SHOULD be as specified for "application/json".
(At publication of this document, there is no fragment identification
syntax defined for "application/json".)
</t>
<t>
Additionally, the "application/schema+json" media type supports two
fragment identifier structures: plain names and JSON Pointers.
The "application/schema-instance+json" media type supports one
fragment identifier structure: JSON Pointers.
</t>
<t>
The use of JSON Pointers as IRI fragment identifiers is described in
<xref target="RFC6901">RFC 6901</xref>.
For "application/schema+json", which supports two fragment identifier syntaxes,
fragment identifiers matching the JSON Pointer syntax, including the empty string,
MUST be interpreted as JSON Pointer fragment identifiers.
</t>
<t>
Per the W3C's
<xref target="W3C.WD-fragid-best-practices-20121025">best practices for fragment identifiers</xref>,
plain name fragment identifiers in "application/schema+json" are reserved for referencing
locally named schemas.
</t>
<t>
Plain name fragments MUST start with a letter ([A-Za-z]) or underscore ("_"),
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), and periods ("."). This matches the US-ASCII part of XML's
<xref target="xml-names">NCName production</xref>, which allows for compatibility
with the recommended plain name <xref target="W3C.REC-xptr-framework-20030325">syntax</xref> for
XML-based media types.
</t>
<t>
All fragment identifiers that do
not match the JSON Pointer syntax MUST be interpreted as
plain name fragment identifiers.
</t>
<t>
Defining and referencing a plain name fragment identifier within an
"application/schema+json" document are specified
in the <xref target="anchor">"$anchor" keyword</xref> section.
</t>
</section>
<section title="General Considerations">
<section title="Range of JSON Values">
<t>
An instance may be any valid JSON value as defined by <xref target="RFC8259">JSON</xref>.
JSON Schema imposes no restrictions on type: JSON Schema can describe any JSON
value, including, for example, null.
</t>
</section>
<section title="Programming Language Independence" anchor="language">
<t>
JSON Schema is programming language agnostic, and supports the full range of
values described in the data model.
Be aware, however, that some languages and JSON parsers may not be able to
represent in memory the full range of values describable by JSON.
</t>
</section>
<section title="Mathematical Integers" anchor="integers">
<t>
Some programming languages and parsers use different internal representations
for floating point numbers than they do for integers.
</t>
<t>
For consistency, integer JSON numbers SHOULD NOT be encoded with a fractional
part.
</t>
</section>
<section title="Regular Expressions" anchor="regex">
<t>
Keywords MAY use regular expressions to express constraints, or constrain
the instance value to be a regular expression.
These regular expressions SHOULD be valid according to the regular expression
dialect described in <xref target="ecma262">ECMA-262, section 21.2.1</xref>.
</t>
<t>
Unless otherwise specified by a keyword, regular expressions MUST NOT be
considered to be implicitly anchored at either end. All regular expression
keywords in this specification and its companion documents are un-anchored.
</t>
<t>
Regular expressions SHOULD be built with the "u" flag (or equivalent) to provide
Unicode support, or processed in such a way which provides Unicode support as
defined by ECMA-262.
</t>
<t>
Furthermore, given the high disparity in regular expression constructs support,
schema authors SHOULD limit themselves to the following regular expression
tokens:
<ul>
<li>individual Unicode characters, as defined by the <xref
target="RFC8259">JSON specification</xref>;</li>
<li>simple character classes ([abc]), range character classes ([a-z]);</li>
<li>complemented character classes ([^abc], [^a-z]);</li>
<li>simple quantifiers: "+" (one or more), "*" (zero or more), "?" (zero or
one), and their lazy versions ("+?", "*?", "??");</li>
<li>range quantifiers: "{x}" (exactly x occurrences), "{x,y}" (at least x, at
most y, occurrences), {x,} (x occurrences or more), and their lazy
versions;</li>
<li>the beginning-of-input ("^") and end-of-input ("$") anchors;</li>
<li>simple grouping ("(...)") and alternation ("|").</li>
</ul>
</t>
<t>
Finally, implementations MUST NOT take regular expressions to be
anchored, neither at the beginning nor at the end. This means, for instance,
the pattern "es" matches "expression".
</t>
</section>
<section title="Extending JSON Schema" anchor="extending">
<t>
Additional schema keywords and schema vocabularies MAY be defined
by any entity. Save for explicit agreement, schema authors SHALL NOT
expect these additional keywords and vocabularies to be supported by
implementations that do not explicitly document such support.
</t>
<t>
Implementations MAY provide the ability to register or load handlers
for vocabularies that they do not support directly. The exact mechanism
for registering and implementing such handlers is implementation-dependent.
</t>
<section title="Handling of unrecognized or unsupported keywords" anchor="unrecognized">
<t>
Implementations SHOULD treat keywords they do not recognize, or that
they recognize but do not support, as annotations, where the value of
the keyword is the value of the annotation. Whether an implementation
collects these annotations or not, they MUST otherwise ignore the keywords.
</t>
</section>
</section>
</section>
<section title="Keyword Behaviors">
<t>
JSON Schema keywords fall into several general behavior categories.
Assertions validate that an instance satisfies constraints, producing
a boolean result. Annotations attach information that applications
may use in any way they see fit.
Applicators apply subschemas to parts of the instance and combine
their results.
</t>
<t>
Extension keywords SHOULD stay within these categories, keeping in mind
that annotations in particular are extremely flexible. Complex behavior
is usually better delegated to applications on the basis of annotation
data than implemented directly as schema keywords. However, extension
keywords MAY define other behaviors for specialized purposes.
</t>
<t>
Evaluating an instance against a schema involves processing all of the
keywords in the schema against the appropriate locations within the instance.
Typically, applicator keywords are processed until a schema object with no
applicators (and therefore no subschemas) is reached. The appropriate
location in the instance is evaluated against the assertion and
annotation keywords in the schema object. The interactions of those
keyword results to produce the schema object results are governed by
<xref target="annot-assert"></xref>, while the
relationship of subschema results to the results of the applicator
keyword that applied them is described by <xref target="applicators"></xref>.
</t>
<t>
Evaluation of a parent schema object can complete once all of its
subschemas have been evaluated, although in some circumstances evaluation
may be short-circuited due to assertion results. When annotations are
being collected, some assertion result short-circuiting is not possible
due to the need to examine all subschemas for annotation collection, including
those that cannot further change the assertion result.
</t>
<section title="Lexical Scope and Dynamic Scope" anchor="scopes">
<t>
While most JSON Schema keywords can be evaluated on their own,
or at most need to take into account the values or results of
adjacent keywords in the same schema object, a few have more
complex behavior.
</t>
<t>
The lexical scope of a keyword is determined by the nested JSON
data structure of objects and arrays. The largest such scope
is an entire schema document. The smallest scope is a single
schema object with no subschemas.
</t>
<t>
Keywords MAY be defined with a partial value, such as a IRI-reference,
which must be resolved against another value, such as another
IRI-reference or a full IRI, which is found through the lexical
structure of the JSON document. The "$id", "$ref", and
"$dynamicRef" core keywords, and the "base" JSON Hyper-Schema
keyword, are examples of this sort of behavior.
</t>
<t>
Note that some keywords, such as "$schema", apply to the lexical
scope of the entire schema resource, and therefore MUST only
appear in a schema resource's root schema.
</t>
<t>
Other keywords may take into account the dynamic scope that
exists during the evaluation of a schema, typically together
with an instance document.
The outermost dynamic scope is the schema object at
which processing begins, even if it is not a schema resource root.
The path from this root schema to any particular keyword (that
includes any "$ref" and "$dynamicRef" keywords that may have
been resolved) is considered the keyword's "evaluation path."
</t>
<t>
Lexical and dynamic scopes align until a reference keyword
is encountered. While following the reference keyword moves processing
from one lexical scope into a different one, from the perspective
of dynamic scope, following a reference is no different from descending
into a subschema present as a value. A keyword on the far side of
that reference that resolves information through the dynamic scope
will consider the originating side of the reference to be their
dynamic parent, rather than examining the local lexically enclosing parent.
</t>
<t>
The concept of dynamic scope is primarily used with "$dynamicRef" and
"$dynamicAnchor", and should be considered an advanced feature
and used with caution when defining additional keywords. It also appears
when reporting errors and collected annotations, as it may be possible
to revisit the same lexical scope repeatedly with different dynamic
scopes. In such cases, it is important to inform the user of the
evaluation path that produced the error or annotation.
</t>
</section>
<section title="Keyword Interactions">
<t>
Keyword behavior MAY be defined in terms of the annotation results
of <xref target="root">subschemas</xref> and/or adjacent keywords
(keywords within the same schema object) and their subschemas.
Such keywords MUST NOT result in a circular dependency.
Keywords MAY modify their behavior based on the presence or absence
of another keyword in the same
<xref target="schema-document">schema object</xref>.
</t>
</section>
<section title="Default Behaviors" anchor="default-behaviors">
<t>
A missing keyword MUST NOT produce a false assertion result, MUST
NOT produce annotation results, and MUST NOT cause any other schema
to be evaluated as part of its own behavioral definition.
However, given that missing keywords do not contribute annotations,
the lack of annotation results may indirectly change the behavior
of other keywords.
</t>
<t>
In some cases, the missing keyword assertion behavior of a keyword is
identical to that produced by a certain value, and keyword definitions
SHOULD note such values where known. However, even if the value which
produces the default behavior would produce annotation results if
present, the default behavior still MUST NOT result in annotations.
</t>
<t>
Because annotation collection can add significant cost in terms of both
computation and memory, implementations MAY opt out of this feature.
Keywords that are specified in terms of collected annotations SHOULD
describe reasonable alternate approaches when appropriate.
This approach is demonstrated by the
"<xref target="items" format="title"/>" and
"<xref target="additionalProperties" format="title"/>" keywords in this
document.
</t>
<t>
Note that when no such alternate approach is possible for a keyword,
implementations that do not support annotation collections will not
be able to support those keywords or vocabularies that contain them.
</t>
</section>
<section title="Identifiers">
<t>
Identifiers define IRIs for a schema, or affect how such IRIs are
resolved in <xref target="references">references</xref>, or both.
The Core vocabulary defined in this document defines several
identifying keywords, most notably "$id".
</t>
<t>
Canonical schema IRIs MUST NOT change while processing an instance, but
keywords that affect IRI-reference resolution MAY have behavior that
is only fully determined at runtime.
</t>
<t>
While custom identifier keywords are possible, vocabulary designers should
take care not to disrupt the functioning of core keywords. For example,
the "$dynamicAnchor" keyword in this specification limits its IRI resolution
effects to the matching "$dynamicRef" keyword, leaving the behavior
of "$ref" undisturbed.
</t>
</section>
<section title="Applicators" anchor="applicators">
<t>
Applicators allow for building more complex schemas than can be accomplished
with a single schema object. Evaluation of an instance against a
<xref target="schema-document">schema document</xref> begins by applying
the <xref target="root">root schema</xref> to the complete instance
document. From there, keywords known as applicators are used to determine
which additional schemas are applied. Such schemas may be applied in-place
to the current location, or to a child location.
</t>
<t>
The schemas to be applied may be present as subschemas comprising all or
part of the keyword's value. Alternatively, an applicator may refer to
a schema elsewhere in the same schema document, or in a different one.
The mechanism for identifying such referenced schemas is defined by the
keyword.
</t>
<t>
Applicator keywords also define how subschema or referenced schema
boolean <xref target="assertions">assertion</xref>
results are modified and/or combined to produce the boolean result
of the applicator. Applicators may apply any boolean logic operation
to the assertion results of subschemas, but MUST NOT introduce new
assertion conditions of their own.
</t>
<t>
<xref target="annotations">Annotation</xref> results from subschemas
are preserved in accordance with <xref target="collect"></xref> so that applications
can decide how to interpret multiple values. Applicator keywords
do not play a direct role in this preservation.
</t>
<section title="Referenced and Referencing Schemas" anchor="referenced">
<t>
As noted in <xref target="applicators" />, an applicator keyword may
refer to a schema to be applied, rather than including it as a
subschema in the applicator's value. In such situations, the
schema being applied is known as the referenced schema, while
the schema containing the applicator keyword is the referencing schema.
</t>
<t>
While root schemas and subschemas are static concepts based on a
schema's position within a schema document, referenced and referencing
schemas are dynamic. Different pairs of schemas may find themselves
in various referenced and referencing arrangements during the evaluation
of an instance against a schema.
</t>
<t>
For some by-reference applicators, such as
<xref target="ref">"$ref"</xref>, the referenced schema can be determined
by static analysis of the schema document's lexical scope. Others,
such as "$dynamicRef" (with "$dynamicAnchor"), may make use of dynamic
scoping, and therefore only be resolvable in the process of evaluating
the schema with an instance.
</t>
</section>
</section>
<section title="Assertions" anchor="assertions">
<t>
JSON Schema can be used to assert constraints on a JSON document, which
either passes or fails the assertions. This approach can be used to validate
conformance with the constraints, or document what is needed to satisfy them.
</t>
<t>
JSON Schema implementations produce a single boolean result when evaluating
an instance against schema assertions.
</t>
<t>
An instance can only fail an assertion that is present in the schema.
</t>
<section title="Assertions and Instance Primitive Types">
<t>
Most assertions only constrain values within a certain
primitive type. When the type of the instance is not of the type
targeted by the keyword, the instance is considered to conform
to the assertion.
</t>
<t>
For example, the "maxLength" keyword from the companion
<xref target="json-schema-validation">validation vocabulary</xref>:
will only restrict certain strings
(that are too long) from being valid. If the instance is a number,
boolean, null, array, or object, then it is valid against this assertion.
</t>
<t>
This behavior allows keywords to be used more easily with instances
that can be of multiple primitive types. The companion validation
vocabulary also includes a "type" keyword which can independently
restrict the instance to one or more primitive types. This allows
for a concise expression of use cases such as a function that might
return either a string of a certain length or a null value:
</t>
<sourcecode type="json"><![CDATA[
{
"type": ["string", "null"],
"maxLength": 255
}
]]></sourcecode>
<t>
If "maxLength" also restricted the instance type to be a string,
then this would be substantially more cumbersome to express because
the example as written would not actually allow null values.
Each keyword is evaluated separately unless explicitly specified
otherwise, so if "maxLength" restricted the instance to strings,
then including "null" in "type" would not have any useful effect.
</t>
</section>
</section>
<section title="Annotations" anchor="annotations">
<t>
JSON Schema can annotate an instance with information, whenever the instance
validates against the schema object containing the annotation, and all of its
parent schema objects. The information can be a simple value, or can be
calculated based on the instance contents.
</t>
<t>
Annotations are attached to specific locations in an instance.
Since many subschemas can be applied to any single
location, applications may need to decide how to handle differing
annotation values being attached to the same instance location by
the same schema keyword in different schema objects.
</t>
<t>
Unlike assertion results, annotation data can take a wide variety of forms,
which are provided to applications to use as they see fit. JSON Schema
implementations are not expected to make use of the collected information
on behalf of applications.
</t>
<t>
Unless otherwise specified, the value of an annotation keyword
is the keyword's value. However, other behaviors are possible.
For example, <xref target="json-hyper-schema">JSON Hyper-Schema's</xref>
"links" keyword is a complex annotation that produces a value based
in part on the instance data.
</t>
<t>
While "short-circuit" evaluation is possible for assertions, collecting
annotations requires examining all schemas that apply to an instance
location, even if they cannot change the overall assertion result.
The only exception is that subschemas of a schema object that has
failed validation MAY be skipped, as annotations are not retained
for failing schemas.
</t>
<section title="Collecting Annotations" anchor="collect">
<t>
Annotations are collected by keywords that explicitly define
annotation-collecting behavior. Note that boolean schemas cannot
produce annotations as they do not make use of keywords.
</t>
<t>
A collected annotation MUST include the following information:
</t>
<ul>
<li>
The name of the keyword that produces the annotation
</li>
<li>
The instance location to which it is attached, as a JSON Pointer
</li>
<li>
The evaluation path, indicating how reference keywords
such as "$ref" were followed to reach the absolute schema location.
</li>
<li>
The absolute schema location of the attaching keyword, as a IRI.
This MAY be omitted if it is the same as the evaluation path
from above.
</li>
<li>
The attached value(s)
</li>
</ul>
<section title="Distinguishing Among Multiple Values">
<t>
Applications MAY make decisions on which of multiple annotation values
to use based on the schema location that contributed the value.
This is intended to allow flexible usage. Collecting the schema location
facilitates such usage.
</t>
<t>
For example, consider this schema, which uses annotations and assertions from
the <xref target="json-schema-validation">Validation specification</xref>:
</t>
<t>
Note that some lines are wrapped for clarity.
</t>
<sourcecode type="json"><![CDATA[
{
"title": "Feature list",
"type": "array",
"prefixItems": [
{
"title": "Feature A",
"properties": {
"enabled": {
"$ref": "#/$defs/enabledToggle",
"default": true
}
}
},
{
"title": "Feature B",
"properties": {
"enabled": {