def _validate_minimum(self, value, schema, constraint, path): if is_number(value) and is_number(constraint): if self._get_exclusive('exclusiveMinimum', schema): if value < constraint: return f'{value} is less than {constraint}.' else: if value <= constraint: return f'{value} is less than or equal to {constraint}.'
def _validate_maximum(self, value, schema, constraint, path): if is_number(value) and is_number(constraint): if self._get_exclusive('exclusiveMaximum', schema): if value > constraint: return f'{value} is greater than {constraint}.' else: if value >= constraint: return f'{value} is greater than or equal to {constraint}.'
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_number(self): assert is_number(True) is False assert is_number(False) is False assert is_number(0) is True assert is_number(0.0) is True assert is_number('Bob') is False assert is_number([1]) is False assert is_number((1,)) is False assert is_number({}) is False
def test_is_string(self): assert is_string(True) is False assert is_number(False) is False assert is_string(0) is False assert is_string(0.0) is False assert is_string('Bob') is True assert is_string([1]) is False assert is_string((1,)) is False assert is_string({}) is False
def test_is_array(self): assert is_array(True) is False assert is_number(False) is False assert is_array(0) is False assert is_array(0.0) is False assert is_array('Bob') is False assert is_array([1]) is True assert is_array((1,)) is False assert is_array({}) is False
def test_is_object(self): assert is_object(True) is False assert is_number(False) is False assert is_object(0) is False assert is_object(0.0) is False assert is_object('Bob') is False assert is_object([1]) is False assert is_object((1,)) is False assert is_object({}) is True assert is_object(OrderedDict()) is True
def _validate_max_items(self, value, schema, constraint, path): if is_array(value) and is_number(constraint): if len(value) > constraint: return f'the array can have no more than {constraint} item{"" if constraint == 1 else "s"}.'
def _validate_min_items(self, value, schema, constraint, path): if is_array(value) and is_number(constraint): if len(value) < constraint: return f'the array needs at least {constraint} item{"" if constraint == 1 else "s"}.'
def _validate_max_properties(self, value, schema, constraint, path): if is_object(value) and is_number(constraint): if len(value) > constraint: # noinspection SpellCheckingInspection return f'the object can have no more than {constraint} propert{"y" if constraint == 1 else "ies"}.'
def _validate_min_properties(self, value, schema, constraint, path): if is_object(value) and is_number(constraint): if len(value) < constraint: # noinspection SpellCheckingInspection return f'the object needs at least {constraint} propert{"y" if constraint == 1 else "ies"}.'
def _validate_multiple_of(self, value, schema, constraint, path): if is_number(value) and is_number(constraint): if value % constraint != 0: return f'{value} is not a multiple of {constraint}.'
def _validate_max_length(self, value, schema, constraint, path): if is_string(value) and is_number(constraint): if len(value) > constraint: return f'the string is longer than {constraint}.'