@@ -40,3 +40,140 @@ related:
4040 - vocabulary : applicator
4141 keyword : unevaluatedProperties
4242---
43+
44+ The ` additionalProperties ` keyword restricts object instance properties not
45+ described by the _ sibling_ [ ` properties ` ] ({{< ref
46+ "2019-09/applicator/properties"
47+ > }}) and [ ` patternProperties ` ] ({{< ref "2019-09/applicator/patternproperties"
48+ > }}) keywords (if any), to validate against the given subschema. Information
49+ about the properties that this keyword was evaluated for is reported using
50+ annotations.
51+
52+ {{<common-pitfall >}}The use of the [ ` properties ` ] ({{< ref
53+ "2019-09/applicator/properties" >}}) keyword ** does not prevent the presence of
54+ other properties** in the object instance and ** does not enforce the presence
55+ of the declared properties** . In other words, additional data that is not
56+ explicitly prohibited is permitted by default. This is intended behaviour to
57+ ease schema evolution (open schemas are backwards compatible by default) and to
58+ enable highly-expressive constraint-driven schemas.
59+
60+ If you want to restrict instances to only contain the properties you declared,
61+ you must set this keyword to the boolean schema ` false ` , and if you want to
62+ enforce the presence of certain properties, you must use the [ ` required ` ] ({{<
63+ ref "2019-09/validation/required" >}}) keyword accordingly.
64+ {{</common-pitfall >}}
65+
66+ {{<learning-more >}}While the most common use of this keyword is setting it to
67+ the boolean schema ` false ` to prevent additional properties, it is possible to
68+ set it to a satisfiable schema. Doing this, while omitting the
69+ [ ` properties ` ] ({{< ref "2019-09/applicator/properties" >}}) and
70+ [ ` patternProperties ` ] ({{< ref "2019-09/applicator/patternproperties" >}})
71+ keywords, is an elegant way of describing how the value of every property in
72+ the object instance must look like independently of its
73+ name.{{</learning-more >}}
74+
75+ {{<constraint-warning ` object ` >}}
76+
77+ ## Examples
78+
79+ {{<schema ` A schema that constrains object instances to not define additional properties ` >}}
80+ {
81+ "$schema": "https://json-schema.org/draft/2019-09/schema ",
82+ "properties": {
83+ "foo": { "type": "string" }
84+ },
85+ "patternProperties": {
86+ "^x-": { "type": "integer" }
87+ },
88+ "additionalProperties": false
89+ }
90+ {{</schema >}}
91+
92+ {{<instance-pass ` An object value that defines properties that only match static and regular expression definitions is valid ` >}}
93+ { "foo": "bar", "x-test": 2 }
94+ {{</instance-pass >}}
95+
96+ {{<instance-annotation >}}
97+ { "keyword": "/properties", "instance": "", "value": [ "foo" ] }
98+ { "keyword": "/patternProperties", "instance": "", "value": [ "x-test" ] }
99+ {{</instance-annotation >}}
100+
101+ {{<instance-fail ` An object value that defines valid properties and also defines additional properties is invalid ` >}}
102+ { "foo": "bar", "x-test": 2, "extra": true }
103+ {{</instance-fail >}}
104+
105+ {{<instance-fail ` An object value that only defines additional properties is invalid ` >}}
106+ { "extra": true, "random": 1234 }
107+ {{</instance-fail >}}
108+
109+ {{<instance-pass ` An empty object value is valid ` >}}
110+ {}
111+ {{</instance-pass >}}
112+
113+ {{<instance-pass ` A non-object value is valid ` >}}
114+ "Hello World"
115+ {{</instance-pass >}}
116+
117+ {{<schema ` A schema that constrains object instances to only define integer properties ` >}}
118+ {
119+ "$schema": "https://json-schema.org/draft/2019-09/schema ",
120+ "additionalProperties": { "type": "integer" }
121+ }
122+ {{</schema >}}
123+
124+ {{<instance-pass ` An object value that only defines integer properties is valid ` >}}
125+ { "foo": 1, "bar": 2, "baz": 3 }
126+ {{</instance-pass >}}
127+
128+ {{<instance-annotation >}}
129+ { "keyword": "/additionalProperties", "instance": "", "value": [ "foo", "bar", "baz" ] }
130+ {{</instance-annotation >}}
131+
132+ {{<instance-fail ` An object value that defines at least one non-integer property is invalid ` >}}
133+ { "foo": 1, "name": "John Doe" }
134+ {{</instance-fail >}}
135+
136+ {{<instance-pass ` An empty object value is valid ` >}}
137+ {}
138+ {{</instance-pass >}}
139+
140+ {{<instance-pass ` A non-object value is valid ` >}}
141+ "Hello World"
142+ {{</instance-pass >}}
143+
144+ {{<schema ` A schema that constrains object instances to define boolean additional properties ` >}}
145+ {
146+ "$schema": "https://json-schema.org/draft/2019-09/schema ",
147+ "properties": {
148+ "foo": { "type": "string" }
149+ },
150+ "patternProperties": {
151+ "^x-": { "type": "integer" }
152+ },
153+ "additionalProperties": {
154+ "type": "boolean"
155+ }
156+ }
157+ {{</schema >}}
158+
159+ {{<instance-pass ` An object value that defines valid properties and boolean additional properties is valid ` >}}
160+ { "foo": "bar", "x-test": 2, "extra": true }
161+ {{</instance-pass >}}
162+
163+ {{<instance-annotation >}}
164+ { "keyword": "/properties", "instance": "", "value": [ "foo" ] }
165+ { "keyword": "/patternProperties", "instance": "", "value": [ "x-test" ] }
166+ { "keyword": "/additionalProperties", "instance": "", "value": [ "extra" ] }
167+ {{</instance-annotation >}}
168+
169+ {{<instance-fail ` An object value that defines valid properties and also defines non-boolean additional properties is invalid ` >}}
170+ { "foo": "bar", "x-test": 2, "extra": "should be a boolean" }
171+ {{</instance-fail >}}
172+
173+ {{<instance-pass ` An empty object value is valid ` >}}
174+ {}
175+ {{</instance-pass >}}
176+
177+ {{<instance-pass ` A non-object value is valid ` >}}
178+ "Hello World"
179+ {{</instance-pass >}}
0 commit comments