A simple schema builder for value parsing and validation
Project description
PyYep
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
Install
pip install PyYep
Getting Started
Basic usage
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
Validation using the Schema and InputItem objects
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
Validation using the DictValidator directly
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
Table of Contents
API
String validation
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(),
})
min
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),
})
max
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),
})
Number validation
min
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),
})
max
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),
})
Boolean validation
to_be
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),
})
Array validation
of
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()
),
})
includes
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"),
})
len
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),
})
min
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),
})
max
# 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.
Dict validation
shape
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()
})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file PyYep-1.5.1.tar.gz
.
File metadata
- Download URL: PyYep-1.5.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d85c9b1bc891cfc42b39e802719dee919e12140f139dbd7ab8770c08d25d7f0 |
|
MD5 | 8b2237ba1b133cb6f106ab36e1f15d0a |
|
BLAKE2b-256 | 597d15d0821468050f1c498b26a79c294f1b26e6d84c2bd37e40e285ebb100c5 |
File details
Details for the file PyYep-1.5.1-py3-none-any.whl
.
File metadata
- Download URL: PyYep-1.5.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66c78ce2c57b75d6da8000518e81b99efd6505caa471f2777f005fcdb9e8388e |
|
MD5 | 250e18b96e519b82a4c5e4ae86941067 |
|
BLAKE2b-256 | 06794aeec1b199fcaaf6b9bf78401fbea362b2ec653d4ec4246c195def9f1d9c |