Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_exclusive_maximum(self):
with self.l('if isinstance({variable}, (int, float)):'):
if not isinstance(self._definition['exclusiveMaximum'], (int, float)):
raise JsonSchemaDefinitionException('exclusiveMaximum must be an integer or a float')
with self.l('if {variable} >= {exclusiveMaximum}:'):
self.exc('{name} must be smaller than {exclusiveMaximum}', rule='exclusiveMaximum')
def _generate_func_code_block(self, definition):
if not isinstance(definition, dict):
raise JsonSchemaDefinitionException("definition must be an object")
if '$ref' in definition:
# needed because ref overrides any sibling keywords
self.generate_ref()
else:
self.run_generate_functions(definition)
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_type(self):
"""
Validation of type. Can be one type or list of types.
.. code-block:: python
{'type': 'string'}
{'type': ['string', 'number']}
"""
types = enforce_list(self._definition['type'])
try:
python_types = ', '.join(JSON_TYPE_TO_PYTHON_TYPE[t] for t in types)
except KeyError as exc:
raise JsonSchemaDefinitionException('Unknown type: {}'.format(exc))
extra = ''
if ('number' in types or 'integer' in types) and 'boolean' not in types:
extra = ' or isinstance({variable}, bool)'.format(variable=self._variable)
with self.l('if not isinstance({variable}, ({})){}:', python_types, extra):
self.exc('{name} must be {}', ' or '.join(types), rule='type')
def generate_exclusive_minimum(self):
with self.l('if isinstance({variable}, (int, float)):'):
if not isinstance(self._definition['exclusiveMinimum'], (int, float)):
raise JsonSchemaDefinitionException('exclusiveMinimum must be an integer or a float')
with self.l('if {variable} <= {exclusiveMinimum}:'):
self.exc('{name} must be bigger than {exclusiveMinimum}', rule='exclusiveMinimum')
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_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')
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_))