def process_tree(value, key=None, parent_key=None):
def _process_leaf(value, key=None, parent_key=None):
if key == 'description' and parent_key != 'properties':
return DescriptionContent(value.strip())
return value
def _enforce_strict_types(dictionary):
if dictionary.get('type') == 'object':
dictionary.setdefault('additionalProperties', False)
elif dictionary.get('type') == 'string':
dictionary.setdefault('minLength', 1)
elif dictionary.get('type') == 'array':
dictionary.setdefault('uniqueItems', True)
dictionary.setdefault('minItems', 1)
return dictionary
def _ensure_values_have_types(properties, parent_key):
for key, val in properties.iteritems():
if not val.get('type') and not val.get('$ref'):
warnings.warn(
u'"{}" field of "{}" does not have a type'.format(
key, parent_key
)
)
def _is_leaf(value):
return (not isinstance(value, collections.Container)
or isinstance(value, basestring))
if _is_leaf(value):
return _process_leaf(value, key, parent_key)
elif isinstance(value, list):
return [process_tree(v) for v in value]
elif isinstance(value, dict):
value = _enforce_strict_types(value)
if key == 'properties':
_ensure_values_have_types(value, parent_key)
return {k: process_tree(v, k, key) for k, v in value.iteritems()}
else:
raise TypeError(u"'{}' has unexpected type: {}".format(
value, type(value).__name__))
评论列表
文章目录