Validate dicts against a schema
Project description
Valedictory validates the data in dicts. It is designed for use in API validation, and other situations where you are receiving structured JSON data as opposed to key-value POST form data. It takes in a dict of data (probably obtained from a JSON POST request), and validates that data against some fields.
Validators are defined as classes. Declare fields on a Validator class. Once constructed, Validator instances are immutable.
from valedictory import Validator, fields, InvalidDataException
class PersonValidator(Validator):
name = fields.CharField()
height = fields.IntegerField()
date_of_birth = fields.DateField()
person_validator = PersonValidator()
A Python dict can be checked to see if it conforms to this validator. The dict can come from a JSON POST request, or a configuration file, or any other external data source that needs validation and cleaning. The cleaned data will be returned. Validator classes will return a dict of cleaned data. Each field type may transform the data as part of cleaning it. For example, the DateField will transform the data into a datetime.date instance.
input_data = json.loads(request.body)
try:
# cleaned_data will be a dict of cleaned, validated data
cleaned_data = person_validator.clean(input_data)
# Do something with the returned data
Person.objects.create(**cleaned_data)
except InvalidDataException as errors:
# The data did not pass validation
for path, message in errors.flatten():
# This will print something like 'name: This field is required'
print("{0}: {1}".format('.'.join(path), message))
Validators can be nested, allowing dicts of arbitrary complexity:
class ArticleValidator(Validator):
content = fields.CharField()
published = fields.DateTimeField()
author = fields.NestedValidator(PersonValidator())
tags = fields.ListField(fields.CharField())
# Some example data that would pass validation:
data = {
"content": "An interesting article",
"published": "2018-06-13T1:44:00+10:00",
"author": {
"name": "Alex Smith",
"height": 175,
"date_of_birth": "1990-03-26",
},
"tags": ["humour", "interesting", "clickbait"],
}
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 valedictory-0.8.0.tar.gz
.
File metadata
- Download URL: valedictory-0.8.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901842f7ec988bd285fe3aa4e5591dd3bbfb108076e08b0d5e6f5d64b451e419 |
|
MD5 | bb5408499f8098df218c1cfadb71e4d2 |
|
BLAKE2b-256 | 72b7f26efceb2052b2fa326a89199a471ad5ed4adc60cf4ab8ddf2e822f5493f |
File details
Details for the file valedictory-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: valedictory-0.8.0-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c048ee36e53b28a39aa75e663b26121ffca20a9dc930cacf62ce82b61267371 |
|
MD5 | 546c74e3c9c613358ea3d36e2afe739d |
|
BLAKE2b-256 | ab8ee76815ecd3f69260593a2549742f0ef9ea9a5ebee9fd9f59fe771cae8997 |