Skip to content

Commit 1bd77b4

Browse files
committed
Annotated enums and extended validation
This tidies up the increasingly long Schema Object section and adds explanations of what I've been calling "extended validation", including validating `readOnly` and `writeOnly`, and also using a `oneOf` or `anyOf` with `const` plus annotations for enumerated types with additional information. There are many OAS issues/discussions where we have recommended these techniques, so it makes sense to include them in 3.1.1 where draft 2020-12's formal collection of annotations enables tools to build support for them.
1 parent 204de0c commit 1bd77b4

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

versions/3.1.1.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,29 @@ JSON Schema implementations MAY choose to treat keywords defined by the OpenAPI
27582758

27592759
This object MAY be extended with [Specification Extensions](#specification-extensions), though as noted, additional properties MAY omit the `x-` prefix within this object.
27602760

2761+
##### Extended Validation with Annotations
2762+
2763+
JSON Schema draft 2020-12 supports [collecting annotations](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-7.7.1), including [treating unrecognized keywords as annotations](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-6.5).
2764+
OAS implementations MAY use such annotations, including [extensions](https://spec.openapis.org/registry/extension/) not recognized as part of a declared JSON Schema vocabulary, as the basis for further validation.
2765+
Note that JSON Schema draft 2020-12 does not require an `x-` prefix for extensions.
2766+
2767+
###### Non-validating constraint keywords
2768+
2769+
The [`format` keyword (when using default format-annotation vocabulary)](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.2.1) and the [`contentMediaType`, `contentEncoding`, and `contentSchema` keywords](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-8.2) define constraints on the data, but are treated as annotations instead of being validated directly.
2770+
Extended validation is one way that these constraints MAY be enforced.
2771+
2772+
###### Validating `readOnly` and `writeOnly`
2773+
2774+
The `readOnly` and `writeOnly` keywords are annotations, as JSON Schema is not aware of how the data it is validating is being used.
2775+
Validation of these keywords MAY be done by checking the annotation, the read or write direction, and (if relevant) the current value of the field.
2776+
[JSON Schema Validation draft 2020-12 §9.4](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-9.4) defines the expectations of these keywords, including that resource (described as the "owning authority") MAY either ignore a `readOnly` field or treat it as an error.
2777+
2778+
An example of where ignoring a "written" `readOnly` field might be appropriate is a PUT request where the field is included but the value has not been changed, since the alternative of leaving out the field would result in the field's deletion per [[RFC7231]].
2779+
2780+
Note that the behavior of `readOnly` in particular differs from that specified by version 3.0 of this specification.
2781+
2782+
##### Data Modeling Techniques
2783+
27612784
###### Composition and Inheritance (Polymorphism)
27622785

27632786
The OpenAPI Specification allows combining and extending model definitions using the `allOf` keyword of JSON Schema, in effect offering model composition.
@@ -2781,12 +2804,18 @@ Implementations MAY support defining generic or template data structures using J
27812804

27822805
An example is included in the "Schema Object Examples" section below, and further information can be found on the Learn OpenAPI site's ["Dynamic References"](https://learn.openapis.org/referencing/dynamic.html) page.
27832806

2807+
###### Annotated Enumerations
2808+
2809+
The Schema Object's `enum` keyword does not allow associating descriptions or other information with individual values.
2810+
2811+
Implementations MAY support recognizing a `oneOf` or `anyOf` where each subschema in the keyword's array consists of a `const` keyword and annotations such as `title` or `description` as an enumerated type with additional information. The exact behavior of this pattern beyond what is required by JSON Schema is implementation-defined.
2812+
27842813
###### XML Modeling
27852814

27862815
The [xml](#schema-xml) field allows extra definitions when translating the JSON definition to XML.
27872816
The [XML Object](#xml-object) contains additional information about the available options.
27882817

2789-
###### Specifying Schema Dialects
2818+
##### Specifying Schema Dialects
27902819

27912820
It is important for tooling to be able to determine which dialect or meta-schema any given resource wishes to be processed with: JSON Schema Core, JSON Schema Validation, OpenAPI Schema dialect, or some custom meta-schema.
27922821

@@ -2886,6 +2915,32 @@ additionalProperties:
28862915
$ref: '#/components/schemas/ComplexModel'
28872916
```
28882917

2918+
###### Model with Annotated Enumeration
2919+
2920+
```json
2921+
{
2922+
"oneOf": [{
2923+
"const": "RGB",
2924+
"title": "Red, Green, Blue",
2925+
"description": "Specify colors with the red, green, and blue additive color model"
2926+
}, {
2927+
"const": "CMYK",
2928+
"title": "Cyan, Magenta, Yellow, Black",
2929+
"description": "Specify colors with the cyan, magenta, yellow, and black subtractive color model"
2930+
}]
2931+
}
2932+
```
2933+
2934+
```yaml
2935+
oneOf:
2936+
- const: rgb
2937+
title: Red, Green, Blue
2938+
description: Specify colors with the red, green, and blue additive color model
2939+
- const: cmyk
2940+
title: Cyan, Magenta, Yellow, Black
2941+
description: Specify colors with the cyan, magenta, yellow, and black subtractive color model
2942+
```
2943+
28892944
###### Model with Example
28902945

28912946
```json

0 commit comments

Comments
 (0)