Pythonish object scheme validator
Project description
Data validation library for Python without complex schemas. It’s how you write Python code:
🐍 Works with Python >= 3.7
from pythonish_validator.common import Validator
validator = Validator({
'name': str,
'age': int,
'skills': [str]
})
validator.is_valid({
'name': 'Georgy',
'age': 29,
'skills': ['Python', 'Perl', 'C']
})
What can be easier?
Install
pip3 install pythonish-validator
Error messages
from pythonish_validator.common import validate
validator = validate({
'name': str,
'age': int,
'skills': [str]
}, {
'name': 'Georgy',
'age': None,
'skills': ['Python', 'Perl', 42]
})
assert validator.repr_errors() == [
"{'age'}->NoneType(None)",
"{'skills'}->[2]->int(42)"
]
Features
🗣️ Speak the language of Python classes:
from pythonish_validator.common import Validator
class User:
__validation_schema__ = {
'id': int,
'name': str
}
validator = Validator({
"users": [User]
})
# valid structure
validator.is_valid({
"users": [
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'},
]
})
# invalid structure
validator.is_valid({
"users": [
{'id': '1', 'name': 'Alice'},
{'id': 2},
]
})
assert validator.repr_errors() == [
"{'users'}->[0]->{'id'}->str('1')",
"{'users'}->[1]->{'name'}",
]
🎓 And even custom validation:
import re
from pythonish_validator.common import Validator
class EmailType:
@staticmethod
def __validation_schema__(data):
if not isinstance(data, str):
return False
if re.match(r'\w+@\w+.\w{2,5}', data) is None:
return False
return True
class User:
__validation_schema__ = {
'id': int,
'name': str,
'email': EmailType,
}
validator = Validator({
"users": [User]
})
validator.is_valid({
"users": [
{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'},
{'id': 2, 'name': 'Bob', 'email': 'bob@example.com'},
]
})
If you find any mistake – please write to the issue list 🐨 (https://github.com/bugov/pythonish-validator/issues).
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
File details
Details for the file pythonish-validator-0.4.1.tar.gz
.
File metadata
- Download URL: pythonish-validator-0.4.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b425648fe8ecbc50f8e44016c153f240bf19be50f9fe9440894e11d3bb9a2f6d |
|
MD5 | bda4ab70fd1425e47d8dee54fef136ff |
|
BLAKE2b-256 | 8703bc0e1305ba271fbabf6e6b81b909aed9be09efafe9ef9f1cc12a16e3717c |