No project description provided
Project description
Installation
You can install it directly from PyPi
pip install easyvalid-data-validator
Tests
All functions are fully tested
You are able to run tests on your own by using this command being in package directory
poetry run python -m unittest discover -v
or
poetry run pytest
easyvalid-data-validator
It's a package developed mainly for validation of json dict that is created by using json.load().
Here is an example of json dict, that has name, age, and balance.
user = {
"name": "ADAM",
"age": 18,
"balance": "2000.00"
}
We want to validate if:
- name contain only uppercase letters,
- age is greater or equal to 18,
- balance is valid for Decimal conversion
We need to prepare constraint dict which describes this rules as explained:
constraints = {
"key_name1": {<ConstraintEnumObject>: *args},
"key_name2": {<ConstraintEnumObject>: *args},
"key_name3": {<ConstraintEnumObject>: *args}
}
So we create dict that stores dicts containing Constraint Objects as key that are indicators for validator of which case it's currently working on, and what datachecker it should use. Value should be arguments that datachecker need:
- Constraint Object - Enum object
- datachecker - function that takes needed arguments and returns True or False if condition is mached
- validator - validator function that raises error when any of value is not valid, or returns data when it's valid
from easyvalid_data_validator.constraints import Constraint
constraints = {
"name": {Constraint.STRING_REGEX: r'^[A-Z]+$'},
"age": {Constraint.INT_GE: 18},
"balance": {Constraint.STRING_IS_DECIMAL: None}
}
Validation is very easy now, we just need to provide validate_json_data() with json_data, and constraints:
from easyvalid_data_validator.validator import validate_json_data
result = validate_json_data(user, constraints)
# result --> {"name": "ADAM", "age": 18, "balance": "2000.00"}
If we would change age of user to 17, validator would throw an error:
ValidationError("age": ["Invalid integer expression - isn't grater or equal to compare value"])
Documentation link
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
Built Distribution
Hashes for easyvalid_data_validator-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42ac789bd029bd4a0a4706646afa7bc97ebeb240530c8b9869324ec920fedef7 |
|
MD5 | 1e1094cccdedb933f5ea89d6e8a36100 |
|
BLAKE2b-256 | 24de0b13360696b776de19661ec1910b4ff72f10b3f45bdec9bd1b421a2ad8d8 |
Hashes for easyvalid_data_validator-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c30245135f2c098275d8e76bdfe54145cbc09ea9fa8e9cb5b45a5cb396a2c70 |
|
MD5 | 3174e035d0bf3f06cac695775a474ada |
|
BLAKE2b-256 | 3a2b4fd740ce67402bf35d78361a14b6ec64f931a81e93e1516eda47a0463b8f |