forked from urnest/urnest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonschema.py
216 lines (209 loc) · 8.01 KB
/
jsonschema.py
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
# coding: utf-8
#
# Copyright (c) 2018 Trevor Taylor
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that all
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# json schema represented as a straight-forward python datasture, e.g.:
#
# {
# 'name': str,
# 'age': int
# }
#
# ... means the corresponding json object must be a dictionary with
# "name" and "age" items with the specified types (string and int respectively).
#
from xju.xn import Xn,in_context,in_function_context,first_line_of,readable_repr
l1=first_line_of
class Schema:
def __init__(self,x):
'initialse schema %(x)r'
validateSchemaElement(x)
self.x=x
pass
def __repr__(self):
return repr(self.x)
def validate(self,x):
'verify that %(x)r conforms to jsonschema.Schema %(self)r'%vars()
return validate(self.x,x)
pass
class OneOf:
def __init__(self,*choices):
self.choices=choices
pass
def __str__(self):
return 'one of %(choices)s'%self.__dict__
def __repr__(self):
return 'one of %(choices)r'%self.__dict__
pass
def validateSchemaElement(x):
'verify that {x!r} is a valid json schema element'
try:
if x is None: return
if x in [int,str,float,bool]: return
if type(x) in [int,str]: return #literal
if type(x) is dict:
if len(x) == 1 and list(x.keys())[0] in (int,str):
validateSchemaElement(list(x.values())[0])
return
for name,y in x.items():
try:
if type(name) is not str:
raise Exception(f'{name} is not a string (it is a {name.__class__.__name__})')
validateSchemaElement(y)
except:
raise in_context('validate dict schema item %(name)r'%vars()) from None
pass
return
if type(x) is list:
if len(x) == 0:
raise Exception(
'list schema must contain at least one element')
for xx in x: validateSchemaElement(xx)
return
if type(x) is tuple:
for i,y in enumerate(x):
try:
validateSchemaElement(y)
except:
raise in_context('validate tuple schema item %(i)r'%vars()) from None
pass
return
if isinstance(x,OneOf):
for c in x.choices:
validateSchemaElement(c)
pass
return
if type(x) is bool:
#fixed literal value True or False
return
if isinstance(x,Schema):
return
t=type(x)
if t is object: t=x.__class__
raise Exception(f'jsonschema element may not be a {t}, it must be a list, a dictionary or int, str, float, bool, tuple, {__name__}.OneOf or None')
except:
raise in_function_context(validateSchemaElement,vars()) from None
pass
def validate(schema,x):
'verify {x!r} conforms to json schema {schema!r}'
try:
if schema is None and not x is None:
raise Exception('%(x)r is not None'%vars())
if type(schema) is int:
if not type(x) is int:
raise Exception('%(x)r is not an integer'%vars())
if not x==schema:
raise Exception('%(x)r is not %(schema)r'%vars())
pass
if type(schema) in [str]:
if not type(x) in [str]:
raise Exception('%(x)r is not a string'%vars())
if not x==schema:
raise Exception('%(x)r is not %(schema)r'%vars())
pass
if schema is int and not type(x) is int:
raise Exception('%(x)r is not an Int'%vars())
if schema is bool and not type(x) is bool:
raise Exception('%(x)r is not a Boolean'%vars())
if schema is float and not type(x) in [int,float]:
raise Exception('%(x)r is not a Float'%vars())
if schema is str and not type(x) in [str]:
raise Exception('%(x)r is not a String'%vars())
if type(schema) is dict:
if not type(x) is dict:
raise Exception('%(x)r is not a Dictionary'%vars())
if len(schema)==1 and list(schema.keys())[0] in (int,str):
for key,y in x.items():
try:
validate(list(schema.keys())[0],key)
validate(list(schema.values())[0],y)
except:
raise in_context('validate dictionary item %(key)r'%vars()) from None
pass
return x
for name, y in schema.items():
try:
if not name in x:
try:
validate(y,None)
except:
keys=list(x.keys())
raise Exception('%(name)r is not in %(keys)r and %(name)r is not optional'%vars())
pass
else:
validate(y,x[name])
pass
except:
raise in_context('validate dictionary item %(name)r'%vars()) from None
pass
return x
if type(schema) is list:
if not type(x) is list:
raise Exception('%(x)r is not a List'%vars())
if len(schema)==1:
for i,y in enumerate(x):
try:
validate(schema[0],y)
except:
raise in_context('validate list item %(i)r'%vars()) from None
pass
pass
else:
if len(schema)!=len(x):
i=len(schema)
j=len(x)
raise Exception('list {x} does not have {i} elements, it has {j} elements'.format(**vars()))
for i,v in enumerate(zip(schema,x)):
try:
validate(v[0],v[1])
except:
raise in_context('validate list element {i}'.format(**vars())) from None
pass
pass
if type(schema) is tuple:
if len(schema) != len(x):
sl=len(schema)
xl=len(x)
raise Exception('tuple has %(xl)s items not %(sl)s'%vars())
for i,y in enumerate(x):
try:
validate(schema[i],y)
except:
raise in_context('validate tuple schema element %(i)s'%vars()) from None
pass
pass
if isinstance(schema,OneOf):
choices=schema.choices[:]
failures=[]
while len(choices):
try:
validate(choices[0],x)
return x
except Exception as e:
failures.append(e)
pass
choices=choices[1:]
pass
raise Exception(' and '.join([readable_repr(_) for _ in failures]))
if type(schema) is bool:
if x==schema: return x
raise Exception('{x!r} is not {schema!r}'.format(**vars()))
if isinstance(schema,Schema):
schema.validate(x)
pass
return x
except:
raise in_function_context(validate,vars()) from None
pass