193 |
193 |
return schema
|
194 |
194 |
|
195 |
195 |
if isinstance(simple_type, xmlschema.validators.XsdUnion):
|
196 |
|
return {'anyOf': [cls.simpletype_to_jsonschema(m) for m in simple_type.member_types]}
|
|
196 |
return {'oneOf': [cls.simpletype_to_jsonschema(m) for m in simple_type.member_types]}
|
197 |
197 |
|
198 |
198 |
raise NotImplementedError(simple_type)
|
199 |
199 |
|
... | ... | |
280 |
280 |
one_of.append(new_schema)
|
281 |
281 |
|
282 |
282 |
@classmethod
|
283 |
|
def type_to_jsonschema(cls, xmltype, depth=0):
|
|
283 |
def type_to_jsonschema(cls, xmltype):
|
284 |
284 |
assert isinstance(xmltype, xmlschema.validators.XsdType)
|
285 |
285 |
|
286 |
286 |
if xmltype.is_simple():
|
287 |
|
schema = cls.simpletype_to_jsonschema(xmltype)
|
288 |
|
if depth == 0:
|
289 |
|
schema = {'oneOf': [schema, {'type': 'null'}]}
|
290 |
|
return schema
|
|
287 |
base_schema = cls.simpletype_to_jsonschema(xmltype)
|
|
288 |
try:
|
|
289 |
xmltype.decode('')
|
|
290 |
except xmlschema.XMLSchemaValidationError:
|
|
291 |
return base_schema
|
|
292 |
else:
|
|
293 |
if base_schema.get('oneOf'):
|
|
294 |
base_schema['oneOf'].append({'type': 'null'})
|
|
295 |
return base_schema
|
|
296 |
return {'oneOf': [{'type': 'null'}, base_schema]}
|
291 |
297 |
elif xmltype.has_simple_content():
|
292 |
|
base_schema = cls.type_to_jsonschema(xmltype.base_type, depth=depth + 1)
|
|
298 |
base_schema = cls.type_to_jsonschema(xmltype.base_type)
|
293 |
299 |
if not xmltype.attributes:
|
294 |
300 |
schema = base_schema
|
295 |
301 |
else:
|
296 |
302 |
cls.attributegroup_to_jsonschema(xmltype.attributes)
|
297 |
303 |
schema['properties']['$'] = base_schema
|
298 |
|
if depth == 0:
|
299 |
|
schema = {'oneOf': [schema, {'type': 'null'}]}
|
300 |
304 |
return schema
|
301 |
305 |
else:
|
302 |
306 |
if xmltype.has_mixed_content() or not xmltype.is_element_only():
|
... | ... | |
314 |
318 |
assert isinstance(element, xmlschema.validators.XsdElement)
|
315 |
319 |
|
316 |
320 |
is_array = element.max_occurs > 1 or element.max_occurs is None
|
317 |
|
type_schema = cls.type_to_jsonschema(element.type)
|
|
321 |
item_schema = cls.type_to_jsonschema(element.type)
|
318 |
322 |
if is_array:
|
319 |
|
d = {
|
|
323 |
item_schema = {
|
320 |
324 |
'type': 'array',
|
321 |
|
'items': type_schema,
|
|
325 |
'items': item_schema,
|
322 |
326 |
'minItems': element.min_occurs,
|
323 |
327 |
}
|
324 |
328 |
if element.max_occurs is not None:
|
325 |
|
d['maxItems'] = element.max_occurs
|
326 |
|
return d
|
327 |
|
else:
|
328 |
|
return type_schema
|
|
329 |
item_schema['maxItems'] = element.max_occurs
|
|
330 |
return item_schema
|
329 |
331 |
|
330 |
332 |
def validate(self, instance):
|
331 |
333 |
return jsonschema.validate(instance=instance, schema=self.json_schema)
|
332 |
|
-
|