Skip to content

Commit f530c12

Browse files
committed
Improve examples
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent f13cf9b commit f530c12

1 file changed

Lines changed: 42 additions & 66 deletions

File tree

content/2020-12/meta-data/examples.markdown

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -30,93 +30,69 @@ related:
3030
keyword: deprecated
3131
---
3232

33-
The `examples` keyword is used to provide a list of example instances associated with a particular schema that should ideally validate against the schema. These examples serve to illustrate the intended structure and constraints defined by the schema. While these examples are not used for validation purposes, they are helpful in providing sample valid instances against the schema they are defined in.
3433

35-
_**Note:** While it is recommended that the examples validate against the subschema they are defined in, this requirement is not strictly enforced._
34+
The `examples` keyword declares a set of example instances for a schema or any
35+
of its subschemas, typically for documentation purposes. This keyword does not
36+
affect validation, but the evaluator will collect its set of values as an
37+
annotation.
3638

37-
* Used to demonstrate how data should conform to the schema.
38-
* `examples` does not affect data validation but serves as an informative annotation.
39+
{{<common-pitfall>}}
3940

40-
## Examples
41+
Meta-schema validation will not check that the examples you declare are
42+
actually valid against their respective schemas, as JSON Schema does not offer
43+
a mechanism for meta-schemas to declare that instances validate against parts
44+
of the same instance being evaluated. As a consequence, it is not rare for
45+
schemas to declare invalid examples that go undetected for a long time.
4146

42-
{{<schema `Schema with 'examples' keyword`>}}
43-
{
44-
"$schema": "https://json-schema.org/draft/2020-12/schema",
45-
"examples": [ "foo", "bar", "Doe" ],
46-
"type": "string"
47-
}
48-
{{</schema>}}
47+
It is recommended to use the [`jsonschema
48+
lint`](https://github.com/sourcemeta/jsonschema/blob/main/docs/lint.markdown)
49+
command, as this linter performs further checks to detect many corner cases,
50+
including this one.
4951

50-
{{<instance-pass `An instance with a string value is valid`>}}
51-
"John"
52-
{{</instance-pass>}}
52+
{{</common-pitfall>}}
5353

54-
{{<instance-annotation>}}
55-
{ "keyword": "/examples", "instance": "", "value": [ "foo", "bar", "Doe" ] }
56-
{{</instance-annotation>}}
54+
{{<metaschema-check-type `array`>}}
55+
56+
## Examples
5757

58-
{{<schema `Schema with logical operators`>}}
58+
{{<schema `A schema that describes name and age properties and declares top level and nested valid examples`>}}
5959
{
6060
"$schema": "https://json-schema.org/draft/2020-12/schema",
61-
"if": {
62-
"properties": {
63-
"foo": { "const": "foo" }
64-
}
65-
},
66-
"then": {
67-
"properties": {
68-
"bar": {
69-
"type": "array",
70-
"examples": [ [ "foo" ], [ "bar", "baz" ] ]
71-
}
72-
}
73-
},
74-
"else": {
75-
"properties": {
76-
"bar": {
77-
"type": "boolean",
78-
"examples": [ false, true ]
79-
}
61+
"examples": [
62+
{ "name": "John Doe", "age": 23 }
63+
],
64+
"properties": {
65+
"name": {
66+
"type": "string",
67+
"examples": [ "John Doe", "Jane Doe" ]
68+
},
69+
"age": {
70+
"type": "integer",
71+
"examples": [ 1, 18, 55 ]
8072
}
8173
}
8274
}
8375
{{</schema>}}
8476

85-
{{<instance-pass>}}
86-
{ "foo": "foo", "bar": [ "bar" ] }
77+
{{<instance-pass `An object value that defines name and age is valid and the corresponding annotations are emitted`>}}
78+
{ "name": "Juan Cruz Viotti", "age": 30 }
8779
{{</instance-pass>}}
8880

8981
{{<instance-annotation>}}
90-
{ "keyword": "/then/properties/bar/examples", "instance": "/bar", "value": [ [ "foo" ], [ "bar", "baz" ] ] }
82+
{ "keyword": "/examples", "instance": "", "value": [ { "name": "John Doe", "age": 23 } ] }
83+
{ "keyword": "/properties/name/examples", "instance": "/name", "value": [ "John Doe", "Jane Doe" ] }
84+
{ "keyword": "/properties/age/examples", "instance": "/age", "value": [ 1, 18, 55 ] }
9185
{{</instance-annotation>}}
9286

93-
{{<instance-pass>}}
94-
{ "foo": "not foo", "bar": true }
87+
{{<instance-pass `An object value that omits some properties is valid and only the corresponding annotations are emitted`>}}
88+
{ "age": 50 }
9589
{{</instance-pass>}}
9690

9791
{{<instance-annotation>}}
98-
{ "keyword": "/else/properties/bar/examples", "instance": "/bar", "value": [ false, true ] }
92+
{ "keyword": "/examples", "instance": "", "value": [ { "name": "John Doe", "age": 23 } ] }
93+
{ "keyword": "/properties/age/examples", "instance": "/age", "value": [ 1, 18, 55 ] }
9994
{{</instance-annotation>}}
10095

101-
{{<schema `Schema with multiple annotations for the same instance`>}}
102-
{
103-
"$schema": "https://json-schema.org/draft/2020-12/schema",
104-
"examples": [ "John", "Karl" ],
105-
"$ref": "#/$defs/name",
106-
"$defs": {
107-
"name": {
108-
"examples": [ "John", "Karl" ],
109-
"type": "string"
110-
}
111-
}
112-
}
113-
{{</schema>}}
114-
115-
{{<instance-pass>}}
116-
"John Doe"
117-
{{</instance-pass>}}
118-
119-
{{<instance-annotation>}}
120-
{ "keyword": "/examples", "instance": "", "value": [ "John", "Karl" ] }
121-
{ "keyword": "/$ref/examples", "instance": "", "value": [ "John", "Karl" ] }
122-
{{</instance-annotation>}}
96+
{{<instance-fail `A value that does not match the schema is invalid and no annotations are emitted`>}}
97+
{ "name": 1, "age": true }
98+
{{</instance-fail>}}

0 commit comments

Comments
 (0)