Skip to content

Commit 3eb5de8

Browse files
committed
Merge pull request #255 from stuarteberg/fix-docs-faq-default-property
docs: faq: Updates to extend_with_default() example
2 parents 11666e8 + ea8ffcb commit 3eb5de8

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

docs/faq.rst

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ would pass validation the first time, but fail the second!
1919
Still, filling in defaults is a thing that is useful. :mod:`jsonschema`
2020
allows you to :doc:`define your own validator classes and callables
2121
<creating>`, so you can easily create a :class:`IValidator` that does do
22-
default setting. Here's some code to get you started:
22+
default setting. Here's some code to get you started. (In this code, we add
23+
the default properties to each object *before* the properties are validated,
24+
so the default values themselves will need to be valid under the schema.)
2325

2426
.. code-block:: python
2527
@@ -30,15 +32,15 @@ default setting. Here's some code to get you started:
3032
validate_properties = validator_class.VALIDATORS["properties"]
3133
3234
def set_defaults(validator, properties, instance, schema):
35+
for property, subschema in properties.iteritems():
36+
if "default" in subschema:
37+
instance.setdefault(property, subschema["default"])
38+
3339
for error in validate_properties(
3440
validator, properties, instance, schema,
3541
):
3642
yield error
3743
38-
for property, subschema in properties.iteritems():
39-
if "default" in subschema:
40-
instance.setdefault(property, subschema["default"])
41-
4244
return validators.extend(
4345
validator_class, {"properties" : set_defaults},
4446
)
@@ -60,10 +62,50 @@ See the above-linked document for more info on how this works, but basically,
6062
it just extends the :validator:`properties` validator on a
6163
:class:`Draft4Validator` to then go ahead and update all the defaults.
6264

63-
If you're interested in a more interesting solution to a larger class of these
64-
types of transformations, keep an eye on `Seep
65-
<https://github.com/Julian/Seep>`_, which is an experimental data
66-
transformation and extraction library written on top of :mod:`jsonschema`.
65+
.. note::
66+
67+
If you're interested in a more interesting solution to a larger class of these
68+
types of transformations, keep an eye on `Seep
69+
<https://github.com/Julian/Seep>`_, which is an experimental data
70+
transformation and extraction library written on top of :mod:`jsonschema`.
71+
72+
73+
.. hint::
74+
75+
The above code can provide default values for an entire object and all of its properties,
76+
but only if your schema provides a default value for the object itself, like so:
77+
78+
.. code-block:: python
79+
80+
schema = {
81+
"type": "object",
82+
"properties": {
83+
"outer-object": {
84+
"type": "object",
85+
"properties" : {
86+
"inner-object": {
87+
"type": "string",
88+
"default": "INNER-DEFAULT"
89+
}
90+
},
91+
"default": {} # <-- MUST PROVIDE DEFAULT OBJECT
92+
}
93+
}
94+
}
95+
96+
obj = {}
97+
DefaultValidatingDraft4Validator(schema).validate(obj)
98+
assert obj == {'outer-object': {'inner-object': 'INNER-DEFAULT'}}
99+
100+
...but if you don't provide a default value for your object,
101+
then it won't be instantiated at all, much less populated with default properties.
102+
103+
.. code-block:: python
104+
105+
del schema["properties"]["outer-object"]["default"]
106+
obj2 = {}
107+
DefaultValidatingDraft4Validator(schema).validate(obj2)
108+
assert obj2 == {} # whoops
67109
68110
69111
How do jsonschema version numbers work?

0 commit comments

Comments
 (0)