Skip to main content

Validate dicts against a schema

Project description

https://badge.fury.io/py/valedictory.svg https://readthedocs.org/projects/valedictory/badge/?version=latest

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"],
}

Read the documentation for more information.

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

valedictory-0.7.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

valedictory-0.7.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file valedictory-0.7.0.tar.gz.

File metadata

  • Download URL: valedictory-0.7.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for valedictory-0.7.0.tar.gz
Algorithm Hash digest
SHA256 f9c47db0d70157cdf3bc3978038c9f0ffc2aa861479e1ccfd52e240b71ab4c51
MD5 ae06b2cfa122b91bae30a6cd12229514
BLAKE2b-256 c8aeee255e122172eb6c32acbfd88ce8035fcde209e05e68dd8c46e65495c82a

See more details on using hashes here.

File details

Details for the file valedictory-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for valedictory-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ead59e3dad9fa2578c3b35722d1271d3f6daafd908b322418437ef4cb7d684fa
MD5 27bdd0fb4b5babb2cd8d39cb917afb47
BLAKE2b-256 d226b9237ed972e4d10f8e19091fd94db612d54308ac252b0388c8b0b3348315

See more details on using hashes here.

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