152 |
152 |
'required': [root_element],
|
153 |
153 |
'additionalProperties': False,
|
154 |
154 |
}
|
155 |
155 |
|
156 |
156 |
@classmethod
|
157 |
157 |
def simpletype_to_jsonschema(cls, simple_type):
|
158 |
158 |
assert isinstance(simple_type, xmlschema.validators.XsdSimpleType)
|
159 |
159 |
|
|
160 |
def add_patterns():
|
|
161 |
if simple_type.patterns:
|
|
162 |
if len(simple_type.patterns) == 1:
|
|
163 |
schema['pattern'] = simple_type.patterns.regexps[0]
|
|
164 |
else:
|
|
165 |
schema['pattern'] = '|'.join(simple_type.patterns.regexps)
|
|
166 |
|
160 |
167 |
if simple_type.name in getattr(cls, 'type_map', {}):
|
161 |
168 |
mapped = cls.type_map[simple_type.name]
|
162 |
169 |
if hasattr(cls, 'schema_%s' % mapped):
|
163 |
170 |
return getattr(cls, 'schema_%s' % mapped)()
|
164 |
171 |
|
165 |
172 |
if isinstance(simple_type, xmlschema.validators.XsdAtomicBuiltin):
|
166 |
173 |
if (
|
167 |
174 |
simple_type.min_length
|
168 |
175 |
or simple_type.max_length
|
169 |
176 |
or simple_type.white_space not in ('collapse', 'preserve')
|
170 |
|
or simple_type.patterns
|
171 |
177 |
):
|
172 |
178 |
raise NotImplementedError(simple_type)
|
173 |
179 |
|
174 |
180 |
if simple_type.name in cls.SIMPLE_TYPE_MAPPING:
|
175 |
181 |
schema = {'type': cls.SIMPLE_TYPE_MAPPING[simple_type.name]}
|
176 |
182 |
else:
|
177 |
183 |
raise NotImplementedError(simple_type)
|
|
184 |
add_patterns()
|
178 |
185 |
return schema
|
179 |
186 |
|
180 |
187 |
if isinstance(simple_type, xmlschema.validators.XsdAtomicRestriction):
|
181 |
188 |
if simple_type.white_space not in ('collapse', 'preserve'):
|
182 |
189 |
raise NotImplementedError(simple_type)
|
183 |
190 |
schema = OrderedDict(cls.simpletype_to_jsonschema(simple_type.base_type))
|
184 |
191 |
for validator in simple_type.validators:
|
185 |
192 |
if isinstance(validator, xmlschema.validators.XsdEnumerationFacets):
|
... | ... | |
211 |
218 |
schema['exclusiveMaximum'] = 10**validator.value
|
212 |
219 |
elif (
|
213 |
220 |
isinstance(validator, xmlschema.validators.XsdFractionDigitsFacet)
|
214 |
221 |
and simple_type.base_type.name == xmlschema.qnames.XSD_DECIMAL
|
215 |
222 |
):
|
216 |
223 |
schema['multipleOf'] = 1 / 10.0**validator.value
|
217 |
224 |
else:
|
218 |
225 |
raise NotImplementedError(validator)
|
219 |
|
if simple_type.patterns:
|
220 |
|
if len(simple_type.patterns) == 1:
|
221 |
|
schema['pattern'] = simple_type.patterns.regexps[0]
|
222 |
|
else:
|
223 |
|
schema['pattern'] = '|'.join(simple_type.patterns.regexps)
|
|
226 |
add_patterns()
|
224 |
227 |
return schema
|
225 |
228 |
|
226 |
229 |
if isinstance(simple_type, xmlschema.validators.XsdUnion):
|
227 |
230 |
return {'oneOf': [cls.simpletype_to_jsonschema(m) for m in simple_type.member_types]}
|
228 |
231 |
|
229 |
232 |
raise NotImplementedError(simple_type)
|
230 |
233 |
|
231 |
234 |
@classmethod
|
232 |
|
-
|