A library for validating that dictionary values meet certain sets of parameters. Much like form validators, but for dicts.
Project description
validator.py [](https://travis-ci.org/mansam/validator.py)
============
A library for validating that dictionary values meet certain sets of parameters. Much like form validators, but for dicts.
## Usage Example
First, install it from PyPI.
pip install validator.py
```python
from validator import Required, Not, Truthy, Blank, Range, Equals, In, validate
# let's say that my dictionary needs to meet the following rules...
rules = {
"foo": [Required, Equals(123)],
"bar": [Required, Truthy()],
"baz": [In(["spam", "eggs", "bacon"])],
"qux": [Not(Range(1, 100))] # by default, Range is inclusive
}
# then this following dict would pass:
passes = {
"foo": 123,
"bar": True, # or a non-empty string, or a non-zero int, etc...
"baz": "spam",
"qux": 101
}
print validate(rules, passes)
# (True, {})
# but this one would fail
fails = {
"foo": 321,
"bar": False, # or 0, or [], or an empty string, etc...
"baz": "barf",
"qux": 99
}
print validate(rules, fails)
# (False,
# {
# 'foo': ["must be equal to '123'"],
# 'bar': ['must be True-equivalent value'],
# 'baz': ["must be one of ['spam', 'eggs', 'bacon']"],
# 'qux': ['must not fall between 1 and 100']
# })
```
============
A library for validating that dictionary values meet certain sets of parameters. Much like form validators, but for dicts.
## Usage Example
First, install it from PyPI.
pip install validator.py
```python
from validator import Required, Not, Truthy, Blank, Range, Equals, In, validate
# let's say that my dictionary needs to meet the following rules...
rules = {
"foo": [Required, Equals(123)],
"bar": [Required, Truthy()],
"baz": [In(["spam", "eggs", "bacon"])],
"qux": [Not(Range(1, 100))] # by default, Range is inclusive
}
# then this following dict would pass:
passes = {
"foo": 123,
"bar": True, # or a non-empty string, or a non-zero int, etc...
"baz": "spam",
"qux": 101
}
print validate(rules, passes)
# (True, {})
# but this one would fail
fails = {
"foo": 321,
"bar": False, # or 0, or [], or an empty string, etc...
"baz": "barf",
"qux": 99
}
print validate(rules, fails)
# (False,
# {
# 'foo': ["must be equal to '123'"],
# 'bar': ['must be True-equivalent value'],
# 'baz': ["must be one of ['spam', 'eggs', 'bacon']"],
# 'qux': ['must not fall between 1 and 100']
# })
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
validator.py-0.4.0.tar.gz
(3.5 kB
view details)
File details
Details for the file validator.py-0.4.0.tar.gz.
File metadata
- Download URL: validator.py-0.4.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cd9f31b05aa3374d02ada175b0044b52d8f40ef438c2e3448b796f2e284ba10
|
|
| MD5 |
42d8a2be9728846c3f918e1e303113f0
|
|
| BLAKE2b-256 |
3820ed643c4455d0dab4972310ba894b9baa9845d4eb2ce9adbc9be012ad184e
|