Skip to main content

Effective dictionary and nested object validation

Project description

Build Status codecov

Valleydeight

Effective dictionary and nested object validation for Python

Lately, I've found myself writing many YAML-based config files. Being able to quickly and easily put together a schema for these files has become helpful, and the existing options out there were proving awkward to me.

The approach here is to work directly on the resulting python objects. This allows the code here to be useful in many other situations, and to validate other types of markup easily (eg JSON, XML (?), pickled primitives, etc).

Installation

(Coming soon:)

pip install --user valleydeight

Usage

To be able to validate an object, you must build up a Validator. Doing this is straight forward for most types. There are currently many types of Validators implemented:

  • Primitive types: Str, Int, Float, Bool
  • Lists of items: List, FixedList
  • Dictionaries of items: Dict
  • Mixed types: Choice
  • Custom objects: Object
  • A validator that accepts everything: Pass

To make a validator, simply instantiate one of the above classes, composing together the more complicated types where needed. To use the validator call it with the object you wish to validate.

For example, say we wish to check that we have a list of dictionaries where each dictionary has a string called "name" and a boolean called "on":

import valleydeight as vd

# Build the validator
validator = vd.List(vd.Dict(name=vd.Str(), on=vd.Bool()))

# Make a test object that should pass fine
test_object = [dict(name="hello", on=True), dict(name="World", on=False)]
parsed_object = validator(test_object)


# Make a test object that will fail, since one of the elements has the wrong type:
test_object = [dict(name="hello", on=True), dict(name="World", on=2018)]
parsed_object = validator(test_object)
# Raises ValidatorException

The Choice class allows us to make complicated "custom" types:

import valleydeight as vd

# Something like a pythonic Enum with mixed types:
enum_t = vd.Choice("one", 4, True)

# A mixture of validator types:
mix_t = vd.Choice(vd.Str(), vd.Dict(name=vd.Str(), value=vd.Pass()).opts(need_all_keys=True))

# A mixture of specific values and generic types
mix_t = vd.Choice(10012, False, vd.Str(), vd.List(vd.Float()))

The difference between a List and a FixedList is that a List allows an arbitrary number of items, which must all be the same type (although this can be a Choice type), whereas a FixedList has both a fixed length and specific types for each element.

Example program

For an example program see the script in the examples/ directory on GitHub. In addition the unit tests in the tests/ directory might be informative.

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

valleydeight-0.0.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

valleydeight-0.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file valleydeight-0.0.1.tar.gz.

File metadata

File hashes

Hashes for valleydeight-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a0847e1790ca6bd7cc3efb720d4f725df3b6076151afb1237aa04cd12f897083
MD5 36b483ac9f9e65c31ab8db1d53156d27
BLAKE2b-256 f4e6f128868faba9e5e93d54b2b24dd02a5e470ec5139ed1bb74bcf450fd8493

See more details on using hashes here.

File details

Details for the file valleydeight-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for valleydeight-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d12cdafb3ecc0fc6c316c21e81663d08eebe3d863435a95d279f8012adbdb834
MD5 34971a90ad927cadd9c2f2c9581cae39
BLAKE2b-256 147cad0ded7c75539aacc344b4243706f76936eb8bbdd224b2782b3f0109568c

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