Valleydeight
Pronounced like: "validate" [val-i-deyt]
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
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.
Metadata
Release files for valleydeight 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| valleydeight-0.0.4.tar.gz | 4.6 kB | Without provenance | Details for valleydeight-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| valleydeight-0.0.4-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for valleydeight-0.0.4-py2.py3-none-any.whl |
Total release size: 11.7 kB
Release files / valleydeight-0.0.4.tar.gz
| Download URL | valleydeight-0.0.4.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c67f97e80463218beeebef07b59b536c1ef03470d3ead719da9a9257b70b1d2
|
|
BLAKE2b-256 checksum How to use checksums |
4203912ddbeb346f8e013e52bf291cfa1a1e2c865a892a7f654c825231bf5088
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14
|
Release files / valleydeight-0.0.4-py2.py3-none-any.whl
| Download URL | valleydeight-0.0.4-py2.py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b3146d07bba7c9d1fbc4fb38886dda6b9999cad75071ed363359af734d3f8a1c
|
|
BLAKE2b-256 checksum How to use checksums |
1c5b47c173377dc3d521636e86720e00677fa84fc5c1a5230cae4305f92b8682
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14
|
Security for valleydeight 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.