kohi
A powerfull schema validator
Instalation
Via Poetry:
poetry add kohi
Via PIP:
pip install kohi
Via GitHub (recommended only in dev env):
git clone https://github.com/natanfeitosa/kohi.git && cd kohi && pip install .
Quickstart
To validate a type you can import your schema validator from kohi or from kohi.<type> import <type>Schema
e.g.
Let's check if a person's date of birth is a positive integer less than the current date — 2023 — and greater than or equal to 2005
from kohi import NumberSchema
# from kohi.number import NumberSchema
n = NumberSchema().int().positive().lt(2023).gte(2005)
print(n.validate(2005)) # True
print(n.validate(2022)) # True
print(n.validate(2004)) # False
print(n.validate(2023)) # False
# You can see the errors generated in the last `validate` call just by accessing the `errors` property
# print(n.errors) # ['number must be less than 2022']
Validators
Only one base class for all schema validators
or
kohi.NumberSchema
or
kohi.StringSchema
or
kohi.EnumSchema
or
kohi.DictSchema
Methods
BaseSchema
add_validator(name, func): SelfAdd a custom data validator
validate(data): boolThe method to be called when we validate the schema
reset(): NoneReset error list
throw(): SelfBy default no errors are thrown, but when this method is chained a
ValidationErrorwill be thrownadd_mutation(): SelfAdd a mutation function than will run after the
validatemethod. P.S. Will only be executed in theparsemethodparse(data): typeof dataRun the
validatemethod, the mutations and return a deep clone of datadefault(data): SelfSet a default value for when the validator receives None and you don't want to generate an error
optional(): SelfAllow values None
required(error_message=None): SelfMark the schema as required. Does not allow values None
NumberSchema
inherits from BaseSchema
By default validates int and float
float(): SelfValidate only
floatint(): SelfValidate only
intlt(num): SelfValidates if the data is less than
numgt(num): SelfValidates if the data is greater than
numlte(num): SelfValidates if the data is less than or equal to
numgte(num): SelfValidates if the data is greater than or equal to
nummin(num): SelfJust an alias for
gte(num)max(num): SelfJust an alias for
lte(nun)positive(): SelfJust an alias for
gt(0)negative(): SelfJust an alias for
lt(0)nonpositive(): SelfJust an alias for
lte(0)nonnegative(): SelfJust an alias for
gte(0)
StringSchema
inherits from BaseSchema
min(min_length): SelfValidate if the data len is greater than or equal to min_length
length(length): SelfValidate if the data len equal to length
max(max_length): SelfValidate if the data len is less than or equal to max_length
url(): SelfValidate if the data is an url
uuid(): SelfValidate if the data is a valid uuid
starts_with(text): SelfValidate if the data starts with text
ends_with(text): SelfValidate if the data ends with text
EnumSchema
inherits from BaseSchema
one_of(opts): SelfValidate if the data is in opts
not_one_of(opts): SelfValidate that data is different from the values in opts
DictSchema
inherits from BaseSchema
props(**props): SelfDefines the structure of the dictionary in the format
[key]: ClassValidator
Dev env
- install development dependencies
- check types using
mypy - run all tests using
pytest
Metadata
Release files for kohi 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kohi-0.4.0.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kohi-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.9 kB
Release files / kohi-0.4.0.tar.gz
| Download URL | kohi-0.4.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f8d968ace0bf45d512058bdeb9b6e2f48a562463aed5d8731d22b7fbb8c65f33
|
|
BLAKE2b-256 checksum How to use checksums |
db3d7ecf6bd1b8eee5c821cb032eb73bbe74f7de1a5f71ab17077b972bf85206
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.8.13 Linux/5.4.0-137-generic
|
Release files / kohi-0.4.0-py3-none-any.whl
| Download URL | kohi-0.4.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
860c5ed5144c49067628ffff4f3a7f418473683b6674edc5a0e3ea5be6b4a67e
|
|
BLAKE2b-256 checksum How to use checksums |
8ebf6fd20c134ba2425790b498600c00ab6b2a739d4c33079569394660783489
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.8.13 Linux/5.4.0-137-generic
|