import decimal import re from .exceptions import JsonSchemaDefinitionException from .generator import CodeGenerator, enforce_list JSON_TYPE_TO_PYTHON_TYPE = { 'null': 'NoneType', 'boolean': 'bool', 'number': 'int, float', 'integer': 'int', 'string': 'str', 'array': 'list, tuple', 'object': 'dict', } DOLLAR_FINDER = re.compile(r"(? {maxLength}:'): self.exc('{name} must be shorter than or equal to {maxLength} characters', rule='maxLength') def generate_pattern(self): with self.l('if isinstance({variable}, str):'): pattern = self._definition['pattern'] safe_pattern = pattern.replace('\\', '\\\\').replace('"', '\\"') end_of_string_fixed_pattern = DOLLAR_FINDER.sub(r'\\Z', pattern) self._compile_regexps[pattern] = re.compile(end_of_string_fixed_pattern) with self.l('if not REGEX_PATTERNS[{}].search({variable}):', repr(pattern)): self.exc('{name} must match pattern {}', safe_pattern, rule='pattern') def generate_format(self): """ Means that value have to be in specified format. For example date, email or other. .. code-block:: python {'format': 'email'} Valid value for this definition is user@example.com but not @username """ with self.l('if isinstance({variable}, str):'): format_ = self._definition['format'] # Checking custom formats - user is allowed to override default formats. if format_ in self._custom_formats: custom_format = self._custom_formats[format_] if isinstance(custom_format, str): self._generate_format(format_, format_ + '_re_pattern', custom_format) else: with self.l('if not custom_formats["{}"]({variable}):', format_): self.exc('{name} must be {}', format_, rule='format') elif format_ in self.FORMAT_REGEXS: format_regex = self.FORMAT_REGEXS[format_] self._generate_format(format_, format_ + '_re_pattern', format_regex) # Format regex is used only in meta schemas. elif format_ == 'regex': with self.l('try:', optimize=False): self.l('re.compile({variable})') with self.l('except Exception:'): self.exc('{name} must be a valid regex', rule='format') else: raise JsonSchemaDefinitionException('Unknown format: {}'.format(format_)) def _generate_format(self, format_name, regexp_name, regexp): if self._definition['format'] == format_name: if not regexp_name in self._compile_regexps: self._compile_regexps[regexp_name] = re.compile(regexp) with self.l('if not REGEX_PATTERNS["{}"].match({variable}):', regexp_name): self.exc('{name} must be {}', format_name, rule='format') def generate_minimum(self): with self.l('if isinstance({variable}, (int, float)):'): if not isinstance(self._definition['minimum'], (int, float)): raise JsonSchemaDefinitionException('minimum must be a number') if self._definition.get('exclusiveMinimum', False): with self.l('if {variable} <= {minimum}:'): self.exc('{name} must be bigger than {minimum}', rule='minimum') else: with self.l('if {variable} < {minimum}:'): self.exc('{name} must be bigger than or equal to {minimum}', rule='minimum') def generate_maximum(self): with self.l('if isinstance({variable}, (int, float)):'): if not isinstance(self._definition['maximum'], (int, float)): raise JsonSchemaDefinitionException('maximum must be a number') if self._definition.get('exclusiveMaximum', False): with self.l('if {variable} >= {maximum}:'): self.exc('{name} must be smaller than {maximum}', rule='maximum') else: with self.l('if {variable} > {maximum}:'): self.exc('{name} must be smaller than or equal to {maximum}', rule='maximum') def generate_multiple_of(self): with self.l('if isinstance({variable}, (int, float)):'): if not isinstance(self._definition['multipleOf'], (int, float)): raise JsonSchemaDefinitionException('multipleOf must be a number') # For proper multiplication check of floats we need to use decimals, # because for example 19.01 / 0.01 = 1901.0000000000002. if isinstance(self._definition['multipleOf'], float): self._extra_imports_lines.append('from decimal import Decimal') self._extra_imports_objects['Decimal'] = decimal.Decimal self.l('quotient = Decimal(repr({variable})) / Decimal(repr({multipleOf}))') else: self.l('quotient = {variable} / {multipleOf}') with self.l('if int(quotient) != quotient:'): self.exc('{name} must be multiple of {multipleOf}', rule='multipleOf') def generate_min_items(self): self.create_variable_is_list() with self.l('if {variable}_is_list:'): if not isinstance(self._definition['minItems'], int): raise JsonSchemaDefinitionException('minItems must be a number') self.create_variable_with_length() with self.l('if {variable}_len < {minItems}:'): self.exc('{name} must contain at least {minItems} items', rule='minItems') def generate_max_items(self): self.create_variable_is_list() with self.l('if {variable}_is_list:'): if not isinstance(self._definition['maxItems'], int): raise JsonSchemaDefinitionException('maxItems must be a number') self.create_variable_with_length() with self.l('if {variable}_len > {maxItems}:'): self.exc('{name} must contain less than or equal to {maxItems} items', rule='maxItems') def generate_unique_items(self): """ With Python 3.4 module ``timeit`` recommended this solutions: .. code-block:: python >>> timeit.timeit("len(x) > len(set(x))", "x=range(100)+range(100)", number=100000) 0.5839540958404541 >>> timeit.timeit("len({}.fromkeys(x)) == len(x)", "x=range(100)+range(100)", number=100000) 0.7094449996948242 >>> timeit.timeit("seen = set(); any(i in seen or seen.add(i) for i in x)", "x=range(100)+range(100)", number=100000) 2.0819358825683594 >>> timeit.timeit("np.unique(x).size == len(x)", "x=range(100)+range(100); import numpy as np", number=100000) 2.1439831256866455 """ unique_definition = self._definition['uniqueItems'] if not unique_definition: return self.create_variable_is_list() with self.l('if {variable}_is_list:'): self.l( 'def fn(var): ' 'return frozenset(dict((k, fn(v)) ' 'for k, v in var.items()).items()) ' 'if hasattr(var, "items") else tuple(fn(v) ' 'for v in var) ' 'if isinstance(var, (dict, list)) else str(var) ' 'if isinstance(var, bool) else var') self.create_variable_with_length() with self.l('if {variable}_len > len(set(fn({variable}_x) for {variable}_x in {variable})):'): self.exc('{name} must contain unique items', rule='uniqueItems') def generate_items(self): """ Means array is valid only when all items are valid by this definition. .. code-block:: python { 'items': [ {'type': 'integer'}, {'type': 'string'}, ], } Valid arrays are those with integers or strings, nothing else. Since draft 06 definition can be also boolean. True means nothing, False means everything is invalid. """ items_definition = self._definition['items'] if items_definition is True: return self.create_variable_is_list() with self.l('if {variable}_is_list:'): self.create_variable_with_length() if items_definition is False: with self.l('if {variable}:'): self.exc('{name} must not be there', rule='items') elif isinstance(items_definition, list): for idx, item_definition in enumerate(items_definition): with self.l('if {variable}_len > {}:', idx): self.l('{variable}__{0} = {variable}[{0}]', idx) self.generate_func_code_block( item_definition, '{}__{}'.format(self._variable, idx), '{}[{}]'.format(self._variable_name, idx), ) if self._use_default and isinstance(item_definition, dict) and 'default' in item_definition: self.l('else: {variable}.append({})', repr(item_definition['default'])) if 'additionalItems' in self._definition: if self._definition['additionalItems'] is False: with self.l('if {variable}_len > {}:', len(items_definition)): self.exc('{name} must contain only specified items', rule='items') else: with self.l('for {variable}_x, {variable}_item in enumerate({variable}[{0}:], {0}):', len(items_definition)): count = self.generate_func_code_block( self._definition['additionalItems'], '{}_item'.format(self._variable), '{}[{{{}_x}}]'.format(self._variable_name, self._variable), ) if count == 0: self.l('pass') else: if items_definition: with self.l('for {variable}_x, {variable}_item in enumerate({variable}):'): count = self.generate_func_code_block( items_definition, '{}_item'.format(self._variable), '{}[{{{}_x}}]'.format(self._variable_name, self._variable), ) if count == 0: self.l('pass') def generate_min_properties(self): self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): if not isinstance(self._definition['minProperties'], int): raise JsonSchemaDefinitionException('minProperties must be a number') self.create_variable_with_length() with self.l('if {variable}_len < {minProperties}:'): self.exc('{name} must contain at least {minProperties} properties', rule='minProperties') def generate_max_properties(self): self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): if not isinstance(self._definition['maxProperties'], int): raise JsonSchemaDefinitionException('maxProperties must be a number') self.create_variable_with_length() with self.l('if {variable}_len > {maxProperties}:'): self.exc('{name} must contain less than or equal to {maxProperties} properties', rule='maxProperties') def generate_required(self): self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): if not isinstance(self._definition['required'], (list, tuple)): raise JsonSchemaDefinitionException('required must be an array') self.create_variable_with_length() with self.l('if not all(prop in {variable} for prop in {required}):'): self.exc('{name} must contain {} properties', self.e(self._definition['required']), rule='required') def generate_properties(self): """ Means object with defined keys. .. code-block:: python { 'properties': { 'key': {'type': 'number'}, }, } Valid object is containing key called 'key' and value any number. """ self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): self.create_variable_keys() for key, prop_definition in self._definition['properties'].items(): key_name = re.sub(r'($[^a-zA-Z]|[^a-zA-Z0-9])', '', key) if not isinstance(prop_definition, (dict, bool)): raise JsonSchemaDefinitionException('{}[{}] must be object'.format(self._variable, key_name)) with self.l('if "{}" in {variable}_keys:', self.e(key)): self.l('{variable}_keys.remove("{}")', self.e(key)) self.l('{variable}__{0} = {variable}["{1}"]', key_name, self.e(key)) self.generate_func_code_block( prop_definition, '{}__{}'.format(self._variable, key_name), '{}.{}'.format(self._variable_name, self.e(key)), clear_variables=True, ) if self._use_default and isinstance(prop_definition, dict) and 'default' in prop_definition: self.l('else: {variable}["{}"] = {}', self.e(key), repr(prop_definition['default'])) def generate_pattern_properties(self): """ Means object with defined keys as patterns. .. code-block:: python { 'patternProperties': { '^x': {'type': 'number'}, }, } Valid object is containing key starting with a 'x' and value any number. """ self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): self.create_variable_keys() for pattern, definition in self._definition['patternProperties'].items(): self._compile_regexps[pattern] = re.compile(pattern) with self.l('for {variable}_key, {variable}_val in {variable}.items():'): for pattern, definition in self._definition['patternProperties'].items(): with self.l('if REGEX_PATTERNS[{}].search({variable}_key):', repr(pattern)): with self.l('if {variable}_key in {variable}_keys:'): self.l('{variable}_keys.remove({variable}_key)') self.generate_func_code_block( definition, '{}_val'.format(self._variable), '{}.{{{}_key}}'.format(self._variable_name, self._variable), clear_variables=True, ) def generate_additional_properties(self): """ Means object with keys with values defined by definition. .. code-block:: python { 'properties': { 'key': {'type': 'number'}, } 'additionalProperties': {'type': 'string'}, } Valid object is containing key called 'key' and it's value any number and any other key with any string. """ self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): self.create_variable_keys() add_prop_definition = self._definition["additionalProperties"] if add_prop_definition is True or add_prop_definition == {}: return if add_prop_definition: properties_keys = list(self._definition.get("properties", {}).keys()) with self.l('for {variable}_key in {variable}_keys:'): with self.l('if {variable}_key not in {}:', properties_keys): self.l('{variable}_value = {variable}.get({variable}_key)') self.generate_func_code_block( add_prop_definition, '{}_value'.format(self._variable), '{}.{{{}_key}}'.format(self._variable_name, self._variable), ) else: with self.l('if {variable}_keys:'): self.exc('{name} must not contain "+str({variable}_keys)+" properties', rule='additionalProperties') def generate_dependencies(self): """ Means when object has property, it needs to have also other property. .. code-block:: python { 'dependencies': { 'bar': ['foo'], }, } Valid object is containing only foo, both bar and foo or none of them, but not object with only bar. Since draft 06 definition can be boolean or empty array. True and empty array means nothing, False means that key cannot be there at all. """ self.create_variable_is_dict() with self.l('if {variable}_is_dict:'): is_empty = True for key, values in self._definition["dependencies"].items(): if values == [] or values is True: continue is_empty = False with self.l('if "{}" in {variable}:', self.e(key)): if values is False: self.exc('{} in {name} must not be there', key, rule='dependencies') elif isinstance(values, list): for value in values: with self.l('if "{}" not in {variable}:', self.e(value)): self.exc('{name} missing dependency {} for {}', self.e(value), self.e(key), rule='dependencies') else: self.generate_func_code_block(values, self._variable, self._variable_name, clear_variables=True) if is_empty: self.l('pass')