Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert error is None
validator = Union(any_of=[Integer(), String()])
value, error = validator.validate_or_error(123)
assert value == 123
assert error is None
validator = Union(any_of=[Integer(), String()])
value, error = validator.validate_or_error(None)
assert value is None
assert error == ValidationError(text="May not be null.", code="null")
validator = Union(any_of=[Integer(), String()])
value, error = validator.validate_or_error(True)
assert value is None
assert error == ValidationError(text="Did not match any valid type.", code="union")
validator = Union(any_of=[Integer(allow_null=True), String()])
value, error = validator.validate_or_error(None)
assert error is None
assert value is None
validator = Union(any_of=[Integer(), String()], allow_null=True)
value, error = validator.validate_or_error(None)
assert error is None
assert value is None
validator = Union(any_of=[Integer(maximum=1000), String()])
value, error = validator.validate_or_error(9999)
assert value is None
assert error == ValidationError(
text="Must be less than or equal to 1000.", code="maximum"
def test_string():
validator = String()
validated = validator.validate_or_error("abc")
assert validated
assert validated.value == "abc"
assert validated.error is None
validator = String()
value, error = validator.validate_or_error("")
assert error == ValidationError(text="Must not be blank.", code="blank")
validator = String()
value, error = validator.validate_or_error(None)
assert error == ValidationError(text="May not be null.", code="null")
validator = String()
value, error = validator.validate_or_error(123)
assert error == ValidationError(text="Must be a string.", code="type")
validator = String(max_length=10)
value, error = validator.validate_or_error("abc" * 10)
assert error == ValidationError(
text="Must have no more than 10 characters.", code="max_length"
)
validator = String(min_length=3)
def test_choice():
validator = Choice(choices=[("R", "red"), ("B", "blue"), ("G", "green")])
value, error = validator.validate_or_error(None)
assert error == ValidationError(text="May not be null.", code="null")
validator = Choice(
choices=[("R", "red"), ("B", "blue"), ("G", "green")], allow_null=True
)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Choice(choices=[("R", "red"), ("B", "blue"), ("G", "green")])
value, error = validator.validate_or_error("")
assert error == ValidationError(text="This field is required.", code="required")
validator = Choice(
choices=[("R", "red"), ("B", "blue"), ("G", "green")], allow_null=True
)
value, error = validator.validate_or_error("")
assert value is None
assert error is None
validator = Choice(choices=[("R", "red"), ("B", "blue"), ("G", "green")])
value, error = validator.validate_or_error("Z")
assert error == ValidationError(text="Not a valid choice.", code="choice")
validator = Choice(choices=[("R", "red"), ("B", "blue"), ("G", "green")])
value, error = validator.validate_or_error("R")
assert value == "R"
def test_date():
validator = Date()
value, error = validator.validate_or_error("2049-01-01")
assert value == datetime.date(2049, 1, 1)
validator = Date()
value, error = validator.validate_or_error(datetime.date(2049, 1, 1))
assert value == datetime.date(2049, 1, 1)
validator = Date()
value, error = validator.validate_or_error("20490101")
assert error == ValidationError(text="Must be a valid date format.", code="format")
validator = Date()
value, error = validator.validate_or_error("2049-01-32")
assert error == ValidationError(text="Must be a real date.", code="invalid")
validator = Boolean()
value, error = validator.validate_or_error("True")
assert value is True
validator = Boolean()
value, error = validator.validate_or_error(1)
assert value is True
validator = Boolean()
value, error = validator.validate_or_error(None)
assert error == ValidationError(text="May not be null.", code="null")
validator = Boolean()
value, error = validator.validate_or_error(2)
assert error == ValidationError(text="Must be a boolean.", code="type")
validator = Boolean()
value, error = validator.validate_or_error([])
assert error == ValidationError(text="Must be a boolean.", code="type")
validator = Boolean(allow_null=True)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Boolean(allow_null=True)
value, error = validator.validate_or_error("")
assert value is None
assert error is None
validator = Boolean()
def test_const():
validator = Const(const=None)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Const(const=None)
value, error = validator.validate_or_error(123)
assert value is None
assert error == ValidationError(text="Must be null.", code="only_null")
validator = Const(const="abc")
value, error = validator.validate_or_error("def")
assert value is None
assert error == ValidationError(text="Must be the value 'abc'.", code="const")
validator = Const(const="abc")
value, error = validator.validate_or_error("abc")
assert value == "abc"
assert error is None
item, error = validator.validate_or_error(item, strict=strict)
if error:
error_messages += error.messages(add_prefix=pos)
else:
validated.append(item)
if self.unique_items:
if item in seen_items:
text = self.get_error_text("unique_items")
message = Message(text=text, code="unique_items", key=pos)
error_messages.append(message)
else:
seen_items.add(item)
if error_messages:
raise ValidationError(messages=error_messages)
return validated
def validation_error(self, code: str) -> ValidationError:
text = self.errors[code].format(**self.__dict__)
return ValidationError(text=text, code=code)
def validate_or_error(
self, value: typing.Any, *, strict: bool = False
) -> ValidationResult:
try:
value = self.validate(value, strict=strict)
except ValidationError as error:
return ValidationResult(value=None, error=error)
return ValidationResult(value=value, error=None)
def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, ValidationError) and self._messages == other._messages