pythonish-validator
Data validation library for Python without complex schemas. It’s how you write Python code:
🐍 Works with Python >= 3.7
from re import compile as regex
from pythonish_validator.common import Validator
validator = Validator({
'name': str,
'age': int,
'email': regex(r'^\w+@\w+.\w{2,5}$')
'skills': [str]
})
validator.is_valid({
'name': 'Georgy',
'age': 29,
'email': 'bugov@cpan.org'
'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)"
]
Basic typing module support
Supported types: List, Dict, Optional, Union.
from typing import Dict, List, Optional, Union
from pythonish_validator.common import validate
schema_example = {
'name': str,
'age': Optional[int], # None if undefined
'skill': Union[str, List[str]], # Awful API, but who cares...
'level_by_skill': Dict[str, str]
}
valid_data = {
'name': 'Georgy',
'age': None,
'skill': ['Python', 'ECMA Script'],
'level_by_skill': {
'Python': 'senior',
'ECMA Script': 'middle',
}
}
validator = validate(schema_example, valid_example)
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).
Metadata
Release files for pythonish-validator 0.5.1
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 |
|---|---|---|---|---|
| pythonish-validator-0.5.1.tar.gz | 6.3 kB | Without provenance | Details for pythonish-validator-0.5.1.tar.gz |
Release files / pythonish-validator-0.5.1.tar.gz
| Download URL | pythonish-validator-0.5.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5df6573145bee74fa63585d7fd44a6d8c6ad6f43911220a199952a0975a9c830
|
|
BLAKE2b-256 checksum How to use checksums |
96f0a0219cd2cb0f080f821c449cb265e85d556e0951a52de70be0fafcb573ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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.4
|
Security for pythonish-validator 0.5.1
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.