PyYep is a python schema builder for value parsing and validation. Define a schema, transform a value to match and validate the inputs with existing validator or custom functions.
PyYep is heavily inspired by Yup
pip install PyYep
There are two ways of defining an schema, using the Schema and the InputItem objects or using an DictValidator to define the schema directly. On both methods you can chain multiple validation methods
You define and create schema objects with its inputs and validation methods. Then use the verify method to check the schema. A ValidationError will be raised if some input value does not match the validation.
from PyYep import Schema, InputItem, ValidationError
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.string().email().required(),
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.number().min(10).max(100).required(),
], abort_early=False)
# check validity
try:
result = schema.validate()
# handle result
except ValidationError:
# handle fail
You can also define the schema directly using the shape method of a DictValidator as shown bellow.
from PyYep import DictValidator, StringValidator, NumericValidator, ArrayValidator, ValidationError
schema = DictValidator().shape({
"string": StringValidator().email().required(),
"number": NumericValidator().min(8).max(10).required(),
"list": ArrayValidator().of(
NumericValidator().max(3).required()
).min(1).required(),
})
# check validity
data = { "string": "test", "number": 10, "list": [1, 2, 3] }
try:
result = schema.verify(data)
# handle result
except ValidationError:
# handle fail
Validates the value as an email address.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.string().email()
])
# Example using the DictValidator.
schema = DictValidator().shape({
"string": StringValidator().email(),
})
Set a minimum length limit for the string value.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.string().min(8)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"string": StringValidator().min(8),
})
Set a maximum length limit for the string value.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.string().max(10)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"string": StringValidator().max(10),
})
Set the minimum value allowed.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.number().min(5)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"number": NumericValidator().min(5),
})
Set the maximum value allowed.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.number().max(10)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"number": NumericValidator().max(10),
})
Set the expected boolean value. The "strict" flag defines if the validator should only accept bool values or attempt to cast the received value as a boolean.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.bool(strict=True).to_be(True)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"number": BooleanValidator(strict=True).to_be(True),
})
Specify the schema of iterable elements.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.array().of(NumericValidator().required())
])
# Example using the DictValidator.
schema = DictValidator().shape({
"array": ArrayValidator().of(
NumericValidator().required()
),
})
Requires the iterable to have a defined value as one of its values.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.array().includes("value")
])
# Example using the DictValidator.
schema = DictValidator().shape({
"array": ArrayValidator().includes("value"),
})
Set a specific length requirement for the iterable.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.array().len(3)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"array": ArrayValidator().len(3),
})
Set a minimum length limit for the iterable.
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.array().min(5)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"array": ArrayValidator().min(5),
})
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.array().max(5)
])
# Example using the DictValidator.
schema = DictValidator().shape({
"array": ArrayValidator().max(5),
})
Set a minimum length limit for the iterable.
Define the keys of the dict and the schemas for said keys
# Example using the Schema and InputItem objects.
schema = Schema([
InputItem("name", input_object, "path-to-input_object-value-property-or-method")
.dict().shape({ "value": StringValidator().required() })
])
# Example using the DictValidator.
schema = DictValidator().shape({
"value": StringValidator().required()
})