UsageΒΆ

All validation is done by Validator subclasses, and the fields from valedictory.fields. The schema to validate data against is defined declaratively by subclassing Validator:

from valedictory import Validator, fields

class ContactValidator(Validator):
    name = fields.CharField()
    height = fields.IntegerField()
    date_of_birth = fields.DateField()

Validator instances are immutable, so the same instance can be reused to validate all your data.

contact_validator = ContactValidator()

Validation is done by calling the clean() method on a Validator instance. clean() will either returned the cleaned data, or raise an InvalidDataException. InvalidDataException instances store all the validation errors found when validating the data. These can be used to create an appropriate error response.

from valedictory.exceptions import InvalidDataException

input_data = json.loads(request.body)

try:
    cleaned_data = contact_validator.clean(input_data)

    # Save the data
    Contact.objects.create(**cleaned_data)

except InvalidDataException as errors:
    # Handle the error
    for path, message in errors.flatten():
        print("{0}: {1}".format('.'.join(path), message))