Primitives
Primitives
z.str() # Validate that the data is of type `str`.
z.int() # Validate that the data is of type `int`.
z.float() # Validate that the data is of type `float`.
z.bool() # Validate that the data is of type `bool`.
z.none() # Validate that the data is of type `None`.
z.datetime() # Validate that the data is of type `datetime`.
z.any() # Validate that the data is of any type.
These schemas are simple type validations and do not provide type conversion. Here's an equivalent example:
list
>>> z.list(z.transform(int)).parse(['1', 2, '3'])
[1, 2, 3]
# equivalent to:
>>> z.ensure(lambda x: isinstance(x, list)).transform(lambda x: [z.transform(int).parse(i) for i in x]).parse(['1', 2, '3'])
[1, 2, 3]
Conversions
The available type conversions are located in the namespace named to
.
str
int
>>> assert z.to.int().parse('1') == 1
>>> assert z.to.int().parse('1.0') == 1
>>> z.to.int().parse(1.2)
Traceback (most recent call last):
zangar.exceptions.ValidationError: [{'msgs': ['1.2 is not a valid integer']}]
This is the conversion function for int
.
def int_convert(value):
f = float(value)
i = int(f)
if f != i:
raise ValidationError(f"{value!r} is not a valid integer")
return i
float
>>> assert z.to.float().parse('1.0') == 1.0
# equivalent to:
>>> assert z.transform(float).parse('1.0') == 1.0