| •1 |
--- |
| 2 |
title: JSON Type Definition |
| 3 |
docname: draft-ucarion-json-type-definition-04 |
| 4 |
date: 2020-06-28 |
| 5 |
ipr: trust200902 |
| 6 |
area: Applications |
| 7 |
wg: Independent Submission |
| 8 |
kw: Internet-Draft |
| 9 |
cat: exp |
| 10 |
|
| 11 |
pi: |
| 12 |
toc: yes |
| 13 |
sortrefs: |
| 14 |
symrefs: yes |
| 15 |
|
| 16 |
author: |
| 17 |
- ins: U. Carion |
| 18 |
name: Ulysse Carion |
| 19 |
org: Segment.io, Inc |
| 20 |
abbrev: Segment |
| 21 |
street: 100 California Street |
| 22 |
city: San Francisco |
| 23 |
code: 94111 |
| 24 |
country: United States of America |
| 25 |
email: [email protected] |
| 26 |
|
| 27 |
normative: |
| 28 |
RFC3339: |
| 29 |
RFC4287: |
| 30 |
RFC6901: |
| 31 |
RFC8259: |
| 32 |
RFC8610: |
| 33 |
informative: |
| 34 |
RFC7071: |
| 35 |
RFC7493: |
| 36 |
I-D.handrews-json-schema: |
| 37 |
OPENAPI: |
| 38 |
target: https://spec.openapis.org/oas/v3.0.2 |
| 39 |
title: OpenAPI Specification |
| 40 |
author: |
| 41 |
org: OpenAPI Initiative |
| 42 |
date: 2019-10-08 |
| 43 |
|
| 44 |
--- abstract |
| 45 |
|
| 46 |
This document proposes a format, called JSON Type Definition (JTD), for |
| 47 |
describing the shape of JavaScript Object Notation (JSON) messages. Its main |
| 48 |
goals are to enable code generation from schemas as well as portable validation |
| 49 |
with standardized error indicators. To this end, JTD is intentionally limited to |
| 50 |
be no more expressive than the type systems of mainstream programming languages. |
| 51 |
This intentional limitation, as well as the decision to make JTD schemas be JSON |
| 52 |
documents, makes tooling atop of JTD easier to build. |
| 53 |
|
| 54 |
This document does not have IETF consensus and is presented here to facilitate |
| 55 |
experimentation with the concept of JTD. |
| 56 |
|
| 57 |
--- middle |
| 58 |
|
| 59 |
# Introduction |
| 60 |
|
| 61 |
This document describes a schema language for JSON {{RFC8259}} called JSON Type |
| 62 |
Definition (JTD). |
| 63 |
|
| 64 |
There exist many options for describing JSON data. JTD's niche is to focus on |
| 65 |
enabling code generation from schemas; to this end, JTD's expressiveness is |
| 66 |
intentionally limited to be no more powerful than what can be expressed in the |
| 67 |
type systems of mainstream programming languages. |
| 68 |
|
| 69 |
The goals of JTD are to: |
| 70 |
|
| 71 |
- Provide an unambiguous description of the overall structure of a JSON |
| 72 |
document. |
| 73 |
|
| 74 |
- Be able to describe common JSON datatypes and structures. That is, the |
| 75 |
datatypes and structures necessary to support most JSON documents, and which |
| 76 |
are widely understood in an interoperable way by JSON implementations. |
| 77 |
|
| 78 |
- Provide a single format that is readable and editable by both humans and |
| 79 |
machines, and which can be embedded within other JSON documents. This makes |
| 80 |
JTD a convenient format for tooling to accept as input or produce as output. |
| 81 |
|
| 82 |
- Enable code generation from JTD schemas. JTD schemas are meant to be easy to |
| 83 |
convert into data structures idiomatic to mainstream programming languages. |
| 84 |
|
| 85 |
- Provide a standardized format for error indicators when data does not conform |
| 86 |
with a schema. |
| 87 |
|
| 88 |
JTD is intentionally designed as a rather minimal schema language. Thus, |
| 89 |
although JTD can describe some categories of JSON, it is not able to describe |
| 90 |
its own structure: this document uses Concise Data Definition Language (CDDL) |
| 91 |
{{RFC8610}} to describe JTD's syntax. By keeping the expressiveness of the |
| 92 |
schema language minimal, JTD makes code generation and standardized error |
| 93 |
indicators easier to implement. |
| 94 |
|
| 95 |
Examples in this document use constructs from the C++ programming language. |
| 96 |
These examples are provided to aid the reader in understanding the principles of |
| 97 |
JTD, but are not limiting in any way. |
| 98 |
|
| 99 |
JTD's feature set is designed to represent common patterns in JSON-using |
| 100 |
applications, while still having a clear correspondence to programming languages |
| 101 |
in widespread use. Thus, JTD supports: |
| 102 |
|
| 103 |
- Signed and unsigned 8, 16, and 32-bit integers. A tool which converts JTD |
| 104 |
schemas into code can use `int8_t`, `uint8_t`, `int16_t`, etc., or their |
| 105 |
equivalents in the target language, to represent these JTD types. |
| 106 |
|
| 107 |
- A distinction between `float32` and `float64`. Code generators can use `float` |
| 108 |
and `double`, or their equivalents, for these JTD types. |
| 109 |
|
| 110 |
- A "properties" form of JSON objects, corresponding to some sort of struct or |
| 111 |
record. The "properties" form of JSON objects is akin to a C++ `struct`. |
| 112 |
|
| 113 |
- A "values" form of JSON objects, corresponding to some sort of dictionary or |
| 114 |
associative array. The "values" form of JSON objects is akin to a C++ |
| 115 |
`std::map`. |
| 116 |
|
| 117 |
- A "discriminator" form of JSON objects, corresponding to a discriminated (or |
| 118 |
"tagged") union. The "discriminator" form of JSON objects is akin to a C++ |
| 119 |
`std::variant`. |
| 120 |
|
| 121 |
The principle of common patterns in JSON is why JTD does not support 64-bit |
| 122 |
integers, as these are usually transmitted over JSON in a non-interoperable |
| 123 |
(i.e., ignoring the recommendations in Section 2.2 of {{RFC7493}}) or mutually |
| 124 |
inconsistent ways. {{other-considerations-int64}} further elaborates on why JTD |
| 125 |
does not support 64-bit integers. |
| 126 |
|
| 127 |
The principle of clear correspondence to common programming languages is why JTD |
| 128 |
does not support, for example, a data type for integers up to 2**53-1. |
| 129 |
|
| 130 |
It is expected that for many use-cases, a schema language of JTD's |
| 131 |
expressiveness is sufficient. Where a more expressive language is required, |
| 132 |
alternatives exist in CDDL and others. |
| 133 |
|
| 134 |
This document does not have IETF consensus and is presented here to facilitate |
| 135 |
experimentation with the concept of JTD. The purpose of the experiment is to |
| 136 |
gain experience with JTD and to possibly revise this work accordingly. If JTD |
| 137 |
is determined to be a valuable and popular approach it may be taken to the IETF |
| 138 |
for further discussion and revision. |
| 139 |
|
| 140 |
This document has the following structure: |
| 141 |
|
| 142 |
{{syntax}} defines the syntax of JTD. {{semantics}} describes the semantics of |
| 143 |
JTD; this includes determining whether some data satisfies a schema and what |
| 144 |
error indicators should be produced when the data is unsatisfactory. |
| 145 |
{{other-considerations}} discusses why certain features are omitted from JTD. |
| 146 |
{{comparison-with-cddl}} presents various JTD schemas and their CDDL |
| 147 |
equivalents. |
| 148 |
|
| 149 |
## Terminology |
| 150 |
|
| 151 |
{::boilerplate bcp14} |
| 152 |
|
| 153 |
The term "JSON Pointer", when it appears in this document, is to be understood |
| 154 |
as it is defined in {{RFC6901}}. |
| 155 |
|
| 156 |
The terms "object", "member", "array", "number", "name", and "string" in this |
| 157 |
document are to be interpreted as described in {{RFC8259}}. |
| 158 |
|
| 159 |
The term "instance", when it appears in this document, refers to a JSON value |
| 160 |
being validated against a JTD schema. This value can be an entire JSON document, |
| 161 |
or it can be a value embedded within a JSON document. |
| 162 |
|
| 163 |
## Scope of Experiment |
| 164 |
|
| 165 |
JTD is an experiment. Participation in this experiment consists of using JTD to |
| 166 |
validate or document interchanged JSON messages, or in building tooling atop of |
| 167 |
JTD. Feedback on the results of this experiment may be e-mailed to the author. |
| 168 |
Participants in this experiment are anticipated to mostly be nodes that provide |
| 169 |
or consume JSON-based APIs. |
| 170 |
|
| 171 |
Nodes know if they are participating in the experiment if they are validating |
| 172 |
JSON messages against a JTD schema, or if they are relying on another node to do |
| 173 |
so. Nodes are also participating in the experiment if they are running code |
| 174 |
generated from a JTD schema. |
| 175 |
|
| 176 |
The risk of this experiment "escaping" takes the form of a JTD-supporting node |
| 177 |
expecting another node, which lacks such support, to validate messages against |
| 178 |
some JTD schema. In such a case, the outcome will likely be that the nodes fail |
| 179 |
to interchange information correctly. |
| 180 |
|
| 181 |
This experiment will be deemed successful when JTD has been implemented by |
| 182 |
multiple independent parties, and these parties successfully use JTD to |
| 183 |
facilitate information interchange within their internal systems or between |
| 184 |
systems operated by independent parties. |
| 185 |
|
| 186 |
If this experiment is deemed successful, and JTD is determined to be a valuable |
| 187 |
and popular approach, it may be taken to the IETF for further discussion and |
| 188 |
revision. One possible outcome of this discussion and revision could be that a |
| 189 |
working group produces a Standards Track specification of JTD. |
| 190 |
|
| 191 |
Some implementations of JTD, as well as code generators and other tooling |
| 192 |
related to JTD, are available at \<https://github.com/jsontypedef\>. |
| 193 |
|
| 194 |
# Syntax {#syntax} |
| 195 |
|
| 196 |
This section describes when a JSON document is a correct JTD schema. Because |
| 197 |
Concise Data Definition Language (CDDL) is well-suited to the task of defining |
| 198 |
complex JSON formats, such as JTD schemas, this section uses CDDL to describe |
| 199 |
the format of JTD schemas. |
| 200 |
|
| 201 |
JTD schemas may recursively contain other schemas. In this document, a "root |
| 202 |
schema" is one which is not contained within another schema, i.e. it is |
| 203 |
"top-level". |
| 204 |
|
| 205 |
A JTD schema is a JSON object taking on an appropriate form. JTD schemas may |
| 206 |
contain "additional data", discussed in {{extending-JTD-syntax}}. Root JTD |
| 207 |
schemas may optionally contain definitions (a mapping from names to schemas). |
| 208 |
|
| 209 |
A correct root JTD schema MUST match the `root-schema` CDDL rule described in |
| 210 |
this section. A correct non-root JTD schema MUST match the `schema` CDDL rule |
| 211 |
described in this section. |
| 212 |
|
| 213 |
~~~ cddl |
| 214 |
; root-schema is identical to schema, but additionally allows for |
| 215 |
; definitions. |
| 216 |
; |
| 217 |
; definitions are prohibited from appearing on non-root schemas. |
| 218 |
root-schema = { |
| 219 |
? definitions: { * tstr => { schema}}, |
| 220 |
schema, |
| 221 |
} |
| 222 |
|
| 223 |
; schema is the main CDDL rule defining a JTD schema. |
| 224 |
; |
| 225 |
; All JTD schemas are JSON objects taking on one of eight forms |
| 226 |
; listed here. |
| 227 |
schema = ( |
| 228 |
ref // |
| 229 |
type // |
| 230 |
enum // |
| 231 |
elements // |
| 232 |
properties // |
| 233 |
values // |
| 234 |
discriminator // |
| 235 |
empty // |
| 236 |
) |
| 237 |
|
| 238 |
; shared is a CDDL rule containing properties that all eight schema |
| 239 |
; forms share. |
| 240 |
shared = ( |
| 241 |
? metadata: { * tstr => any }, |
| 242 |
? nullable: bool, |
| 243 |
) |
| 244 |
|
| 245 |
; empty describes the "empty" schema form. |
| 246 |
empty = shared |
| 247 |
|
| 248 |
; ref describes the "ref" schema form. |
| 249 |
; |
| 250 |
; There are additional constraints on this form that cannot be |
| 251 |
; expressed in CDDL. Section 2.2.2 describes these additional |
| 252 |
; constraints in detail. |
| 253 |
ref = ( ref: tstr, shared ) |
| 254 |
|
| 255 |
; type describes the "type" schema form. |
| 256 |
type = ( |
| 257 |
type: "boolean" |
| 258 |
/ "float32" |
| 259 |
/ "float64" |
| 260 |
/ "int8" |
| 261 |
/ "uint8" |
| 262 |
/ "int16" |
| 263 |
/ "uint16" |
| 264 |
/ "int32" |
| 265 |
/ "uint32" |
| 266 |
/ "string" |
| 267 |
/ "timestamp", |
| 268 |
shared, |
| 269 |
) |
| 270 |
|
| 271 |
; enum describes the "enum" schema form. |
| 272 |
; |
| 273 |
; There are additional constraints on this form that cannot be |
| 274 |
; expressed in CDDL. Section 2.2.4 describes these additional |
| 275 |
; constraints in detail. |
| 276 |
enum = ( enum: [+ tstr], shared ) |
| 277 |
|
| 278 |
; elements describes the "elements" schema form. |
| 279 |
elements = ( elements: { schema }, shared ) |
| 280 |
|
| 281 |
; properties describes the "properties" schema form. |
| 282 |
; |
| 283 |
; This CDDL rule is defined so that a schema of the "properties" form |
| 284 |
; may omit a member named "properties" or a member named |
| 285 |
; "optionalProperties", but not both. |
| 286 |
; |
| 287 |
; There are additional constraints on this form that cannot be |
| 288 |
; expressed in CDDL. Section 2.2.6 describes these additional |
| 289 |
; constraints in detail. |
| 290 |
properties = (with-properties // with-optional-properties) |
| 291 |
|
| 292 |
with-properties = ( |
| 293 |
properties: { * tstr => { schema }}, |
| 294 |
? optionalProperties: { * tstr => { schema }}, |
| 295 |
? additionalProperties: bool, |
| 296 |
shared, |
| 297 |
) |
| 298 |
|
| 299 |
with-optional-properties = ( |
| 300 |
? properties: { * tstr => { schema }}, |
| 301 |
optionalProperties: { * tstr => { schema }}, |
| 302 |
? additionalProperties: bool, |
| 303 |
shared, |
| 304 |
) |
| 305 |
|
| 306 |
; values describes the "values" schema form. |
| 307 |
values = ( values: { schema }, shared ) |
| 308 |
|
| 309 |
; discriminator describes the "discriminator" schema form. |
| 310 |
; |
| 311 |
; There are additional constraints on this form that cannot be |
| 312 |
; expressed in CDDL. Section 2.2.8 describes these additional |
| 313 |
; constraints in detail. |
| 314 |
discriminator = ( |
| 315 |
discriminator: tstr, |
| 316 |
|
| 317 |
; Note well: this rule is defined in terms of the "properties" |
| 318 |
; CDDL rule, not the "schema" CDDL rule. |
| 319 |
mapping: { * tstr => { properties } } |
| 320 |
shared, |
| 321 |
) |
| 322 |
~~~ |
| 323 |
{: #cddl-schema title="CDDL definition of a schema"} |
| 324 |
|
| 325 |
The remainder of this section will describe constraints on JTD schemas which |
| 326 |
cannot be expressed in CDDL, and will provide examples of valid and invalid JTD |
| 327 |
schemas. |
| 328 |
|
| 329 |
## Root vs. non-root schemas |
| 330 |
|
| 331 |
The `root-schema` rule in {{cddl-schema}} permits for a member named |
| 332 |
`definitions`, but the `schema` rule does not permit for such a member. This |
| 333 |
means that only root (i.e., "top-level") JTD schemas can have a `definitions` |
| 334 |
object, and sub-schemas may not. |
| 335 |
|
| 336 |
Thus |
| 337 |
|
| 338 |
~~~ json |
| 339 |
{ "definitions": {} } |
| 340 |
~~~ |
| 341 |
|
| 342 |
is a correct JTD schema, but |
| 343 |
|
| 344 |
~~~ json |
| 345 |
{ |
| 346 |
"definitions": { |
| 347 |
"foo": { |
| 348 |
"definitions": {} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
~~~ |
| 353 |
|
| 354 |
is not, because sub-schemas (such as the object at `/definitions/foo`) must not |
| 355 |
have a member named `definitions`. |
| 356 |
|
| 357 |
## Forms |
| 358 |
|
| 359 |
JTD schemas (i.e. JSON objects satisfying the `schema` CDDL rule in |
| 360 |
{{cddl-schema}}) must take on one of eight forms. These forms are defined so as |
| 361 |
to be mutually exclusive; a schema cannot satisfy multiple forms at once. |
| 362 |
|
| 363 |
### Empty {#syntax-form-empty} |
| 364 |
|
| 365 |
The `empty` form is defined by the `empty` CDDL rule in {{cddl-schema}}. The |
| 366 |
semantics of the `empty` form are described in {{semantics-form-empty}}. |
| 367 |
|
| 368 |
Despite the name "empty", schemas of the `empty` form are not necessarily empty |
| 369 |
JSON objects. Like schemas of any of the eight forms, schemas of the `empty` |
| 370 |
form may contain members named `nullable` (whose value must be `true` or |
| 371 |
`false`) or `metadata` (whose value must be an object) or both. |
| 372 |
|
| 373 |
Thus |
| 374 |
|
| 375 |
~~~ json |
| 376 |
{} |
| 377 |
~~~ |
| 378 |
|
| 379 |
and |
| 380 |
|
| 381 |
~~~ json |
| 382 |
{ "nullable": true } |
| 383 |
~~~ |
| 384 |
|
| 385 |
and |
| 386 |
|
| 387 |
~~~ json |
| 388 |
{ "nullable": true, "metadata": { "foo": "bar" }} |
| 389 |
~~~ |
| 390 |
|
| 391 |
are correct JTD schemas of the empty form, but |
| 392 |
|
| 393 |
~~~ json |
| 394 |
{ "nullable": "foo" } |
| 395 |
~~~ |
| 396 |
|
| 397 |
is not, because the value of the member named `nullable` must be `true` or |
| 398 |
`false`. |
| 399 |
|
| 400 |
### Ref {#syntax-form-ref} |
| 401 |
|
| 402 |
The `ref` form is defined by the `ref` CDDL rule in {{cddl-schema}}. The |
| 403 |
semantics of the `ref` form are described in {{semantics-form-ref}}. |
| 404 |
|
| 405 |
For a schema of the `ref` form to be correct, the value of the member named |
| 406 |
`ref` must refer to one of the definitions found at the root level of the schema |
| 407 |
it appears in. More formally, for a schema *S* of the `ref` form: |
| 408 |
|
| 409 |
- Let *B* be the root schema containing the schema, or the schema itself if it |
| 410 |
is a root schema. |
| 411 |
- Let *R* be the value of the member of *S* with the name `ref`. |
| 412 |
|
| 413 |
If the schema is correct, then *B* MUST have a member *D* with the name |
| 414 |
`definitions`, and *D* MUST contain a member whose name equals *R*. |
| 415 |
|
| 416 |
Thus |
| 417 |
|
| 418 |
~~~ json |
| 419 |
{ |
| 420 |
"definitions": { |
| 421 |
"coordinates": { |
| 422 |
"properties": { |
| 423 |
"lat": { "type": "float32" }, |
| 424 |
"lng": { "type": "float32" } |
| 425 |
} |
| 426 |
} |
| 427 |
}, |
| 428 |
"properties": { |
| 429 |
"user_location": { "ref": "coordinates" }, |
| 430 |
"server_location": { "ref": "coordinates" } |
| 431 |
} |
| 432 |
} |
| 433 |
~~~ |
| 434 |
|
| 435 |
is a correct JTD schema, and demonstrates the point of the `ref` form: to avoid |
| 436 |
re-defining the same thing twice. However, |
| 437 |
|
| 438 |
~~~ json |
| 439 |
{ "ref": "foo" } |
| 440 |
~~~ |
| 441 |
|
| 442 |
is not a correct JTD schema, as there is no top-level `definitions`, and so the |
| 443 |
`ref` form cannot be correct. Similarly, |
| 444 |
|
| 445 |
~~~ json |
| 446 |
{ "definitions": { "foo": {}}, "ref": "bar" } |
| 447 |
~~~ |
| 448 |
|
| 449 |
is not a correct JTD schema, as there is no member named `bar` in the top-level |
| 450 |
`definitions`. |
| 451 |
|
| 452 |
### Type {#syntax-form-type} |
| 453 |
|
| 454 |
The `type` form is defined by the `type` CDDL rule in {{cddl-schema}}. The |
| 455 |
semantics of the `type` form are described in {{semantics-form-type}}. |
| 456 |
|
| 457 |
As an example of a correct JTD schema of the `type` form, |
| 458 |
|
| 459 |
~~~ json |
| 460 |
{ "type": "uint8" } |
| 461 |
~~~ |
| 462 |
|
| 463 |
is a correct JTD schema, whereas |
| 464 |
|
| 465 |
~~~ json |
| 466 |
{ "type": true } |
| 467 |
~~~ |
| 468 |
|
| 469 |
and |
| 470 |
|
| 471 |
~~~ json |
| 472 |
{ "type": "foo" } |
| 473 |
~~~ |
| 474 |
|
| 475 |
are not correct schemas, as neither `true` nor the JSON string `foo` are in the |
| 476 |
list of permitted values of the `type` member described in the `type` CDDL rule |
| 477 |
in {{cddl-schema}}. |
| 478 |
|
| 479 |
### Enum {#syntax-form-enum} |
| 480 |
|
| 481 |
The `enum` form is defined by the `enum` CDDL rule in {{cddl-schema}}. The |
| 482 |
semantics of the `enum` form are described in {{semantics-form-enum}}. |
| 483 |
|
| 484 |
For a schema of the `enum` form to be correct, the value of the member named |
| 485 |
`enum` must be a nonempty array of strings, and that array must not contain |
| 486 |
duplicate values. More formally, for a schema *S* of the `enum` form: |
| 487 |
|
| 488 |
- Let *E* be the value of the member of *S* with name `enum`. |
| 489 |
|
| 490 |
If the schema is correct, then there MUST NOT exist any pair of elements of *E* |
| 491 |
which encode equal string values, where string equality is defined as in Section |
| 492 |
8.3 of {{RFC8259}}. |
| 493 |
|
| 494 |
Thus |
| 495 |
|
| 496 |
~~~ json |
| 497 |
{ "enum": [] } |
| 498 |
~~~ |
| 499 |
|
| 500 |
is not a correct JTD schema, as the value of the member named `enum` must be |
| 501 |
nonempty, and |
| 502 |
|
| 503 |
~~~ json |
| 504 |
{ "enum": ["a\\b", "a\u005Cb"] } |
| 505 |
~~~ |
| 506 |
|
| 507 |
is not a correct JTD schema, as |
| 508 |
|
| 509 |
~~~ json |
| 510 |
"a\\b" |
| 511 |
~~~ |
| 512 |
|
| 513 |
and |
| 514 |
|
| 515 |
~~~ json |
| 516 |
"a\u005Cb" |
| 517 |
~~~ |
| 518 |
|
| 519 |
encode strings that are equal by the definition of string equality given in |
| 520 |
Section 8.3 of {{RFC8259}}. By contrast, |
| 521 |
|
| 522 |
~~~ json |
| 523 |
{ "enum": ["PENDING", "IN_PROGRESS", "DONE" ]} |
| 524 |
~~~ |
| 525 |
|
| 526 |
is an example of a correct JTD schema of the `enum` form. |
| 527 |
|
| 528 |
### Elements {#syntax-form-elements} |
| 529 |
|
| 530 |
The `elements` form is defined by the `elements` CDDL rule in {{cddl-schema}}. |
| 531 |
The semantics of the `elements` form are described in |
| 532 |
{{semantics-form-elements}}. |
| 533 |
|
| 534 |
As an example of a correct JTD schema of the `elements` form, |
| 535 |
|
| 536 |
~~~ json |
| 537 |
{ "elements": { "type": "uint8" }} |
| 538 |
~~~ |
| 539 |
|
| 540 |
is a correct JTD schema, whereas |
| 541 |
|
| 542 |
~~~ json |
| 543 |
{ "elements": true } |
| 544 |
~~~ |
| 545 |
|
| 546 |
and |
| 547 |
|
| 548 |
~~~ json |
| 549 |
{ "elements": { "type": "foo" } } |
| 550 |
~~~ |
| 551 |
|
| 552 |
are not correct schemas, as neither |
| 553 |
|
| 554 |
~~~ json |
| 555 |
true |
| 556 |
~~~ |
| 557 |
|
| 558 |
nor |
| 559 |
|
| 560 |
~~~ json |
| 561 |
{ "type": "foo" } |
| 562 |
~~~ |
| 563 |
|
| 564 |
are correct JTD schemas, and the value of the member named `elements` must be a |
| 565 |
correct JTD schema. |
| 566 |
|
| 567 |
### Properties {#syntax-form-properties} |
| 568 |
|
| 569 |
The `properties` form is defined by the `properties` CDDL rule in |
| 570 |
{{cddl-schema}}. The semantics of the `properties` form are described in |
| 571 |
{{semantics-form-props}}. |
| 572 |
|
| 573 |
For a schema of the `properties` form to be correct, properties must either be |
| 574 |
required (i.e., in `properties`) or optional (i.e., in `optionalProperties`), |
| 575 |
but not both. More formally: |
| 576 |
|
| 577 |
If a schema has both a member named `properties` (with value *P*) and another |
| 578 |
member named `optionalProperties` (with value *O*), then *O* and *P* MUST NOT |
| 579 |
have any member names in common; that is, no member of *P* may have a name equal |
| 580 |
to the name of any member of *O*, under the definition of string equality given |
| 581 |
in Section 8.3 of {{RFC8259}}. |
| 582 |
|
| 583 |
Thus |
| 584 |
|
| 585 |
~~~ json |
| 586 |
{ |
| 587 |
"properties": { "confusing": {} }, |
| 588 |
"optionalProperties": { "confusing": {} } |
| 589 |
} |
| 590 |
~~~ |
| 591 |
|
| 592 |
is not a correct JTD schema, as `confusing` appears in both `properties` and |
| 593 |
`optionalProperties`. By contrast, |
| 594 |
|
| 595 |
~~~ json |
| 596 |
{ |
| 597 |
"properties": { |
| 598 |
"users": { |
| 599 |
"elements": { |
| 600 |
"properties": { |
| 601 |
"id": { "type": "string" }, |
| 602 |
"name": { "type": "string" }, |
| 603 |
"create_time": { "type": "timestamp" } |
| 604 |
}, |
| 605 |
"optionalProperties": { |
| 606 |
"delete_time": { "type": "timestamp" } |
| 607 |
} |
| 608 |
} |
| 609 |
}, |
| 610 |
"next_page_token": { "type": "string" } |
| 611 |
} |
| 612 |
} |
| 613 |
~~~ |
| 614 |
|
| 615 |
is a correct JTD schema of the `properties` form, describing a paginated list of |
| 616 |
users and demonstrating the recursive nature of the syntax of JTD schemas. |
| 617 |
|
| 618 |
### Values {#syntax-form-values} |
| 619 |
|
| 620 |
The `values` form is defined by the `values` CDDL rule in {{cddl-schema}}. The |
| 621 |
semantics of the `values` form are described in {{semantics-form-values}}. |
| 622 |
|
| 623 |
As an example of a correct JTD schema of the `values` form, |
| 624 |
|
| 625 |
~~~ json |
| 626 |
{ "values": { "type": "uint8" }} |
| 627 |
~~~ |
| 628 |
|
| 629 |
is a correct JTD schema, whereas |
| 630 |
|
| 631 |
~~~ json |
| 632 |
{ "values": true } |
| 633 |
~~~ |
| 634 |
|
| 635 |
and |
| 636 |
|
| 637 |
~~~ json |
| 638 |
{ "values": { "type": "foo" } } |
| 639 |
~~~ |
| 640 |
|
| 641 |
are not correct schemas, as neither |
| 642 |
|
| 643 |
~~~ json |
| 644 |
true |
| 645 |
~~~ |
| 646 |
|
| 647 |
nor |
| 648 |
|
| 649 |
~~~ json |
| 650 |
{ "type": "foo" } |
| 651 |
~~~ |
| 652 |
|
| 653 |
are correct JTD schemas, and the value of the member named `values` must be a |
| 654 |
correct JTD schema. |
| 655 |
|
| 656 |
### Discriminator {#syntax-form-discriminator} |
| 657 |
|
| 658 |
The `discriminator` form is defined by the `discriminator` CDDL rule in |
| 659 |
{{cddl-schema}}. The semantics of the `discriminator` form are described in |
| 660 |
{{semantics-form-discriminator}}. Understanding the semantics of the |
| 661 |
`discriminator` form will likely aid the reader in understanding why this |
| 662 |
section provides constraints on the `discriminator` form beyond those in |
| 663 |
{{cddl-schema}}. |
| 664 |
|
| 665 |
To prevent ambiguous or unsatisfiable constraints on the `discriminator` |
| 666 |
property of a tagged union, an additional constraint on schemas of the |
| 667 |
`discriminator` form exists. For schemas of the discriminator form: |
| 668 |
|
| 669 |
- Let *D* be the member of the schema with the name `discriminator`. |
| 670 |
- Let *M* be the member of the schema with the name `mapping`. |
| 671 |
|
| 672 |
If the schema is correct, then all member values *S* of *M* will be schemas of |
| 673 |
the "properties" form. For each *S*: |
| 674 |
|
| 675 |
- If *S* has a member *N* whose name equals `nullable`, *N*'s value MUST NOT be |
| 676 |
the JSON primitive value `true`. |
| 677 |
- For each member *P* of *S* whose name equals `properties` or |
| 678 |
`optionalProperties`, *P*'s value, which must be an object, MUST NOT contain |
| 679 |
any members whose name equals *D*'s value. |
| 680 |
|
| 681 |
Thus |
| 682 |
|
| 683 |
~~~ json |
| 684 |
{ |
| 685 |
"discriminator": "event_type", |
| 686 |
"mapping": { |
| 687 |
"can_the_object_be_null_or_not?": { |
| 688 |
"nullable": true, |
| 689 |
"properties": { "foo": { "type": "string" } }} |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
~~~ |
| 694 |
|
| 695 |
is an incorrect schema, as a member of `mapping` has a member named `nullable` |
| 696 |
whose value is `true`. This would suggest that the instance may be null. Yet the |
| 697 |
top-level schema lacks such a `nullable` set to `true`, which would suggest that |
| 698 |
the instance in fact cannot be null. If this were a correct JTD schema, it would |
| 699 |
be unclear which piece of information takes "precedence". |
| 700 |
|
| 701 |
JTD handles such possible ambiguity by disallowing, at the syntactic level, the |
| 702 |
possibility of contradictory specifications of whether an instance described by |
| 703 |
a schema of the `discriminator` form may be null. The schemas in a discriminator |
| 704 |
`mapping` cannot have `nullable` set to `true`; only the discriminator itself |
| 705 |
can use `nullable` in this way. |
| 706 |
|
| 707 |
It also follows that |
| 708 |
|
| 709 |
~~~ json |
| 710 |
{ |
| 711 |
"discriminator": "event_type", |
| 712 |
"mapping": { |
| 713 |
"is_event_type_a_string_or_a_float32?": { |
| 714 |
"properties": { "event_type": { "type": "float32" }} |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
~~~ |
| 719 |
|
| 720 |
and |
| 721 |
|
| 722 |
~~~ json |
| 723 |
{ |
| 724 |
"discriminator": "event_type", |
| 725 |
"mapping": { |
| 726 |
"is_event_type_a_string_or_an_optional_float32?": { |
| 727 |
"optionalProperties": { "event_type": { "type": "float32" }} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
~~~ |
| 732 |
|
| 733 |
are incorrect schemas, as `event_type` is both the value of `discriminator` and |
| 734 |
a member name in one of the `mapping` member `properties` or |
| 735 |
`optionalProperties`. This is ambiguous, because ordinarily the `discriminator` |
| 736 |
keyword would indicate that `event_type` is expected to be a string, but another |
| 737 |
part of the schema specifies that `event_type` is expected to be a number. |
| 738 |
|
| 739 |
JTD handles such possible ambiguity by disallowing, at the syntactic level, the |
| 740 |
possibility of contradictory specifications of discriminator "tags". |
| 741 |
Discriminator "tags" cannot be re-defined in other parts of the schema. |
| 742 |
|
| 743 |
By contrast, |
| 744 |
|
| 745 |
~~~ json |
| 746 |
{ |
| 747 |
"tag": "event_type", |
| 748 |
"mapping": { |
| 749 |
"account_deleted": { |
| 750 |
"properties": { |
| 751 |
"account_id": { "type": "string" } |
| 752 |
} |
| 753 |
}, |
| 754 |
"account_payment_plan_changed": { |
| 755 |
"properties": { |
| 756 |
"account_id": { "type": "string" }, |
| 757 |
"payment_plan": { "enum": ["FREE", "PAID"] } |
| 758 |
}, |
| 759 |
"optionalProperties": { |
| 760 |
"upgraded_by": { "type": "string" } |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
~~~ |
| 766 |
|
| 767 |
is a correct schema, describing a pattern of data common in JSON-based messaging |
| 768 |
systems. {{semantics-form-discriminator}} provides examples of what this schema |
| 769 |
accepts and rejects. |
| 770 |
|
| 771 |
## Extending JTD's Syntax {#extending-JTD-syntax} |
| 772 |
|
| 773 |
This document does not describe any extension mechanisms for JTD schema |
| 774 |
validation, which is described in {{semantics}}. However, schemas are defined to |
| 775 |
optionally contain a `metadata` keyword, whose value is an arbitrary JSON |
| 776 |
object. Call the members of this object "metadata members". |
| 777 |
|
| 778 |
Users MAY add metadata members to JTD schemas to convey information that is not |
| 779 |
pertinent to validation. For example, such metadata members could provide hints |
| 780 |
to code generators, or trigger some special behavior for a library that |
| 781 |
generates user interfaces from schemas. |
| 782 |
|
| 783 |
Users SHOULD NOT expect metadata members to be understood by other parties. As a |
| 784 |
result, if consistent validation with other parties is a requirement, users MUST |
| 785 |
NOT use metadata members to affect how schema validation, as described in |
| 786 |
{{semantics}}, works. |
| 787 |
|
| 788 |
Users MAY expect metadata members to be understood by other parties, and MAY use |
| 789 |
metadata members to affect how schema validation works, if these other parties |
| 790 |
are somehow known to support these metadata members. For example, two parties |
| 791 |
may agree, out of band, that they will support an extended JTD with a custom |
| 792 |
metadata member that affects validation. |
| 793 |
|
| 794 |
# Semantics {#semantics} |
| 795 |
|
| 796 |
This section describes when an instance is valid against a correct JTD schema, |
| 797 |
and the error indicators to produce when an instance is invalid. |
| 798 |
|
| 799 |
## Allowing Additional Properties {#allow-additional-properties} |
| 800 |
|
| 801 |
Users will have different desired behavior with respect to "unspecified" members |
| 802 |
in an instance. For example, consider the JTD schema in {{JTD-properties-a}}: |
| 803 |
|
| 804 |
~~~ json |
| 805 |
{ "properties": { "a": { "type": "string" }}} |
| 806 |
~~~ |
| 807 |
{: #JTD-properties-a title="An illustrative JTD schema"} |
| 808 |
|
| 809 |
Some users may expect that |
| 810 |
|
| 811 |
~~~ json |
| 812 |
{"a": "foo", "b": "bar"} |
| 813 |
~~~ |
| 814 |
|
| 815 |
satisfies the schema in {{JTD-properties-a}}. Others may disagree, as `b` is |
| 816 |
not one of the properties described in the schema. In this document, allowing |
| 817 |
such "unspecified" members, like `b` in this example, happens when evaluation is |
| 818 |
in "allow additional properties" mode. |
| 819 |
|
| 820 |
Evaluation of a schema does not allow additional properties by default, but can |
| 821 |
be overridden by having the schema include a member named |
| 822 |
`additionalProperties`, where that member has a value of `true`. |
| 823 |
|
| 824 |
More formally: evaluation of a schema *S* is in "allow additional properties" |
| 825 |
mode if there exists a member of *S* whose name equals `additionalProperties`, |
| 826 |
and whose value is a boolean `true`. Otherwise, evaluation of *S* is not in |
| 827 |
"allow additional properties" mode. |
| 828 |
|
| 829 |
See {{semantics-form-props}} for how allowing unknown properties affects schema |
| 830 |
evaluation, but briefly, the schema |
| 831 |
|
| 832 |
~~~ json |
| 833 |
{ "properties": { "a": { "type": "string" }}} |
| 834 |
~~~ |
| 835 |
|
| 836 |
rejects |
| 837 |
|
| 838 |
~~~json |
| 839 |
{ "a": "foo", "b": "bar" } |
| 840 |
~~~ |
| 841 |
|
| 842 |
However, the schema |
| 843 |
|
| 844 |
~~~ json |
| 845 |
{ |
| 846 |
"additionalProperties": true, |
| 847 |
"properties": { "a": { "type": "string" }} |
| 848 |
} |
| 849 |
~~~ |
| 850 |
|
| 851 |
accepts |
| 852 |
|
| 853 |
~~~ json |
| 854 |
{ "a": "foo", "b": "bar" } |
| 855 |
~~~ |
| 856 |
|
| 857 |
Note that `additionalProperties` does not get "inherited" by sub-schemas. For |
| 858 |
example, the JTD schema |
| 859 |
|
| 860 |
~~~ json |
| 861 |
{ |
| 862 |
"additionalProperties": true, |
| 863 |
"properties": { |
| 864 |
"a": { |
| 865 |
"properties": { |
| 866 |
"b": { "type": "string" } |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
~~~ |
| 872 |
|
| 873 |
accepts |
| 874 |
|
| 875 |
~~~ json |
| 876 |
{ "a": { "b": "c" }, "foo": "bar" } |
| 877 |
~~~ |
| 878 |
|
| 879 |
but rejects |
| 880 |
|
| 881 |
~~~ json |
| 882 |
{ "a": { "b": "c", "foo": "bar" }} |
| 883 |
~~~ |
| 884 |
|
| 885 |
because the `additionalProperties` at the root level does not affect the |
| 886 |
behavior of sub-schemas. |
| 887 |
|
| 888 |
Note from {{cddl-schema}} that only schemas of the `properties` form may have a |
| 889 |
member named `additionalProperties`. |
| 890 |
|
| 891 |
## Errors |
| 892 |
|
| 893 |
To facilitate consistent validation error handling, this document specifies a |
| 894 |
standard error indicator format. Implementations SHOULD support producing error |
| 895 |
indicators in this standard form. |
| 896 |
|
| 897 |
The standard error indicator format is a JSON array. The order of the elements |
| 898 |
of this array is not specified. The elements of this array are JSON objects |
| 899 |
with: |
| 900 |
|
| 901 |
- A member with the name `instancePath`, whose value is a JSON string encoding a |
| 902 |
JSON Pointer. This JSON Pointer will point to the part of the instance that |
| 903 |
was rejected. |
| 904 |
|
| 905 |
- A member with the name `schemaPath`, whose value is a JSON string encoding a |
| 906 |
JSON Pointer. This JSON Pointer will point to the part of the schema that |
| 907 |
rejected the instance. |
| 908 |
|
| 909 |
The values for `instancePath` and `schemaPath` depend on the form of the schema, |
| 910 |
and are described in detail in {{semantics-forms}}. |
| 911 |
|
| 912 |
## Forms {#semantics-forms} |
| 913 |
|
| 914 |
This section describes, for each of the eight JTD schema forms, the rules |
| 915 |
dictating whether an instance is accepted, as well as the error indicators to |
| 916 |
produce when an instance is invalid. |
| 917 |
|
| 918 |
The forms a correct schema may take on are formally described in {{syntax}}. |
| 919 |
|
| 920 |
### Empty {#semantics-form-empty} |
| 921 |
|
| 922 |
The `empty` form is meant to describe instances whose values are unknown, |
| 923 |
unpredictable, or otherwise unconstrained by the schema. The syntax of the |
| 924 |
`empty` form is described in {{syntax-form-empty}}. |
| 925 |
|
| 926 |
If a schema is of the empty form, then it accepts all instances. A schema of the |
| 927 |
empty form will never produce any error indicators. |
| 928 |
|
| 929 |
### Ref {#semantics-form-ref} |
| 930 |
|
| 931 |
The `ref` form is for when a schema is defined in terms of something in the |
| 932 |
`definitions` of the root schema. The ref form enables schemas to be less |
| 933 |
repetitive, and also enables describing recursive structures. The syntax of the |
| 934 |
`ref` form is described in {{syntax-form-ref}}. |
| 935 |
|
| 936 |
If a schema is of the ref form, then: |
| 937 |
|
| 938 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 939 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 940 |
the instance. Otherwise: |
| 941 |
- Let *B* be the root schema containing the schema, or the schema itself if it |
| 942 |
is a root schema. |
| 943 |
- Let *D* be the member of *B* with the name `definitions`. By {{syntax}}, *D* |
| 944 |
exists. |
| 945 |
- Let *R* be the value of the schema member with the name `ref`. |
| 946 |
- Let *S* be the value of the member of *D* whose name equals *R*. By |
| 947 |
{{syntax-form-ref}}, *S* exists, and is a schema. |
| 948 |
|
| 949 |
The schema accepts the instance if and only if *S* accepts the instance. |
| 950 |
Otherwise, the error indicators to return in this case are the union of the |
| 951 |
error indicators from evaluating *S* against the instance. |
| 952 |
|
| 953 |
For example, the schema: |
| 954 |
|
| 955 |
~~~ json |
| 956 |
{ |
| 957 |
"definitions": { "a": { "type": "float32" }}, |
| 958 |
"ref": "a" |
| 959 |
} |
| 960 |
~~~ |
| 961 |
|
| 962 |
accepts |
| 963 |
|
| 964 |
~~~ json |
| 965 |
123 |
| 966 |
~~~ |
| 967 |
|
| 968 |
but rejects |
| 969 |
|
| 970 |
~~~ json |
| 971 |
null |
| 972 |
~~~ |
| 973 |
|
| 974 |
with the error indicator |
| 975 |
|
| 976 |
~~~ json |
| 977 |
[{ "instancePath": "", "schemaPath": "/definitions/a/type" }] |
| 978 |
~~~ |
| 979 |
|
| 980 |
The schema |
| 981 |
|
| 982 |
~~~ json |
| 983 |
{ |
| 984 |
"definitions": { "a": { "type": "float32" }}, |
| 985 |
"ref": "a", |
| 986 |
"nullable": true |
| 987 |
} |
| 988 |
~~~ |
| 989 |
|
| 990 |
accepts |
| 991 |
|
| 992 |
~~~ json |
| 993 |
null |
| 994 |
~~~ |
| 995 |
|
| 996 |
because the schema has a `nullable` member, whose value is `true`. |
| 997 |
|
| 998 |
Note that `nullable` being `false` has no effect in any of the forms described |
| 999 |
in this document. For example, the schema |
| 1000 |
|
| 1001 |
~~~ json |
| 1002 |
{ |
| 1003 |
"definitions": { "a": { "nullable": false, "type": "float32" }}, |
| 1004 |
"ref": "a", |
| 1005 |
"nullable": true |
| 1006 |
} |
| 1007 |
~~~ |
| 1008 |
|
| 1009 |
accepts |
| 1010 |
|
| 1011 |
~~~ json |
| 1012 |
null |
| 1013 |
~~~ |
| 1014 |
|
| 1015 |
In other words, it is not the case that putting a `false` value for `nullable` |
| 1016 |
will ever "override" a `nullable` member in schemas of the `ref` form; it is |
| 1017 |
correct, though ineffectual, to have a value of `false` for the `nullable` |
| 1018 |
member in a schema. |
| 1019 |
|
| 1020 |
### Type {#semantics-form-type} |
| 1021 |
|
| 1022 |
The `type` form is meant to describe instances whose value is a boolean, number, |
| 1023 |
string, or timestamp ({{RFC3339}}). The syntax of the `type` form is described |
| 1024 |
in {{syntax-form-type}}. |
| 1025 |
|
| 1026 |
If a schema is of the type form, then: |
| 1027 |
|
| 1028 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1029 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 1030 |
the instance. Otherwise: |
| 1031 |
|
| 1032 |
- Let *T* be the value of the member with the name `type`. The following table |
| 1033 |
describes whether the instance is accepted, as a function of *T*'s value: |
| 1034 |
|
| 1035 |
|---------------------+----------------------------------------------| |
| 1036 |
| If \_T\_ equals ... | then the instance is accepted if it is ... | |
| 1037 |
|---------------------+----------------------------------------------| |
| 1038 |
| boolean | equal to `true` or `false` | |
| 1039 |
| float32 | a JSON number | |
| 1040 |
| float64 | a JSON number | |
| 1041 |
| int8 | See {{int-ranges}} | |
| 1042 |
| uint8 | See {{int-ranges}} | |
| 1043 |
| int16 | See {{int-ranges}} | |
| 1044 |
| uint16 | See {{int-ranges}} | |
| 1045 |
| int32 | See {{int-ranges}} | |
| 1046 |
| uint32 | See {{int-ranges}} | |
| 1047 |
| string | a JSON string | |
| 1048 |
| timestamp | a JSON string that follows the standard format described in {{RFC3339}}, as refined by Section 3.3 of {{RFC4287}} | |
| 1049 |
|---------------------+----------------------------------------------| |
| 1050 |
{: #type-values title="Accepted Values for Type"} |
| 1051 |
|
| 1052 |
`float32` and `float64` are distinguished from each other in their intent. |
| 1053 |
`float32` indicates data intended to be processed as an IEEE 754 |
| 1054 |
single-precision float, whereas `float64` indicates data intended to be |
| 1055 |
processed as an IEEE 754 double-precision float. Tools which generate code from |
| 1056 |
JTD schemas will likely produce different code for `float32` than for |
| 1057 |
`float64`. |
| 1058 |
|
| 1059 |
If _T_ starts with `int` or `uint`, then the instance is accepted if and only if |
| 1060 |
it is a JSON number encoding a value with zero fractional part. Depending on the |
| 1061 |
value of _T_, this encoded number must additionally fall within a particular |
| 1062 |
range: |
| 1063 |
|
| 1064 |
|--------+----------------------------+----------------------------| |
| 1065 |
| \_T\_ | Minimum Value (Inclusive) | Maximum Value (Inclusive) | |
| 1066 |
|--------+----------------------------+----------------------------| |
| 1067 |
| int8 | -128 | 127 | |
| 1068 |
| uint8 | 0 | 255 | |
| 1069 |
| int16 | -32,768 | 32,767 | |
| 1070 |
| uint16 | 0 | 65,535 | |
| 1071 |
| int32 | -2,147,483,648 | 2,147,483,647 | |
| 1072 |
| uint32 | 0 | 4,294,967,295 | |
| 1073 |
|--------+----------------------------+----------------------------| |
| 1074 |
{: #int-ranges title="Ranges for Integer Types"} |
| 1075 |
|
| 1076 |
Note that |
| 1077 |
|
| 1078 |
~~~ json |
| 1079 |
10 |
| 1080 |
~~~ |
| 1081 |
|
| 1082 |
and |
| 1083 |
|
| 1084 |
~~~ json |
| 1085 |
10.0 |
| 1086 |
~~~ |
| 1087 |
|
| 1088 |
and |
| 1089 |
|
| 1090 |
~~~ json |
| 1091 |
1.0e1 |
| 1092 |
~~~ |
| 1093 |
|
| 1094 |
encode values with zero fractional part, whereas |
| 1095 |
|
| 1096 |
~~~ json |
| 1097 |
10.5 |
| 1098 |
~~~ |
| 1099 |
|
| 1100 |
encodes a number with a non-zero fractional part. Thus the schema |
| 1101 |
|
| 1102 |
~~~ json |
| 1103 |
{"type": "int8"} |
| 1104 |
~~~ |
| 1105 |
|
| 1106 |
accepts |
| 1107 |
|
| 1108 |
~~~ json |
| 1109 |
10 |
| 1110 |
~~~ |
| 1111 |
|
| 1112 |
and |
| 1113 |
|
| 1114 |
~~~ json |
| 1115 |
10.0 |
| 1116 |
~~~ |
| 1117 |
|
| 1118 |
and |
| 1119 |
|
| 1120 |
~~~ json |
| 1121 |
1.0e1 |
| 1122 |
~~~ |
| 1123 |
|
| 1124 |
but rejects |
| 1125 |
|
| 1126 |
~~~ json |
| 1127 |
10.5 |
| 1128 |
~~~ |
| 1129 |
|
| 1130 |
as well as |
| 1131 |
|
| 1132 |
~~~ json |
| 1133 |
false |
| 1134 |
~~~ |
| 1135 |
|
| 1136 |
because "false" is not a number at all. |
| 1137 |
|
| 1138 |
If the instance is not accepted, then the error indicator for this case shall |
| 1139 |
have an `instancePath` pointing to the instance, and a `schemaPath` pointing to |
| 1140 |
the schema member with the name `type`. |
| 1141 |
|
| 1142 |
For example, the schema: |
| 1143 |
|
| 1144 |
~~~ json |
| 1145 |
{"type": "boolean"} |
| 1146 |
~~~ |
| 1147 |
|
| 1148 |
accepts |
| 1149 |
|
| 1150 |
~~~ json |
| 1151 |
false |
| 1152 |
~~~ |
| 1153 |
|
| 1154 |
but rejects |
| 1155 |
|
| 1156 |
~~~ json |
| 1157 |
127 |
| 1158 |
~~~ |
| 1159 |
|
| 1160 |
The schema: |
| 1161 |
|
| 1162 |
~~~ json |
| 1163 |
{"type": "float32"} |
| 1164 |
~~~ |
| 1165 |
|
| 1166 |
accepts |
| 1167 |
|
| 1168 |
~~~ json |
| 1169 |
10.5 |
| 1170 |
~~~ |
| 1171 |
|
| 1172 |
and |
| 1173 |
|
| 1174 |
~~~ json |
| 1175 |
127 |
| 1176 |
~~~ |
| 1177 |
|
| 1178 |
but rejects |
| 1179 |
|
| 1180 |
~~~ json |
| 1181 |
false |
| 1182 |
~~~ |
| 1183 |
|
| 1184 |
The schema: |
| 1185 |
|
| 1186 |
~~~ json |
| 1187 |
{"type": "string"} |
| 1188 |
~~~ |
| 1189 |
|
| 1190 |
accepts |
| 1191 |
|
| 1192 |
~~~ json |
| 1193 |
"1985-04-12T23:20:50.52Z" |
| 1194 |
~~~ |
| 1195 |
|
| 1196 |
and |
| 1197 |
|
| 1198 |
~~~ json |
| 1199 |
"foo" |
| 1200 |
~~~ |
| 1201 |
|
| 1202 |
but rejects |
| 1203 |
|
| 1204 |
~~~ json |
| 1205 |
false |
| 1206 |
~~~ |
| 1207 |
|
| 1208 |
The schema: |
| 1209 |
|
| 1210 |
~~~ json |
| 1211 |
{"type": "timestamp"} |
| 1212 |
~~~ |
| 1213 |
|
| 1214 |
accepts |
| 1215 |
|
| 1216 |
~~~ json |
| 1217 |
"1985-04-12T23:20:50.52Z" |
| 1218 |
~~~ |
| 1219 |
|
| 1220 |
but rejects |
| 1221 |
|
| 1222 |
~~~ json |
| 1223 |
"foo" |
| 1224 |
~~~ |
| 1225 |
|
| 1226 |
and |
| 1227 |
|
| 1228 |
~~~ json |
| 1229 |
false |
| 1230 |
~~~ |
| 1231 |
|
| 1232 |
The schema: |
| 1233 |
|
| 1234 |
~~~ json |
| 1235 |
{"type": "boolean", "nullable": true} |
| 1236 |
~~~ |
| 1237 |
|
| 1238 |
accepts |
| 1239 |
|
| 1240 |
~~~ json |
| 1241 |
null |
| 1242 |
~~~ |
| 1243 |
|
| 1244 |
and |
| 1245 |
|
| 1246 |
~~~ json |
| 1247 |
false |
| 1248 |
~~~ |
| 1249 |
|
| 1250 |
but rejects |
| 1251 |
|
| 1252 |
~~~ json |
| 1253 |
127 |
| 1254 |
~~~ |
| 1255 |
|
| 1256 |
In all of the examples of rejected instances given in this section, the error |
| 1257 |
indicator to produce is: |
| 1258 |
|
| 1259 |
~~~ json |
| 1260 |
[{ "instancePath": "", "schemaPath": "/type" }] |
| 1261 |
~~~ |
| 1262 |
|
| 1263 |
### Enum {#semantics-form-enum} |
| 1264 |
|
| 1265 |
The `enum` form is meant to describe instances whose value must be one of a |
| 1266 |
given set of string values. The syntax of the `enum` form is described in |
| 1267 |
{{syntax-form-enum}}. |
| 1268 |
|
| 1269 |
If a schema is of the enum form, then: |
| 1270 |
|
| 1271 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1272 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 1273 |
the instance. Otherwise: |
| 1274 |
|
| 1275 |
- Let *E* be the value of the schema member with the name `enum`. The instance |
| 1276 |
is accepted if and only if it is equal to one of the elements of *E*. |
| 1277 |
|
| 1278 |
If the instance is not accepted, then the error indicator for this case shall |
| 1279 |
have an `instancePath` pointing to the instance, and a `schemaPath` pointing to |
| 1280 |
the schema member with the name `enum`. |
| 1281 |
|
| 1282 |
For example, the schema: |
| 1283 |
|
| 1284 |
~~~ json |
| 1285 |
{ "enum": ["PENDING", "DONE", "CANCELED"] } |
| 1286 |
~~~ |
| 1287 |
|
| 1288 |
Accepts |
| 1289 |
|
| 1290 |
~~~ json |
| 1291 |
"PENDING" |
| 1292 |
~~~ |
| 1293 |
|
| 1294 |
and |
| 1295 |
|
| 1296 |
~~~ json |
| 1297 |
"DONE" |
| 1298 |
~~~ |
| 1299 |
|
| 1300 |
and |
| 1301 |
|
| 1302 |
~~~ json |
| 1303 |
"CANCELED" |
| 1304 |
~~~ |
| 1305 |
|
| 1306 |
but rejects all of |
| 1307 |
|
| 1308 |
~~~ json |
| 1309 |
0 |
| 1310 |
~~~ |
| 1311 |
|
| 1312 |
and |
| 1313 |
|
| 1314 |
~~~ json |
| 1315 |
1 |
| 1316 |
~~~ |
| 1317 |
|
| 1318 |
and |
| 1319 |
|
| 1320 |
~~~ json |
| 1321 |
2 |
| 1322 |
~~~ |
| 1323 |
|
| 1324 |
and |
| 1325 |
|
| 1326 |
~~~ json |
| 1327 |
"UNKNOWN" |
| 1328 |
~~~ |
| 1329 |
|
| 1330 |
and |
| 1331 |
|
| 1332 |
~~~ json |
| 1333 |
null |
| 1334 |
~~~ |
| 1335 |
|
| 1336 |
with the error indicator: |
| 1337 |
|
| 1338 |
~~~ json |
| 1339 |
[{ "instancePath": "", "schemaPath": "/enum" }] |
| 1340 |
~~~ |
| 1341 |
|
| 1342 |
The schema |
| 1343 |
|
| 1344 |
~~~ json |
| 1345 |
{ "enum": ["PENDING", "DONE", "CANCELED"], "nullable": true } |
| 1346 |
~~~ |
| 1347 |
|
| 1348 |
accepts |
| 1349 |
|
| 1350 |
~~~ json |
| 1351 |
"PENDING" |
| 1352 |
~~~ |
| 1353 |
|
| 1354 |
and |
| 1355 |
|
| 1356 |
~~~ json |
| 1357 |
null |
| 1358 |
~~~ |
| 1359 |
|
| 1360 |
but rejects |
| 1361 |
|
| 1362 |
~~~ json |
| 1363 |
1 |
| 1364 |
~~~ |
| 1365 |
|
| 1366 |
and |
| 1367 |
|
| 1368 |
~~~ json |
| 1369 |
"UNKNOWN" |
| 1370 |
~~~ |
| 1371 |
|
| 1372 |
with the error indicator: |
| 1373 |
|
| 1374 |
~~~ json |
| 1375 |
[{ "instancePath": "", "schemaPath": "/enum" }] |
| 1376 |
~~~ |
| 1377 |
|
| 1378 |
### Elements {#semantics-form-elements} |
| 1379 |
|
| 1380 |
The `elements` form is meant to describe instances that must be arrays. A |
| 1381 |
further sub-schema describes the elements of the array. The syntax of the |
| 1382 |
`elements` form is described in {{syntax-form-elements}}. |
| 1383 |
|
| 1384 |
If a schema is of the elements form, then: |
| 1385 |
|
| 1386 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1387 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 1388 |
the instance. Otherwise: |
| 1389 |
|
| 1390 |
- Let *S* be the value of the schema member with the name `elements`. The |
| 1391 |
instance is accepted if and only if all of the following are true: |
| 1392 |
|
| 1393 |
- The instance is an array. Otherwise, the error indicator for this case shall |
| 1394 |
have an `instancePath` pointing to the instance, and a `schemaPath` pointing |
| 1395 |
to the schema member with the name `elements`. |
| 1396 |
|
| 1397 |
- If the instance is an array, then every element of the instance must be |
| 1398 |
accepted by *S*. Otherwise, the error indicators for this case are the union |
| 1399 |
of all the errors arising from evaluating *S* against elements of the |
| 1400 |
instance. |
| 1401 |
|
| 1402 |
For example, the schema: |
| 1403 |
|
| 1404 |
~~~ json |
| 1405 |
{ |
| 1406 |
"elements": { |
| 1407 |
"type": "float32" |
| 1408 |
} |
| 1409 |
} |
| 1410 |
~~~ |
| 1411 |
|
| 1412 |
accepts |
| 1413 |
|
| 1414 |
~~~ json |
| 1415 |
[] |
| 1416 |
~~~ |
| 1417 |
|
| 1418 |
and |
| 1419 |
|
| 1420 |
~~~ json |
| 1421 |
[1, 2, 3] |
| 1422 |
~~~ |
| 1423 |
|
| 1424 |
but rejects |
| 1425 |
|
| 1426 |
~~~ json |
| 1427 |
null |
| 1428 |
~~~ |
| 1429 |
|
| 1430 |
with the error indicator: |
| 1431 |
|
| 1432 |
~~~ json |
| 1433 |
[{ "instancePath": "", "schemaPath": "/elements" }] |
| 1434 |
~~~ |
| 1435 |
|
| 1436 |
and rejects |
| 1437 |
|
| 1438 |
~~~ json |
| 1439 |
[1, 2, "foo", 3, "bar"] |
| 1440 |
~~~ |
| 1441 |
|
| 1442 |
with the error indicators: |
| 1443 |
|
| 1444 |
~~~ json |
| 1445 |
[ |
| 1446 |
{ "instancePath": "/2", "schemaPath": "/elements/type" }, |
| 1447 |
{ "instancePath": "/4", "schemaPath": "/elements/type" } |
| 1448 |
] |
| 1449 |
~~~ |
| 1450 |
|
| 1451 |
The schema |
| 1452 |
|
| 1453 |
~~~ json |
| 1454 |
{ |
| 1455 |
"elements": { |
| 1456 |
"type": "float32" |
| 1457 |
}, |
| 1458 |
"nullable": true |
| 1459 |
} |
| 1460 |
~~~ |
| 1461 |
|
| 1462 |
accepts |
| 1463 |
|
| 1464 |
~~~ json |
| 1465 |
null |
| 1466 |
~~~ |
| 1467 |
|
| 1468 |
and |
| 1469 |
|
| 1470 |
~~~ json |
| 1471 |
[] |
| 1472 |
~~~ |
| 1473 |
|
| 1474 |
and |
| 1475 |
|
| 1476 |
~~~ json |
| 1477 |
[1, 2, 3] |
| 1478 |
~~~ |
| 1479 |
|
| 1480 |
but rejects |
| 1481 |
|
| 1482 |
~~~ json |
| 1483 |
[1, 2, "foo", 3, "bar"] |
| 1484 |
~~~ |
| 1485 |
|
| 1486 |
with the error indicators: |
| 1487 |
|
| 1488 |
~~~ json |
| 1489 |
[ |
| 1490 |
{ "instancePath": "/2", "schemaPath": "/elements/type" }, |
| 1491 |
{ "instancePath": "/4", "schemaPath": "/elements/type" } |
| 1492 |
] |
| 1493 |
~~~ |
| 1494 |
|
| 1495 |
### Properties {#semantics-form-props} |
| 1496 |
|
| 1497 |
The `properties` form is meant to describe JSON objects being used as a |
| 1498 |
"struct". The syntax of the `properties` form is described in |
| 1499 |
{{syntax-form-properties}}. |
| 1500 |
|
| 1501 |
If a schema is of the properties form, then: |
| 1502 |
|
| 1503 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1504 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 1505 |
the instance. Otherwise the instance is accepted if and only if all of the |
| 1506 |
following are true: |
| 1507 |
|
| 1508 |
- The instance is an object. |
| 1509 |
|
| 1510 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1511 |
pointing to the instance, and a `schemaPath` pointing to the schema member |
| 1512 |
with the name `properties` if such a schema member exists; if such a member |
| 1513 |
doesn't exist, `schemaPath` shall point to the schema member with the name |
| 1514 |
`optionalProperties`. |
| 1515 |
|
| 1516 |
- If the instance is an object and the schema has a member named `properties`, |
| 1517 |
then let *P* be the value of the schema member named `properties`. *P*, by |
| 1518 |
{{syntax-form-properties}}, must be an object. For every member name in *P*, a |
| 1519 |
member of the same name in the instance must exist. |
| 1520 |
|
| 1521 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1522 |
pointing to the instance, and a `schemaPath` pointing to the member of *P* |
| 1523 |
failing the requirement just described. |
| 1524 |
|
| 1525 |
- If the instance is an object, then let *P* be the value of the schema member |
| 1526 |
named `properties` (if it exists), and *O* be the value of the schema member |
| 1527 |
named `optionalProperties` (if it exists). |
| 1528 |
|
| 1529 |
For every member *I* of the instance, find a member with the same name as |
| 1530 |
*I*'s in *P* or *O*. By {{syntax-form-properties}}, it is not possible for |
| 1531 |
both *P* and *O* to have such a member. If the "discriminator tag exemption" |
| 1532 |
is in effect on *I* (see {{semantics-form-discriminator}}), then ignore *I*. |
| 1533 |
Otherwise: |
| 1534 |
|
| 1535 |
- If no such member in *P* or *O* exists and validation is not in "allow |
| 1536 |
additional properties" mode (see {{allow-additional-properties}}), then the |
| 1537 |
instance is rejected. |
| 1538 |
|
| 1539 |
The error indicator for this case has an `instancePath` pointing to *I*, and |
| 1540 |
a `schemaPath` pointing to the schema. |
| 1541 |
|
| 1542 |
- If such a member in *P* or *O* does exist, then call this member *S*. If *S* |
| 1543 |
rejects *I*'s value, then the instance is rejected. |
| 1544 |
|
| 1545 |
The error indicators for this case are the union of the error indicators |
| 1546 |
from evaluating *S* against *I*'s value. |
| 1547 |
|
| 1548 |
An instance may have multiple errors arising from the third and fourth bullet in |
| 1549 |
the above. In this case, the error indicators are the union of the errors. |
| 1550 |
|
| 1551 |
For example, the schema: |
| 1552 |
|
| 1553 |
~~~ json |
| 1554 |
{ |
| 1555 |
"properties": { |
| 1556 |
"a": { "type": "string" }, |
| 1557 |
"b": { "type": "string" } |
| 1558 |
}, |
| 1559 |
"optionalProperties": { |
| 1560 |
"c": { "type": "string" }, |
| 1561 |
"d": { "type": "string" } |
| 1562 |
} |
| 1563 |
} |
| 1564 |
~~~ |
| 1565 |
|
| 1566 |
accepts |
| 1567 |
|
| 1568 |
~~~ json |
| 1569 |
{ "a": "foo", "b": "bar" } |
| 1570 |
~~~ |
| 1571 |
|
| 1572 |
and |
| 1573 |
|
| 1574 |
~~~ json |
| 1575 |
{ "a": "foo", "b": "bar", "c": "baz" } |
| 1576 |
~~~ |
| 1577 |
|
| 1578 |
and |
| 1579 |
|
| 1580 |
~~~ json |
| 1581 |
{ "a": "foo", "b": "bar", "c": "baz", "d": "quux" } |
| 1582 |
~~~ |
| 1583 |
|
| 1584 |
and |
| 1585 |
|
| 1586 |
~~~ json |
| 1587 |
{ "a": "foo", "b": "bar", "d": "quux" } |
| 1588 |
~~~ |
| 1589 |
|
| 1590 |
but rejects |
| 1591 |
|
| 1592 |
~~~ json |
| 1593 |
null |
| 1594 |
~~~ |
| 1595 |
|
| 1596 |
with the error indicator |
| 1597 |
|
| 1598 |
~~~ json |
| 1599 |
[{ "instancePath": "", "schemaPath": "/properties" }] |
| 1600 |
~~~ |
| 1601 |
|
| 1602 |
and rejects |
| 1603 |
|
| 1604 |
~~~ json |
| 1605 |
{ "b": 3, "c": 3, "e": 3 } |
| 1606 |
~~~ |
| 1607 |
|
| 1608 |
with the error indicators |
| 1609 |
|
| 1610 |
~~~ json |
| 1611 |
[ |
| 1612 |
{ "instancePath": "", |
| 1613 |
"schemaPath": "/properties/a" }, |
| 1614 |
{ "instancePath": "/b", |
| 1615 |
"schemaPath": "/properties/b/type" }, |
| 1616 |
{ "instancePath": "/c", |
| 1617 |
"schemaPath": "/optionalProperties/c/type" }, |
| 1618 |
{ "instancePath": "/e", |
| 1619 |
"schemaPath": "" } |
| 1620 |
] |
| 1621 |
~~~ |
| 1622 |
|
| 1623 |
If instead the schema had `additionalProperties: true`, but was otherwise the |
| 1624 |
same: |
| 1625 |
|
| 1626 |
~~~ json |
| 1627 |
{ |
| 1628 |
"properties": { |
| 1629 |
"a": { "type": "string" }, |
| 1630 |
"b": { "type": "string" } |
| 1631 |
}, |
| 1632 |
"optionalProperties": { |
| 1633 |
"c": { "type": "string" }, |
| 1634 |
"d": { "type": "string" } |
| 1635 |
}, |
| 1636 |
"additionalProperties": true |
| 1637 |
} |
| 1638 |
~~~ |
| 1639 |
|
| 1640 |
And the instance remained the same: |
| 1641 |
|
| 1642 |
~~~ json |
| 1643 |
{ "b": 3, "c": 3, "e": 3 } |
| 1644 |
~~~ |
| 1645 |
|
| 1646 |
Then the error indicators from evaluating the instance against the schema would |
| 1647 |
be: |
| 1648 |
|
| 1649 |
~~~ json |
| 1650 |
[ |
| 1651 |
{ "instancePath": "", |
| 1652 |
"schemaPath": "/properties/a" }, |
| 1653 |
{ "instancePath": "/b", |
| 1654 |
"schemaPath": "/properties/b/type" }, |
| 1655 |
{ "instancePath": "/c", |
| 1656 |
"schemaPath": "/optionalProperties/c/type" }, |
| 1657 |
] |
| 1658 |
~~~ |
| 1659 |
|
| 1660 |
These are the same errors as before, except the final error (associated with the |
| 1661 |
additional member named `e` in the instance) is no longer present. This is |
| 1662 |
because `additionalProperties: true` enables "allow additional properties" mode |
| 1663 |
on the schema. |
| 1664 |
|
| 1665 |
Finally, the schema: |
| 1666 |
|
| 1667 |
~~~ json |
| 1668 |
{ |
| 1669 |
"nullable": true, |
| 1670 |
"properties": { |
| 1671 |
"a": { "type": "string" }, |
| 1672 |
"b": { "type": "string" } |
| 1673 |
}, |
| 1674 |
"optionalProperties": { |
| 1675 |
"c": { "type": "string" }, |
| 1676 |
"d": { "type": "string" } |
| 1677 |
}, |
| 1678 |
"additionalProperties": true |
| 1679 |
} |
| 1680 |
~~~ |
| 1681 |
|
| 1682 |
accepts |
| 1683 |
|
| 1684 |
~~~ json |
| 1685 |
null |
| 1686 |
~~~ |
| 1687 |
|
| 1688 |
but rejects |
| 1689 |
|
| 1690 |
~~~ json |
| 1691 |
{ "b": 3, "c": 3, "e": 3 } |
| 1692 |
~~~ |
| 1693 |
|
| 1694 |
with the error indicators |
| 1695 |
|
| 1696 |
~~~ json |
| 1697 |
[ |
| 1698 |
{ "instancePath": "", |
| 1699 |
"schemaPath": "/properties/a" }, |
| 1700 |
{ "instancePath": "/b", |
| 1701 |
"schemaPath": "/properties/b/type" }, |
| 1702 |
{ "instancePath": "/c", |
| 1703 |
"schemaPath": "/optionalProperties/c/type" }, |
| 1704 |
] |
| 1705 |
~~~ |
| 1706 |
|
| 1707 |
### Values {#semantics-form-values} |
| 1708 |
|
| 1709 |
The `values` form is meant to describe instances that are JSON objects being |
| 1710 |
used as an associative array. The syntax of the `values` form is described in |
| 1711 |
{{syntax-form-values}}. |
| 1712 |
|
| 1713 |
If a schema is of the values form, then: |
| 1714 |
|
| 1715 |
- If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1716 |
and the instance is the JSON primitive value `null`, then the schema accepts |
| 1717 |
the instance. Otherwise: |
| 1718 |
|
| 1719 |
- Let *S* be the value of the schema member with the name `values`. The instance |
| 1720 |
is accepted if and only if all of the following are true: |
| 1721 |
|
| 1722 |
- The instance is an object. Otherwise, the error indicator for this case |
| 1723 |
shall have an `instancePath` pointing to the instance, and a `schemaPath` |
| 1724 |
pointing to the schema member with the name `values`. |
| 1725 |
|
| 1726 |
- If the instance is an object, then every member value of the instance must |
| 1727 |
be accepted by *S*. Otherwise, the error indicators for this case are the |
| 1728 |
union of all the error indicators arising from evaluating *S* against member |
| 1729 |
values of the instance. |
| 1730 |
|
| 1731 |
For example, the schema: |
| 1732 |
|
| 1733 |
~~~ json |
| 1734 |
{ |
| 1735 |
"values": { |
| 1736 |
"type": "float32" |
| 1737 |
} |
| 1738 |
} |
| 1739 |
~~~ |
| 1740 |
|
| 1741 |
accepts |
| 1742 |
|
| 1743 |
~~~ json |
| 1744 |
{} |
| 1745 |
~~~ |
| 1746 |
|
| 1747 |
and |
| 1748 |
|
| 1749 |
~~~ json |
| 1750 |
{"a": 1, "b": 2} |
| 1751 |
~~~ |
| 1752 |
|
| 1753 |
but rejects |
| 1754 |
|
| 1755 |
~~~ json |
| 1756 |
null |
| 1757 |
~~~ |
| 1758 |
|
| 1759 |
with the error indicator |
| 1760 |
|
| 1761 |
~~~ json |
| 1762 |
[{ "instancePath": "", "schemaPath": "/values" }] |
| 1763 |
~~~ |
| 1764 |
|
| 1765 |
and rejects |
| 1766 |
|
| 1767 |
~~~ json |
| 1768 |
{ "a": 1, "b": 2, "c": "foo", "d": 3, "e": "bar" } |
| 1769 |
~~~ |
| 1770 |
|
| 1771 |
with the error indicators |
| 1772 |
|
| 1773 |
~~~ json |
| 1774 |
[ |
| 1775 |
{ "instancePath": "/c", "schemaPath": "/values/type" }, |
| 1776 |
{ "instancePath": "/e", "schemaPath": "/values/type" } |
| 1777 |
] |
| 1778 |
~~~ |
| 1779 |
|
| 1780 |
The schema: |
| 1781 |
|
| 1782 |
~~~ json |
| 1783 |
{ |
| 1784 |
"nullable": true, |
| 1785 |
"values": { |
| 1786 |
"type": "float32" |
| 1787 |
} |
| 1788 |
} |
| 1789 |
~~~ |
| 1790 |
|
| 1791 |
accepts |
| 1792 |
|
| 1793 |
~~~ json |
| 1794 |
null |
| 1795 |
~~~ |
| 1796 |
|
| 1797 |
but rejects |
| 1798 |
|
| 1799 |
~~~ json |
| 1800 |
{ "a": 1, "b": 2, "c": "foo", "d": 3, "e": "bar" } |
| 1801 |
~~~ |
| 1802 |
|
| 1803 |
with the error indicators |
| 1804 |
|
| 1805 |
~~~ json |
| 1806 |
[ |
| 1807 |
{ "instancePath": "/c", "schemaPath": "/values/type" }, |
| 1808 |
{ "instancePath": "/e", "schemaPath": "/values/type" } |
| 1809 |
] |
| 1810 |
~~~ |
| 1811 |
|
| 1812 |
### Discriminator {#semantics-form-discriminator} |
| 1813 |
|
| 1814 |
The `discriminator` form is meant to describe JSON objects being used in a |
| 1815 |
fashion similar to a discriminated union construct in C-like languages. The |
| 1816 |
syntax of the `discriminator` form is described in |
| 1817 |
{{syntax-form-discriminator}}. |
| 1818 |
|
| 1819 |
When a schema is of the "discriminator" form, it validates: |
| 1820 |
|
| 1821 |
- That the instance is an object, |
| 1822 |
- That the instance has a particular "tag" property, |
| 1823 |
- That this "tag" property's value is a string within a set of valid values, and |
| 1824 |
- That the instance satisfies another schema, where this other schema is chosen |
| 1825 |
based on the value of the "tag" property. |
| 1826 |
|
| 1827 |
The behavior of the discriminator form is more complex than the other keywords. |
| 1828 |
Readers familiar with CDDL may find the final example in |
| 1829 |
{{comparison-with-cddl}} helpful in understanding its behavior. What follows in |
| 1830 |
this section is a description of the discriminator form's behavior, as well as |
| 1831 |
some examples. |
| 1832 |
|
| 1833 |
If a schema is of the "discriminator" form, then: |
| 1834 |
|
| 1835 |
- Let *D* be the schema member with the name `discriminator`. |
| 1836 |
- Let *M* be the schema member with the name `mapping`. |
| 1837 |
- Let *I* be the instance member whose name equals *D*'s value. *I* may, for |
| 1838 |
some rejected instances, not exist. |
| 1839 |
- Let *S* be the member of *M* whose name equals *I*'s value. *S* may, for some |
| 1840 |
rejected instances, not exist. |
| 1841 |
|
| 1842 |
If the schema has a member named `nullable` whose value is the boolean `true`, |
| 1843 |
and the instance is the JSON primitive value `null`, then the schema accepts the |
| 1844 |
instance. Otherwise the instance is accepted if and only if all of the following |
| 1845 |
are true: |
| 1846 |
|
| 1847 |
- The instance is an object. |
| 1848 |
|
| 1849 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1850 |
pointing to the instance, and a `schemaPath` pointing to *D*. |
| 1851 |
|
| 1852 |
- If the instance is a JSON object, then *I* must exist. |
| 1853 |
|
| 1854 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1855 |
pointing to the instance, and a `schemaPath` pointing to *D*. |
| 1856 |
|
| 1857 |
- If the instance is a JSON object and *I* exists, *I*'s value must be a string. |
| 1858 |
|
| 1859 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1860 |
pointing to *I*, and a `schemaPath` pointing to *D*. |
| 1861 |
|
| 1862 |
- If the instance is a JSON object and *I* exists and has a string value, then |
| 1863 |
*S* must exist. |
| 1864 |
|
| 1865 |
Otherwise, the error indicator for this case shall have an `instancePath` |
| 1866 |
pointing to *I*, and a `schemaPath` pointing to *M*. |
| 1867 |
|
| 1868 |
- If the instance is a JSON object, *I* exists, and *S* exists, then the |
| 1869 |
instance must satisfy *S*'s value. By {{syntax}}, *S*'s value must be a schema |
| 1870 |
of the properties form. Apply the "discriminator tag exemption" afforded in |
| 1871 |
{{semantics-form-props}} to *I* when evaluating whether the instance satisfies |
| 1872 |
*S*'s value. |
| 1873 |
|
| 1874 |
Otherwise, the error indicators for this case shall be error indicators from |
| 1875 |
evaluating *S*'s value against the instance, with the "discriminator tag |
| 1876 |
exemption" applied to *I*. |
| 1877 |
|
| 1878 |
The list items above are defined in a mutually exclusive way. For any given |
| 1879 |
instance and schema, exactly one of the list items above will apply. |
| 1880 |
|
| 1881 |
For example, the schema: |
| 1882 |
|
| 1883 |
~~~ json |
| 1884 |
{ |
| 1885 |
"discriminator": "version", |
| 1886 |
"mapping": { |
| 1887 |
"v1": { |
| 1888 |
"properties": { |
| 1889 |
"a": { "type": "float32" } |
| 1890 |
} |
| 1891 |
}, |
| 1892 |
"v2": { |
| 1893 |
"properties": { |
| 1894 |
"a": { "type": "string" } |
| 1895 |
} |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
~~~ |
| 1900 |
|
| 1901 |
rejects |
| 1902 |
|
| 1903 |
~~~ json |
| 1904 |
null |
| 1905 |
~~~ |
| 1906 |
|
| 1907 |
with the error indicator |
| 1908 |
|
| 1909 |
~~~ json |
| 1910 |
[{ "instancePath": "", "schemaPath": "/discriminator" }] |
| 1911 |
~~~ |
| 1912 |
|
| 1913 |
(This is the case of the instance not being an object.) |
| 1914 |
|
| 1915 |
Also rejected is |
| 1916 |
|
| 1917 |
~~~ json |
| 1918 |
{} |
| 1919 |
~~~ |
| 1920 |
|
| 1921 |
with the error indicator |
| 1922 |
|
| 1923 |
~~~ json |
| 1924 |
[{ "instancePath": "", "schemaPath": "/discriminator" }] |
| 1925 |
~~~ |
| 1926 |
|
| 1927 |
(This is the case of *I* not existing.) |
| 1928 |
|
| 1929 |
Also rejected is |
| 1930 |
|
| 1931 |
~~~ json |
| 1932 |
{ "version": 1 } |
| 1933 |
~~~ |
| 1934 |
|
| 1935 |
with the error indicator |
| 1936 |
|
| 1937 |
~~~ json |
| 1938 |
[ |
| 1939 |
{ |
| 1940 |
"instancePath": "/version", |
| 1941 |
"schemaPath": "/discriminator" |
| 1942 |
} |
| 1943 |
] |
| 1944 |
~~~ |
| 1945 |
|
| 1946 |
(This is the case of *I* existing, but not having a string value.) |
| 1947 |
|
| 1948 |
Also rejected is |
| 1949 |
|
| 1950 |
~~~ json |
| 1951 |
{ "version": "v3" } |
| 1952 |
~~~ |
| 1953 |
|
| 1954 |
with the error indicator |
| 1955 |
|
| 1956 |
~~~ json |
| 1957 |
[ |
| 1958 |
{ |
| 1959 |
"instancePath": "/version", |
| 1960 |
"schemaPath": "/mapping" |
| 1961 |
} |
| 1962 |
] |
| 1963 |
~~~ |
| 1964 |
|
| 1965 |
(This is the case of *I* existing and having a string value, but *S* not |
| 1966 |
existing.) |
| 1967 |
|
| 1968 |
Also rejected is |
| 1969 |
|
| 1970 |
~~~ json |
| 1971 |
{ "version": "v2", "a": 3 } |
| 1972 |
~~~ |
| 1973 |
|
| 1974 |
with the error indicator |
| 1975 |
|
| 1976 |
~~~ json |
| 1977 |
[ |
| 1978 |
{ |
| 1979 |
"instancePath": "/a", |
| 1980 |
"schemaPath": "/mapping/v2/properties/a/type" |
| 1981 |
} |
| 1982 |
] |
| 1983 |
~~~ |
| 1984 |
|
| 1985 |
(This is the case of *I* and *S* existing, but the instance not satisfying *S*'s |
| 1986 |
value.) |
| 1987 |
|
| 1988 |
Finally, the schema accepts |
| 1989 |
|
| 1990 |
~~~ json |
| 1991 |
{ "version": "v2", "a": "foo" } |
| 1992 |
~~~ |
| 1993 |
|
| 1994 |
This instance is accepted even though `version` is not mentioned by |
| 1995 |
`/mapping/v2/properties`; the "discriminator tag exemption" ensures that |
| 1996 |
`version` is not treated as an additional property when evaluating the instance |
| 1997 |
against *S*'s value. |
| 1998 |
|
| 1999 |
By contrast, consider the same schema, but with `nullable` being `true`. The |
| 2000 |
schema: |
| 2001 |
|
| 2002 |
~~~ json |
| 2003 |
{ |
| 2004 |
"nullable": true, |
| 2005 |
"discriminator": "version", |
| 2006 |
"mapping": { |
| 2007 |
"v1": { |
| 2008 |
"properties": { |
| 2009 |
"a": { "type": "float32" } |
| 2010 |
} |
| 2011 |
}, |
| 2012 |
"v2": { |
| 2013 |
"properties": { |
| 2014 |
"a": { "type": "string" } |
| 2015 |
} |
| 2016 |
} |
| 2017 |
} |
| 2018 |
} |
| 2019 |
~~~ |
| 2020 |
|
| 2021 |
accepts |
| 2022 |
|
| 2023 |
~~~ json |
| 2024 |
null |
| 2025 |
~~~ |
| 2026 |
|
| 2027 |
To further illustrate the discriminator form with examples, recall the JTD |
| 2028 |
schema in {{syntax-form-discriminator}}, reproduced here: |
| 2029 |
|
| 2030 |
~~~ json |
| 2031 |
{ |
| 2032 |
"discriminator": "event_type", |
| 2033 |
"mapping": { |
| 2034 |
"account_deleted": { |
| 2035 |
"properties": { |
| 2036 |
"account_id": { "type": "string" } |
| 2037 |
} |
| 2038 |
}, |
| 2039 |
"account_payment_plan_changed": { |
| 2040 |
"properties": { |
| 2041 |
"account_id": { "type": "string" }, |
| 2042 |
"payment_plan": { "enum": ["FREE", "PAID"] } |
| 2043 |
}, |
| 2044 |
"optionalProperties": { |
| 2045 |
"upgraded_by": { "type": "string" } |
| 2046 |
} |
| 2047 |
} |
| 2048 |
} |
| 2049 |
} |
| 2050 |
~~~ |
| 2051 |
|
| 2052 |
This schema accepts |
| 2053 |
|
| 2054 |
~~~ json |
| 2055 |
{ "event_type": "account_deleted", "account_id": "abc-123" } |
| 2056 |
~~~ |
| 2057 |
|
| 2058 |
and |
| 2059 |
|
| 2060 |
~~~ json |
| 2061 |
{ |
| 2062 |
"event_type": "account_payment_plan_changed", |
| 2063 |
"account_id": "abc-123", |
| 2064 |
"payment_plan": "PAID" |
| 2065 |
} |
| 2066 |
~~~ |
| 2067 |
|
| 2068 |
and |
| 2069 |
|
| 2070 |
~~~ json |
| 2071 |
{ |
| 2072 |
"event_type": "account_payment_plan_changed", |
| 2073 |
"account_id": "abc-123", |
| 2074 |
"payment_plan": "PAID", |
| 2075 |
"upgraded_by": "users/mkhwarizmi" |
| 2076 |
} |
| 2077 |
~~~ |
| 2078 |
|
| 2079 |
but rejects |
| 2080 |
|
| 2081 |
~~~ json |
| 2082 |
{} |
| 2083 |
~~~ |
| 2084 |
|
| 2085 |
with the error indicator |
| 2086 |
|
| 2087 |
~~~ json |
| 2088 |
[{ "instancePath": "", "schemaPath": "/discriminator" }] |
| 2089 |
~~~ |
| 2090 |
|
| 2091 |
and rejects |
| 2092 |
|
| 2093 |
~~~ json |
| 2094 |
{ "event_type": "some_other_event_type" } |
| 2095 |
~~~ |
| 2096 |
|
| 2097 |
with the error indicator |
| 2098 |
|
| 2099 |
~~~ json |
| 2100 |
[ |
| 2101 |
{ |
| 2102 |
"instancePath": "/event_type", |
| 2103 |
"schemaPath": "/mapping" |
| 2104 |
} |
| 2105 |
] |
| 2106 |
~~~ |
| 2107 |
|
| 2108 |
and rejects |
| 2109 |
|
| 2110 |
~~~ json |
| 2111 |
{ "event_type": "account_deleted" } |
| 2112 |
~~~ |
| 2113 |
|
| 2114 |
with the error indicator |
| 2115 |
|
| 2116 |
~~~ json |
| 2117 |
[{ |
| 2118 |
"instancePath": "", |
| 2119 |
"schemaPath": "/mapping/account_deleted/properties/account_id" |
| 2120 |
}] |
| 2121 |
~~~ |
| 2122 |
|
| 2123 |
and rejects |
| 2124 |
|
| 2125 |
~~~ json |
| 2126 |
{ |
| 2127 |
"event_type": "account_payment_plan_changed", |
| 2128 |
"account_id": "abc-123", |
| 2129 |
"payment_plan": "PAID", |
| 2130 |
"xxx": "asdf" |
| 2131 |
} |
| 2132 |
~~~ |
| 2133 |
|
| 2134 |
with the error indicator |
| 2135 |
|
| 2136 |
~~~ json |
| 2137 |
[{ |
| 2138 |
"instancePath": "/xxx", |
| 2139 |
"schemaPath": "/mapping/account_payment_plan_changed" |
| 2140 |
}] |
| 2141 |
~~~ |
| 2142 |
|
| 2143 |
# IANA Considerations |
| 2144 |
|
| 2145 |
No IANA considerations. |
| 2146 |
|
| 2147 |
# Security Considerations |
| 2148 |
|
| 2149 |
Implementations of JTD will necessarily be manipulating JSON data. Therefore, |
| 2150 |
the security considerations of {{RFC8259}} are all relevant here. |
| 2151 |
|
| 2152 |
Implementations which evaluate user-inputted schemas SHOULD implement mechanisms |
| 2153 |
to detect, and abort, circular references which might cause a naive |
| 2154 |
implementation to go into an infinite loop. Without such mechanisms, |
| 2155 |
implementations may be vulnerable to denial-of-service attacks. |
| 2156 |
|
| 2157 |
--- back |
| 2158 |
|
| 2159 |
# Rationale for Omitted Features {#other-considerations} |
| 2160 |
|
| 2161 |
This appendix is not normative. |
| 2162 |
|
| 2163 |
This section describes possible features which are intentionally left out of |
| 2164 |
JSON Type Definition, and justifies why these features are omitted. |
| 2165 |
|
| 2166 |
## Support for 64-bit Numbers {#other-considerations-int64} |
| 2167 |
|
| 2168 |
This document does not allow `int64` or `uint64` as values for the JTD `type` |
| 2169 |
keyword (see {{syntax-form-type}} and {{semantics-form-type}}). Such |
| 2170 |
hypothetical `int64` or `uint64` types would behave like `int32` or `uint32` |
| 2171 |
(respectively), but with the range of values associated with 64-bit instead of |
| 2172 |
32-bit integers, that is: |
| 2173 |
|
| 2174 |
- `int64` would accept numbers between -(2\*\*63) and (2\*\*63)-1 |
| 2175 |
- `uint64` would accept numbers between 0 and (2**64)-1 |
| 2176 |
|
| 2177 |
Users of `int64` and `uint64` would likely expect that the full range of signed |
| 2178 |
or unsigned 64-bit integers could interoperably be transmitted as JSON without |
| 2179 |
loss of precision. But this assumption is likely to be incorrect, for the |
| 2180 |
reasons given in Section 2.2 of {{RFC7493}}. |
| 2181 |
|
| 2182 |
`int64` and `uint64` likely would have led users to falsely assume that the full |
| 2183 |
range of 64-bit integers can be interoperably processed as JSON without loss of |
| 2184 |
precision. To avoid leading users astray, JTD omits `int64` and `uint64`. |
| 2185 |
|
| 2186 |
## Support for Non-Root Definitions |
| 2187 |
|
| 2188 |
This document disallows the `definitions` keyword from appearing outside of root |
| 2189 |
schemas (see {{cddl-schema}}). Conceivably, this document could have instead |
| 2190 |
allowed `definitions` to appear on any schema, even non-root ones. Under this |
| 2191 |
alternative design, `ref`s would resolve to a definition in the "nearest" (i.e., |
| 2192 |
most nested) schema which both contained the `ref` and which had a |
| 2193 |
suitably-named `definitions` member. |
| 2194 |
|
| 2195 |
For instance, under this alternative approach, one could define schemas like the |
| 2196 |
one in {{hypothetical-ref}}: |
| 2197 |
|
| 2198 |
~~~ json |
| 2199 |
{ |
| 2200 |
"properties": { |
| 2201 |
"foo": { |
| 2202 |
"definitions": { |
| 2203 |
"user": { "properties": { "user_id": {"type": "string" }}} |
| 2204 |
}, |
| 2205 |
"ref": "user" |
| 2206 |
}, |
| 2207 |
"bar": { |
| 2208 |
"definitions": { |
| 2209 |
"user": { "properties": { "user_id": {"type": "string" }}} |
| 2210 |
}, |
| 2211 |
"ref": "user" |
| 2212 |
}, |
| 2213 |
"baz": { |
| 2214 |
"definitions": { |
| 2215 |
"user": { "properties": { "userId": {"type": "string" }}} |
| 2216 |
}, |
| 2217 |
"ref": "user" |
| 2218 |
} |
| 2219 |
} |
| 2220 |
} |
| 2221 |
~~~ |
| 2222 |
{: #hypothetical-ref title="A hypothetical schema had this document permitted |
| 2223 |
non-root definitions. This is not a correct JTD schema."} |
| 2224 |
|
| 2225 |
If schemas like that in {{hypothetical-ref}} were permitted, code generation |
| 2226 |
from JTD schemas would be more difficult, and the generated code would be less |
| 2227 |
useful. |
| 2228 |
|
| 2229 |
Code generation would be more difficult because it would force code generators |
| 2230 |
to implement a name mangling scheme for types generated from definitions. This |
| 2231 |
additional difficulty is not immense, but adds complexity to an otherwise |
| 2232 |
relatively trivial task. |
| 2233 |
|
| 2234 |
Generated code would be less useful because generated, mangled struct names are |
| 2235 |
less pithy than human-defined struct names. For instance, the `user` definitions |
| 2236 |
in {{hypothetical-ref}} might have been generated into types named |
| 2237 |
`PropertiesFooUser`, `PropertiesBarUser`, and `PropertiesBazUser`; obtuse names |
| 2238 |
like these are less useful to human-written code than names like `User`. |
| 2239 |
|
| 2240 |
Furthermore, even though `PropertiesFooUser` and `PropertiesBarUser` would be |
| 2241 |
essentially identical, they would not be interchangeable in many |
| 2242 |
statically-typed programming languages. A code generator could attempt to |
| 2243 |
circumvent this by deduplicating identical definitions, but then the user might |
| 2244 |
be confused as to why the subtly distinct `PropertiesBazUser`, defined from a |
| 2245 |
schema allowing a property named `userId` (not `user_id`), was not deduplicated. |
| 2246 |
|
| 2247 |
Because there seem to be implementation and usability challenges associated with |
| 2248 |
non-root definitions, and because it would be easier to later amend JTD to |
| 2249 |
permit for non-root definitions than to later amend JTD to prohibit them, this |
| 2250 |
document does not permit non-root definitions in JTD schemas. |
| 2251 |
|
| 2252 |
# Comparison with CDDL {#comparison-with-cddl} |
| 2253 |
|
| 2254 |
This appendix is not normative. |
| 2255 |
|
| 2256 |
To aid the reader familiar with CDDL, this section illustrates how JTD works by |
| 2257 |
presenting JTD schemas and CDDL schemas which accept and reject the same |
| 2258 |
instances. |
| 2259 |
|
| 2260 |
The JTD schema: |
| 2261 |
|
| 2262 |
~~~ json |
| 2263 |
{} |
| 2264 |
~~~ |
| 2265 |
|
| 2266 |
accepts the same instances as the CDDL rule: |
| 2267 |
|
| 2268 |
~~~ cddl |
| 2269 |
root = any |
| 2270 |
~~~ |
| 2271 |
|
| 2272 |
The JTD schema: |
| 2273 |
|
| 2274 |
~~~ json |
| 2275 |
{ |
| 2276 |
"definitions": { |
| 2277 |
"a": { "elements": { "ref": "b" }}, |
| 2278 |
"b": { "type": "float32" } |
| 2279 |
}, |
| 2280 |
"elements": { |
| 2281 |
"ref": "a" |
| 2282 |
} |
| 2283 |
} |
| 2284 |
~~~ |
| 2285 |
|
| 2286 |
accepts the same instances as the CDDL rule: |
| 2287 |
|
| 2288 |
~~~ cddl |
| 2289 |
root = [* a] |
| 2290 |
|
| 2291 |
a = [* b] |
| 2292 |
b = number |
| 2293 |
~~~ |
| 2294 |
|
| 2295 |
The JTD schema: |
| 2296 |
|
| 2297 |
~~~ json |
| 2298 |
{ "enum": ["PENDING", "DONE", "CANCELED"]} |
| 2299 |
~~~ |
| 2300 |
|
| 2301 |
accepts the same instances as the CDDL rule: |
| 2302 |
|
| 2303 |
~~~ cddl |
| 2304 |
root = "PENDING" / "DONE" / "CANCELED" |
| 2305 |
~~~ |
| 2306 |
|
| 2307 |
The JTD schema: |
| 2308 |
|
| 2309 |
~~~ json |
| 2310 |
{"type": "boolean"} |
| 2311 |
~~~ |
| 2312 |
|
| 2313 |
accepts the same instances as the CDDL rule: |
| 2314 |
|
| 2315 |
~~~ cddl |
| 2316 |
root = bool |
| 2317 |
~~~ |
| 2318 |
|
| 2319 |
The JTD schemas: |
| 2320 |
|
| 2321 |
~~~ json |
| 2322 |
{"type": "float32"} |
| 2323 |
~~~ |
| 2324 |
|
| 2325 |
and |
| 2326 |
|
| 2327 |
~~~ json |
| 2328 |
{"type": "float64"} |
| 2329 |
~~~ |
| 2330 |
|
| 2331 |
both accept the same instances as the CDDL rule: |
| 2332 |
|
| 2333 |
~~~ cddl |
| 2334 |
root = number |
| 2335 |
~~~ |
| 2336 |
|
| 2337 |
The JTD schema: |
| 2338 |
|
| 2339 |
~~~ json |
| 2340 |
{"type": "string"} |
| 2341 |
~~~ |
| 2342 |
|
| 2343 |
accepts the same instances as the CDDL rule: |
| 2344 |
|
| 2345 |
~~~ cddl |
| 2346 |
root = tstr |
| 2347 |
~~~ |
| 2348 |
|
| 2349 |
The JTD schema: |
| 2350 |
|
| 2351 |
~~~ json |
| 2352 |
{"type": "timestamp"} |
| 2353 |
~~~ |
| 2354 |
|
| 2355 |
accepts the same instances as the CDDL rule: |
| 2356 |
|
| 2357 |
~~~ cddl |
| 2358 |
root = tdate |
| 2359 |
~~~ |
| 2360 |
|
| 2361 |
The JTD schema: |
| 2362 |
|
| 2363 |
~~~ json |
| 2364 |
{ "elements": { "type": "float32" }} |
| 2365 |
~~~ |
| 2366 |
|
| 2367 |
accepts the same instances as the CDDL rule: |
| 2368 |
|
| 2369 |
~~~ cddl |
| 2370 |
root = [* number] |
| 2371 |
~~~ |
| 2372 |
|
| 2373 |
The JTD schema: |
| 2374 |
|
| 2375 |
~~~ json |
| 2376 |
{ |
| 2377 |
"properties": { |
| 2378 |
"a": { "type": "boolean" }, |
| 2379 |
"b": { "type": "float32" } |
| 2380 |
}, |
| 2381 |
"optionalProperties": { |
| 2382 |
"c": { "type": "string" }, |
| 2383 |
"d": { "type": "timestamp" } |
| 2384 |
} |
| 2385 |
} |
| 2386 |
~~~ |
| 2387 |
|
| 2388 |
accepts the same instances as the CDDL rule: |
| 2389 |
|
| 2390 |
~~~ cddl |
| 2391 |
root = { a: bool, b: number, ? c: tstr, ? d: tdate } |
| 2392 |
~~~ |
| 2393 |
|
| 2394 |
The JTD schema: |
| 2395 |
|
| 2396 |
~~~ json |
| 2397 |
{ "values": { "type": "float32" }} |
| 2398 |
~~~ |
| 2399 |
|
| 2400 |
accepts the same instances as the CDDL rule: |
| 2401 |
|
| 2402 |
~~~ cddl |
| 2403 |
root = { * tstr => number } |
| 2404 |
~~~ |
| 2405 |
|
| 2406 |
Finally, the JTD schema: |
| 2407 |
|
| 2408 |
~~~ json |
| 2409 |
{ |
| 2410 |
"discriminator": "a", |
| 2411 |
"mapping": { |
| 2412 |
"foo": { |
| 2413 |
"properties": { |
| 2414 |
"b": { "type": "float32" } |
| 2415 |
} |
| 2416 |
}, |
| 2417 |
"bar": { |
| 2418 |
"properties": { |
| 2419 |
"b": { "type": "string" } |
| 2420 |
} |
| 2421 |
} |
| 2422 |
} |
| 2423 |
} |
| 2424 |
~~~ |
| 2425 |
|
| 2426 |
accepts the same instances as the CDDL rule: |
| 2427 |
|
| 2428 |
~~~ cddl |
| 2429 |
root = { a: "foo", b: number } / { a: "bar", b: tstr } |
| 2430 |
~~~ |
| 2431 |
|
| 2432 |
# Example {#examples} |
| 2433 |
|
| 2434 |
This appendix is not normative. |
| 2435 |
|
| 2436 |
As a demonstration of JTD, in {{JTD-reputation-object}} is a JTD schema |
| 2437 |
closely equivalent to the plain-English definition `reputation-object` described |
| 2438 |
in Section 6.2.2 of {{RFC7071}}: |
| 2439 |
|
| 2440 |
~~~ json |
| 2441 |
{ |
| 2442 |
"properties": { |
| 2443 |
"application": { "type": "string" }, |
| 2444 |
"reputons": { |
| 2445 |
"elements": { |
| 2446 |
"additionalProperties": true, |
| 2447 |
"properties": { |
| 2448 |
"rater": { "type": "string" }, |
| 2449 |
"assertion": { "type": "string" }, |
| 2450 |
"rated": { "type": "string" }, |
| 2451 |
"rating": { "type": "float32" }, |
| 2452 |
}, |
| 2453 |
"optionalProperties": { |
| 2454 |
"confidence": { "type": "float32" }, |
| 2455 |
"normal-rating": { "type": "float32" }, |
| 2456 |
"sample-size": { "type": "float64" }, |
| 2457 |
"generated": { "type": "float64" }, |
| 2458 |
"expires": { "type": "float64" } |
| 2459 |
} |
| 2460 |
} |
| 2461 |
} |
| 2462 |
} |
| 2463 |
} |
| 2464 |
~~~ |
| 2465 |
{: #JTD-reputation-object title="A JTD schema describing \"reputation-object\" |
| 2466 |
from Section 6.6.2 of [RFC7071]"} |
| 2467 |
|
| 2468 |
This schema does not enforce the requirement that `sample-size`, `generated`, |
| 2469 |
and `expires` be unbounded positive integers. It does not express the limitation |
| 2470 |
that `rating`, `confidence`, and `normal-rating` should not have more than three |
| 2471 |
decimal places of precision. |
| 2472 |
|
| 2473 |
The example in {{JTD-reputation-object}} can be compared against the equivalent |
| 2474 |
example in Appendix H of {{RFC8610}}. |
| 2475 |
|
| 2476 |
# Acknowledgments |
| 2477 |
{: numbered="no"} |
| 2478 |
|
| 2479 |
Carsten Bormann provided lots of useful guidance and feedback on JTD's design |
| 2480 |
and the structure of this document. |
| 2481 |
|
| 2482 |
Evgeny Poberezkin suggested the addition of `nullable`, and thoroughly vetted |
| 2483 |
this document for mistakes and opportunities for simplification. |
| 2484 |
|
| 2485 |
Tim Bray suggested the current `ref` model, and the addition of `enum`. Anders |
| 2486 |
Rundgren suggested extending `type` to have more support for numerical types. |
| 2487 |
James Manger suggested additional clarifying examples of how integer types work. |
| 2488 |
Adrian Farrel suggested many improvements to help make this document clearer. |
| 2489 |
|
| 2490 |
Members of the IETF JSON mailing list -- in particular, Pete Cordell, Phillip |
| 2491 |
Hallam-Baker, Nico Williams, John Cowan, Rob Sayre, and Erik Wilde -- provided |
| 2492 |
lots of useful feedback. |
| 2493 |
|
| 2494 |
OpenAPI's `discriminator` object {{OPENAPI}} inspired the `discriminator` form. |
| 2495 |
{{I-D.handrews-json-schema}} influenced various parts of JTD's early design. |