Skip to content

Commit 679d2b0

Browse files
committed
Improve the documentation for required
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1a7b616 commit 679d2b0

1 file changed

Lines changed: 50 additions & 75 deletions

File tree

content/2020-12/validation/required.markdown

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,110 +24,85 @@ related:
2424
keyword: minProperties
2525
---
2626

27-
The `required` keyword is used to specify which properties must be present within an object instance.
27+
The `required` keyword restricts object instances to define the given set of properties.
28+
29+
{{<common-pitfall>}} The presence of this keyword does not depend on the
30+
presence of the [`properties`]({{< ref "2020-12/applicator/properties" >}})
31+
keyword. The `required` keyword mandates that certain properties are present
32+
(independently of their value), while the [`properties`]({{< ref
33+
"2020-12/applicator/properties" >}}) keyword describes the value of such
34+
properties when present.{{</common-pitfall>}}
35+
36+
{{<constraint-warning `object`>}}
2837

2938
## Examples
3039

31-
{{<schema `Schema with the 'required' keyword`>}}
40+
{{<schema `A schema that constrains object instances to define certain properties`>}}
3241
{
3342
"$schema": "https://json-schema.org/draft/2020-12/schema",
34-
"type": "object",
35-
"required": [ "foo" ]
43+
"required": [ "foo", "bar", "baz" ]
3644
}
3745
{{</schema>}}
3846

39-
{{<instance-pass `An instance with all the required properties is valid`>}}
40-
{ "foo": "bar" }
47+
{{<instance-pass `An object value that defines the required properties to any values is valid`>}}
48+
{ "foo": 1, "bar": 2, "baz": 3 }
4149
{{</instance-pass>}}
4250

43-
{{<instance-fail `An instance with missing required properties is invalid`>}}
44-
{ "bar": false }
51+
{{<instance-pass `An object value that defines a superset of the required properties is valid`>}}
52+
{ "foo": 1, "bar": 2, "baz": 3, "extra": true }
53+
{{</instance-pass>}}
54+
55+
{{<instance-fail `An object value that only defines a subset of the required properties is invalid`>}}
56+
{ "foo": 1, "bar": 2, "extra": true }
4557
{{</instance-fail>}}
4658

47-
{{<instance-pass `An instance with all the required properties is valid`>}}
48-
{ "foo": [ "bar" ], "baz": 13 }
59+
{{<instance-fail `An empty object is invalid`>}}
60+
{}
61+
{{</instance-fail>}}
62+
63+
{{<instance-pass `A non-object value is valid`>}}
64+
"Hello World"
4965
{{</instance-pass>}}
50-
* _It is important to note that when the required properties are not defined in the `properties`, then the only requirement to make the instance valid is to have those properties present in the instance irrespective of their value's datatype._
5166

52-
{{<schema `Schema with the 'required' keyword`>}}
67+
{{<schema `A schema that constrains object instances to define certain properties and to describe their value`>}}
5368
{
5469
"$schema": "https://json-schema.org/draft/2020-12/schema",
55-
"type": "object",
70+
"required": [ "name", "age" ],
5671
"properties": {
5772
"name": { "type": "string" },
5873
"age": { "type": "integer" }
59-
},
60-
"required": [ "name", "age" ]
74+
}
6175
}
6276
{{</schema>}}
6377

64-
{{<instance-pass `An instance with all the required properties is valid`>}}
65-
{ "name": "John", "age": 65 }
78+
{{<instance-pass `An object value that defines the required properties and matches their definition is valid`>}}
79+
{ "name": "John Doe", "age": 30 }
6680
{{</instance-pass>}}
6781

68-
{{<instance-fail `An instance with missing required properties is invalid`>}}
69-
{ "name": "Doe" }
70-
{{</instance-fail>}}
82+
{{<instance-annotation>}}
83+
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
84+
{{</instance-annotation>}}
7185

72-
{{<instance-fail `The value of 'age' property must be an integer`>}}
73-
{ "name": "John Doe", "age": "48" }
74-
{{</instance-fail>}}
86+
{{<instance-pass `An object value that defines a superset of the required properties and matches their definition is valid`>}}
87+
{ "name": "John Doe", "age": 30, "extra": true }
88+
{{</instance-pass>}}
7589

76-
{{<schema `Schema with the 'required' keyword in nested subschemas`>}}
77-
{
78-
"$schema": "https://json-schema.org/draft/2020-12/schema",
79-
"type": "object",
80-
"properties": {
81-
"name": { "type": "string" },
82-
"address": {
83-
"type": "object",
84-
"properties": {
85-
"city": { "type": "string" },
86-
"country": { "type": "string" }
87-
},
88-
"required": [ "city", "country" ]
89-
}
90-
},
91-
"required": [ "address" ]
92-
}
93-
{{</schema>}}
90+
{{<instance-annotation>}}
91+
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
92+
{{</instance-annotation>}}
9493

95-
{{<instance-pass `An instance with all the required properties is valid`>}}
96-
{
97-
"name": "John",
98-
"address": {
99-
"city": "New York",
100-
"country": "USA"
101-
}
102-
}
103-
{{</instance-pass>}}
94+
{{<instance-fail `An object value that only defines a subset of the required properties and matches their definition is invalid`>}}
95+
{ "name": "John Doe" }
96+
{{</instance-fail>}}
10497

105-
{{<instance-fail `'name' property is missing in the root object`>}}
106-
{
107-
"address": {
108-
"city": "Hyderabad",
109-
"country": "India"
110-
}
111-
}
98+
{{<instance-fail `An object value that defines the required properties but does not match their definition is invalid`>}}
99+
{ "name": 123, "age": "foo" }
112100
{{</instance-fail>}}
113101

114-
{{<instance-fail `'country' property is missing in the nested object`>}}
115-
{
116-
"name": "Doe",
117-
"address": {
118-
"city": "Dallas"
119-
}
120-
}
102+
{{<instance-fail `An empty object is invalid`>}}
103+
{}
121104
{{</instance-fail>}}
122105

123-
{{<schema>}}
124-
{
125-
"$schema": "https://json-schema.org/draft/2020-12/schema",
126-
"type": "object",
127-
"properties": {
128-
"name": { "type": "string" }
129-
},
130-
"required": [ "name", "age", "name" ]
131-
}
132-
// Schema with duplicate items in the required list is invalid.
133-
{{</schema>}}
106+
{{<instance-pass `A non-object value is valid`>}}
107+
"Hello World"
108+
{{</instance-pass>}}

0 commit comments

Comments
 (0)