/
README
77 lines (74 loc) · 2.48 KB
/
README
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
Provides a Python function decorator, @json_validate, which validates
JSON input against a specification.
Example session:
>>> json_structure = required({'must_be_here':str}) + {
... 'a': int,
... 'b': float,
... 'c': str,
... 'd': [int],
... 'e': anytype,
... 'f': {'nested_structure': float},
... 'g': re.compile('^fo*$'),
... 'h': one_of({'a': int, 'b': list}),
... 'i': atleast_one({'a': int, 'b': list}),
... } + atleast_one({'more_keys': str})
>>> class C:
... @json_validate(json_structure)
... def fn(self, input):
... print input
...
>>> c = C()
>>> c.fn({
... 'must_be_here': 'is here',
... 'a': 1.0,
... 'b': 2.5,
... 'c': 'Hi!',
... 'd': [1, 2, 3],
... 'e': [],
... 'f': {'nested_structure': 5},
... 'extra_key': 'this is ignored by json_validate',
... 'g': 'fooooooooo',
... 'h': {'b':['anything','is','OK','here']},
... 'i': {'a':1},
... 'more_keys': ''
... })
{'must_be_here': 'is here', 'a': 1.0, 'c': 'Hi!', 'b': 2.5, 'e': [], 'd': [1, 2, 3], 'g': 'fooooooooo', 'f': {'nested_structure': 5}, 'i': {'a': 1}, 'h': {'b': ['anything', 'is', 'OK', 'here']}, 'more_keys': '', 'extra_key': 'this is ignored by json_validate'}
>>>
>>> c.fn({
... 'must_be_here': 'is here',
... 'a': 1.0,
... 'b': 2.5,
... 'c': 'Hi!',
... 'd': [1, 2, 3],
... 'e': [],
... 'f': {'nested_structure': 5},
... 'extra_key': 'this is ignored by json_validate',
... 'g': 'fooooooooo',
... 'h': {'b':['anything','is','OK','here']},
... 'i': {'a':1},
... })
Traceback (most recent call last):
File "<stdin>", line 12, in <module>
File "json_validate.py", line 455, in validator
'h': {'b':['anything','is','OK','here']},
json_validate.JSONException: client_json requires *at least* one of these keys, but found none: ['more_keys']
>>>
>>> c.fn({
... 'must_be_here': 'is here',
... 'a': 1.0,
... 'b': 2.5,
... 'c': 'Hi!',
... 'd': [1, 2, 3],
... 'e': [],
... 'f': {'nested_structure': 5},
... 'extra_key': 'this is ignored by json_validate',
... 'g': 'fooooooooo',
... 'h': {'a':1,'b':["shouldn't have both a and b"]},
... 'i': {'a':1},
... 'more_keys': ''
... })
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
File "json_validate.py", line 455, in validator
'h': {'b':['anything','is','OK','here']},
json_validate.JSONException: client_json['h'] requires one of these keys: ['a', 'b'], but found several: ['a', 'b']