Installation
You can install it directly from PyPi https://pypi.org/project/easyvalid-data-validator/
pip install easyvalid-data-validator
Tests
All functions are fully tested
You are able to run tests on your own cloning repo and using it's environment:
git clone https://github.com/DSmolke/EASYVALID_DATA_VALIDATOR.git
cd EASYVALID_DATA_VALIDATOR
poetry:
poetry update
poetry shell
poetry run python -m unittest discover -v
or:
poetry run pytest -vv
pipenv:
pipenv shell
pipenv run python -m unittest discover -v
or:
pipenv run pytest -vv
pip:
python -m unittest discover -v
or:
pip install pytest
pytest -vv
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
Click to read documentation