Skip to main content
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.

Release files for valedictory 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for valedictory 0.6.0
File Size Uploaded
valedictory-0.6.0.tar.gz 16.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for valedictory 0.6.0
File Interpreter ABI Platform
valedictory-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.2 kB

Release files / valedictory-0.6.0.tar.gz

Download URL valedictory-0.6.0.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5c21b4d3f3d7ebfe1a6337eeeab873f6016874cce73981541d738e3c4159eb60
BLAKE2b-256 checksum
How to use checksums
5c1010812b63869e6e16a0b645c4baf46c851359f26d40b96fe3c323c4d9fc00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / valedictory-0.6.0-py3-none-any.whl

Download URL valedictory-0.6.0-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f40701961ef156e70f5c4924c3376db650a1ccc72c2de0757852d7d294aa39d9
BLAKE2b-256 checksum
How to use checksums
e01f8a5593c0f70133784c33634c4add45efd250eb1342c02caa5ac3bf81f481
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.9.0

1 release file

0.8.0

2 release files

0.7.0

2 release files

This release

0.6.0 This release

2 release files

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page