4
4
.. _object :
5
5
6
6
object
7
- ------
7
+ ======
8
8
9
9
.. contents :: :local:
10
10
@@ -77,7 +77,7 @@ conventionally referred to as a "property".
77
77
.. _properties :
78
78
79
79
Properties
80
- ''''''''''
80
+ ----------
81
81
82
82
The properties (key-value pairs) on an object are defined using the
83
83
``properties `` keyword. The value of ``properties `` is an object,
@@ -127,7 +127,7 @@ address made up of a number, street name and street type:
127
127
.. _patternProperties :
128
128
129
129
Pattern Properties
130
- ''''''''''''''''''
130
+ ------------------
131
131
132
132
Sometimes you want to say that, given a particular kind of property
133
133
name, the value should match a particular schema. That's where
@@ -181,7 +181,7 @@ are ignored.
181
181
.. _additionalproperties :
182
182
183
183
Additional Properties
184
- '''''''''''''''''''''
184
+ ---------------------
185
185
186
186
The ``additionalProperties `` keyword is used to control the handling
187
187
of extra stuff, that is, properties whose names are not listed in the
@@ -269,6 +269,110 @@ properties (that are neither defined by ``properties`` nor matched by
269
269
// It must be a string:
270
270
{ "keyword": 42 }
271
271
272
+ .. index ::
273
+ single: object; properties; additionalProperties
274
+ single: extending
275
+
276
+ .. _extending :
277
+
278
+ Extending Closed Schemas
279
+ ''''''''''''''''''''''''
280
+
281
+ It's important to note that ``additionalProperties `` only recognizes
282
+ properties declared in the same subschema as itself. So,
283
+ ``additionalProperties `` can restrict you from "extending" a schema
284
+ using `combining ` keywords such as `allOf `. In the following example,
285
+ we can see how the ``additionalProperties `` can cause attempts to
286
+ extend the address schema example to fail.
287
+
288
+ .. schema_example ::
289
+
290
+ {
291
+ "allOf": [
292
+ {
293
+ "type": "object",
294
+ "properties": {
295
+ "street_address": { "type": "string" },
296
+ "city": { "type": "string" },
297
+ "state": { "type": "string" }
298
+ },
299
+ "required": ["street_address", "city", "state"],
300
+ "additionalProperties": false
301
+ }
302
+ ],
303
+
304
+ "properties": {
305
+ "type": { "enum": [ "residential", "business" ] }
306
+ },
307
+ "required": ["type"]
308
+ }
309
+ --X
310
+ // Fails ``additionalProperties ``. "type" is considered additional.
311
+ {
312
+ "street_address": "1600 Pennsylvania Avenue NW",
313
+ "city": "Washington",
314
+ "state": "DC",
315
+ "type": "business"
316
+ }
317
+ --X
318
+ // Fails ``required ``. "type" is required.
319
+ {
320
+ "street_address": "1600 Pennsylvania Avenue NW",
321
+ "city": "Washington",
322
+ "state": "DC"
323
+ }
324
+
325
+ Because ``additionalProperties `` only recognizes properties declared
326
+ in the same subschema, it considers anything other than
327
+ "street_address", "city", and "state" to be additional. Combining the
328
+ schemas with `allOf ` doesn't change that. A workaround you can use is
329
+ to move ``additionalProperties `` to the extending schema and redeclare
330
+ the properties from the extended schema.
331
+
332
+ .. schema_example ::
333
+
334
+ {
335
+ "allOf": [
336
+ {
337
+ "type": "object",
338
+ "properties": {
339
+ "street_address": { "type": "string" },
340
+ "city": { "type": "string" },
341
+ "state": { "type": "string" }
342
+ },
343
+ "required": ["street_address", "city", "state"]
344
+ }
345
+ ],
346
+
347
+ "properties": {
348
+ "street_address": true,
349
+ "city": true,
350
+ "state": true,
351
+ "type": { "enum": [ "residential", "business" ] }
352
+ },
353
+ "required": ["type"],
354
+ "additionalProperties": false
355
+ }
356
+ --
357
+ {
358
+ "street_address": "1600 Pennsylvania Avenue NW",
359
+ "city": "Washington",
360
+ "state": "DC",
361
+ "type": "business"
362
+ }
363
+ --X
364
+ {
365
+ "street_address": "1600 Pennsylvania Avenue NW",
366
+ "city": "Washington",
367
+ "state": "DC",
368
+ "type": "business",
369
+ "something that doesn't belong": "hi!"
370
+ }
371
+
372
+ Now the ``additionalProperties `` keyword is able to recognize all the
373
+ necessary properties and the schema works as expected. Keep reading to
374
+ see how the ``unevaluatedProperties `` keyword solves this problem
375
+ without needing to redeclare properties.
272
376
273
377
.. index ::
274
378
single: object; properties
@@ -277,7 +381,7 @@ properties (that are neither defined by ``properties`` nor matched by
277
381
.. _unevaluatedproperties :
278
382
279
383
Unevaluated Properties
280
- ''''''''''''''''''''''
384
+ ----------------------
281
385
282
386
|draft2019-09 |
283
387
@@ -290,7 +394,7 @@ Documentation Coming Soon
290
394
.. _required :
291
395
292
396
Required Properties
293
- '''''''''''''''''''
397
+ -------------------
294
398
295
399
By default, the properties defined by the ``properties `` keyword are
296
400
not required. However, one can provide a list of required properties
@@ -357,7 +461,7 @@ they don't provide their address or telephone number:
357
461
.. _propertyNames :
358
462
359
463
Property names
360
- ''''''''''''''
464
+ --------------
361
465
362
466
|draft6 |
363
467
@@ -396,7 +500,7 @@ schema given to ``propertyNames`` is always at least::
396
500
single: maxProperties
397
501
398
502
Size
399
- ''''
503
+ ----
400
504
401
505
The number of properties on an object can be restricted using the
402
506
``minProperties `` and ``maxProperties `` keywords. Each of these
0 commit comments