def _validate_exclusive_maximum(self, value, schema, constraint, path): if not is_boolean(constraint): # Got a number so use the newer behavior. if value > constraint: return f'{value} is greater than {constraint}.' # Otherwise, the validation will be handled by the maximum constraint. pass
def _validate_type(self, value, schema, constraint, path): if isinstance(constraint, list): for possible_type in constraint: if self._validate_type(value, schema, possible_type, path) is None: return None return f'it is not one of {str(constraint).replace("None", "null")}' elif constraint == 'object': if not is_object(value): return 'it is not an object.' elif constraint == 'array': if not is_array(value): return 'it is not an array.' elif constraint == 'string': if not is_string(value): return 'it is not a string.' elif constraint == 'integer': if not is_integer(value): return 'it is not an integer.' elif constraint == 'number': if not is_number(value): return 'it is not a number.' elif constraint == 'boolean': if not is_boolean(value): return 'it is not a boolean.' elif constraint == 'null' or constraint is None: if value is not None: return 'it is not null.'
def test_is_boolean(self): assert is_boolean(True) is True assert is_boolean(False) is True assert is_boolean(0) is False assert is_boolean(0.0) is False assert is_boolean('Bob') is False assert is_boolean([1]) is False assert is_boolean((1,)) is False assert is_boolean({}) is False
def _validate_unique_items(self, value, schema, constraint, path): if is_boolean(constraint) and constraint and is_array(value): count = len(value) for outer in range(count - 1): for inner in range(outer + 1, count): if value[outer] == value[inner]: return f'entry {outer} is the same as entry {inner}.'
def _get_additional_schema(name, schema): additional = True if name in schema: additional = schema[name] if is_boolean(additional): additional = empty_schema if additional else None return additional
def _get_exclusive(name, schema): exclusive = schema[name] if name in schema else False return exclusive if is_boolean(exclusive) else False