Skip to main content

A simple schema builder for value parsing and validation

Project description

PyYep CI PyPI - Version Downloads semantic-release Coverage Status

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

Docs

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

email

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyYep-1.5.1.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

PyYep-1.5.1-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page