summaryrefslogtreecommitdiffstats
path: root/python/voluptuous/tests.md
blob: 18f6fbafa7e24a7333b7e7b3e53cac35e5213ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Error reporting should be accurate:

    >>> from voluptuous import *
    >>> schema = Schema(['one', {'two': 'three', 'four': ['five'],
    ...                          'six': {'seven': 'eight'}}])
    >>> schema(['one'])
    ['one']
    >>> schema([{'two': 'three'}])
    [{'two': 'three'}]

It should show the exact index and container type, in this case a list
value:

    >>> try:
    ...   schema(['one', 'two'])
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc) == 'expected a dictionary @ data[1]'
    True

It should also be accurate for nested values:

    >>> try:
    ...   schema([{'two': 'nine'}])
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "not a valid value for dictionary value @ data[0]['two']"

    >>> try:
    ...   schema([{'four': ['nine']}])
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "not a valid value @ data[0]['four'][0]"

    >>> try:
    ...   schema([{'six': {'seven': 'nine'}}])
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "not a valid value for dictionary value @ data[0]['six']['seven']"

Errors should be reported depth-first:

    >>> validate = Schema({'one': {'two': 'three', 'four': 'five'}})
    >>> try:
    ...   validate({'one': {'four': 'six'}})
    ... except Invalid as e:
    ...   print(e)
    ...   print(e.path)
    not a valid value for dictionary value @ data['one']['four']
    ['one', 'four']

Voluptuous supports validation when extra fields are present in the
data:

    >>> schema = Schema({'one': 1, Extra: object})
    >>> schema({'two': 'two', 'one': 1}) == {'two': 'two', 'one': 1}
    True
    >>> schema = Schema({'one': 1})
    >>> try:
    ...   schema({'two': 2})
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "extra keys not allowed @ data['two']"

dict, list, and tuple should be available as type validators:

    >>> Schema(dict)({'a': 1, 'b': 2}) == {'a': 1, 'b': 2}
    True
    >>> Schema(list)([1,2,3])
    [1, 2, 3]
    >>> Schema(tuple)((1,2,3))
    (1, 2, 3)

Validation should return instances of the right types when the types are
subclasses of dict or list:

    >>> class Dict(dict):
    ...   pass
    >>>
    >>> d = Schema(dict)(Dict(a=1, b=2))
    >>> d == {'a': 1, 'b': 2}
    True
    >>> type(d) is Dict
    True
    >>> class List(list):
    ...   pass
    >>>
    >>> l = Schema(list)(List([1,2,3]))
    >>> l
    [1, 2, 3]
    >>> type(l) is List
    True

Multiple errors are reported:

    >>> schema = Schema({'one': 1, 'two': 2})
    >>> try:
    ...   schema({'one': 2, 'two': 3, 'three': 4})
    ... except MultipleInvalid as e:
    ...   errors = sorted(e.errors, key=lambda k: str(k))
    ...   print([str(i) for i in errors])  # doctest: +NORMALIZE_WHITESPACE
    ["extra keys not allowed @ data['three']",
     "not a valid value for dictionary value @ data['one']",
     "not a valid value for dictionary value @ data['two']"]
    >>> schema = Schema([[1], [2], [3]])
    >>> try:
    ...   schema([1, 2, 3])
    ... except MultipleInvalid as e:
    ...   print([str(i) for i in e.errors])  # doctest: +NORMALIZE_WHITESPACE
    ['expected a list @ data[0]',
     'expected a list @ data[1]',
     'expected a list @ data[2]']

Required fields in dictionary which are invalid should not have required :

    >>> from voluptuous import *
    >>> schema = Schema({'one': {'two': 3}}, required=True)
    >>> try:
    ...   schema({'one': {'two': 2}})
    ... except MultipleInvalid as e:
    ...   errors = e.errors
    >>> 'required' in ' '.join([x.msg for x in errors])
    False

Multiple errors for nested fields in dicts and objects:

> \>\>\> from collections import namedtuple \>\>\> validate = Schema({
> ... 'anobject': Object({ ... 'strfield': str, ... 'intfield': int ...
> }) ... }) \>\>\> try: ... SomeObj = namedtuple('SomeObj', ('strfield',
> 'intfield')) ... validate({'anobject': SomeObj(strfield=123,
> intfield='one')}) ... except MultipleInvalid as e: ...
> print(sorted(str(i) for i in e.errors)) \# doctest:
> +NORMALIZE\_WHITESPACE ["expected int for object value @
> data['anobject']['intfield']", "expected str for object value @
> data['anobject']['strfield']"]

Custom classes validate as schemas:

    >>> class Thing(object):
    ...     pass
    >>> schema = Schema(Thing)
    >>> t = schema(Thing())
    >>> type(t) is Thing
    True

Classes with custom metaclasses should validate as schemas:

    >>> class MyMeta(type):
    ...     pass
    >>> class Thing(object):
    ...     __metaclass__ = MyMeta
    >>> schema = Schema(Thing)
    >>> t = schema(Thing())
    >>> type(t) is Thing
    True

Schemas built with All() should give the same error as the original
validator (Issue \#26):

    >>> schema = Schema({
    ...   Required('items'): All([{
    ...     Required('foo'): str
    ...   }])
    ... })

    >>> try:
    ...   schema({'items': [{}]})
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "required key not provided @ data['items'][0]['foo']"

Validator should return same instance of the same type for object:

    >>> class Structure(object):
    ...     def __init__(self, q=None):
    ...         self.q = q
    ...     def __repr__(self):
    ...         return '{0.__name__}(q={1.q!r})'.format(type(self), self)
    ...
    >>> schema = Schema(Object({'q': 'one'}, cls=Structure))
    >>> type(schema(Structure(q='one'))) is Structure
    True

Object validator should treat cls argument as optional. In this case it
shouldn't check object type:

    >>> from collections import namedtuple
    >>> NamedTuple = namedtuple('NamedTuple', ('q',))
    >>> schema = Schema(Object({'q': 'one'}))
    >>> named = NamedTuple(q='one')
    >>> schema(named) == named
    True
    >>> schema(named)
    NamedTuple(q='one')

If cls argument passed to object validator we should check object type:

    >>> schema = Schema(Object({'q': 'one'}, cls=Structure))
    >>> schema(NamedTuple(q='one'))  # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
    ...
    MultipleInvalid: expected a <class 'Structure'>
    >>> schema = Schema(Object({'q': 'one'}, cls=NamedTuple))
    >>> schema(NamedTuple(q='one'))
    NamedTuple(q='one')

Ensure that objects with \_\_slots\_\_ supported properly:

    >>> class SlotsStructure(Structure):
    ...     __slots__ = ['q']
    ...
    >>> schema = Schema(Object({'q': 'one'}))
    >>> schema(SlotsStructure(q='one'))
    SlotsStructure(q='one')
    >>> class DictStructure(object):
    ...     __slots__ = ['q', '__dict__']
    ...     def __init__(self, q=None, page=None):
    ...         self.q = q
    ...         self.page = page
    ...     def __repr__(self):
    ...         return '{0.__name__}(q={1.q!r}, page={1.page!r})'.format(type(self), self)
    ...
    >>> structure = DictStructure(q='one')
    >>> structure.page = 1
    >>> try:
    ...   schema(structure)
    ...   raise AssertionError('MultipleInvalid not raised')
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> str(exc)
    "extra keys not allowed @ data['page']"

    >>> schema = Schema(Object({'q': 'one', Extra: object}))
    >>> schema(structure)
    DictStructure(q='one', page=1)

Ensure that objects can be used with other validators:

    >>> schema = Schema({'meta': Object({'q': 'one'})})
    >>> schema({'meta': Structure(q='one')})
    {'meta': Structure(q='one')}

Ensure that subclasses of Invalid of are raised as is.

    >>> class SpecialInvalid(Invalid):
    ...   pass
    ...
    >>> def custom_validator(value):
    ...   raise SpecialInvalid('boom')
    ...
    >>> schema = Schema({'thing': custom_validator})
    >>> try:
    ...   schema({'thing': 'not an int'})
    ... except MultipleInvalid as e:
    ...   exc = e
    >>> exc.errors[0].__class__.__name__
    'SpecialInvalid'