Skip to main content

PyValidator

Project description

schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.

PyValidator is an (almost) drop-in replacement of Python Schema, written almost from scratch for the sake of better error handling.

Examples (From python schema)

Here is a quick example to get a feeling of schema, validating a list of entries with personal information:

>>> from pyvalidator import Validator, And, Use, Optional

>>> validator = Validator([{'name': And(str, len),
...                   'age':  And(Use(int), lambda n: 18 <= n <= 99),
...                   Optional('sex'): And(str, Use(str.lower),
...                                        lambda s: s in ('male', 'female'))}])

>>> data = [{'name': 'Sue', 'age': '28', 'sex': 'FEMALE'},
...         {'name': 'Sam', 'age': '42'},
...         {'name': 'Sacha', 'age': '20', 'sex': 'Male'}]

>>> validated = validator.validate(data)

>>> assert validated == [{'name': 'Sue', 'age': 28, 'sex': 'female'},
...                      {'name': 'Sam', 'age': 42},
...                      {'name': 'Sacha', 'age' : 20, 'sex': 'male'}]

If data is valid, Validator.validate will return the validated data (optionally converted with Use calls, see below).

If data is invalid, Schema will raise SchemaError exception.

Installation

Use pip or easy_install:

pip install pyvalidator

How Validator validates data

Types

If Validator(...) encounters a type (such as int, str, object, etc.), it will check if the corresponding piece of data is an instance of that type, otherwise it will raise ErrorBucket.

>>> from pyvalidator import Validator

>>> Validator(int).validate(123)
123

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

pyvalidator-0.0.4.tar.gz (8.5 kB view details)

Uploaded Source

File details

Details for the file pyvalidator-0.0.4.tar.gz.

File metadata

  • Download URL: pyvalidator-0.0.4.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyvalidator-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8a76ad0d4e721adfe5870a708ea134c6cfaab1cac5500a497082a6593990c478
MD5 473e42bec251556b817e0bef87c7b7eb
BLAKE2b-256 742f6bff6108904354f4dbe6e9a1a64308334decc5c074539c5a9afe927a018d

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