@@ -19,7 +19,9 @@ would pass validation the first time, but fail the second!
19
19
Still, filling in defaults is a thing that is useful. :mod: `jsonschema `
20
20
allows you to :doc: `define your own validator classes and callables
21
21
<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.)
23
25
24
26
.. code-block :: python
25
27
@@ -30,15 +32,15 @@ default setting. Here's some code to get you started:
30
32
validate_properties = validator_class.VALIDATORS [" properties" ]
31
33
32
34
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
+
33
39
for error in validate_properties(
34
40
validator, properties, instance, schema,
35
41
):
36
42
yield error
37
43
38
- for property , subschema in properties.iteritems():
39
- if " default" in subschema:
40
- instance.setdefault(property , subschema[" default" ])
41
-
42
44
return validators.extend(
43
45
validator_class, {" properties" : set_defaults},
44
46
)
@@ -60,10 +62,50 @@ See the above-linked document for more info on how this works, but basically,
60
62
it just extends the :validator: `properties ` validator on a
61
63
:class: `Draft4Validator ` to then go ahead and update all the defaults.
62
64
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
67
109
68
110
69
111
How do jsonschema version numbers work?
0 commit comments