Schema checker
Another schema validator :)
Features:
- Can validate any dict object (not only json)
- Very customizeble
Tutorial
Base validation function:
def validate(obj: dict, schema: dict) -> dict:
...
where
obj - some object
schema - schema_checker
schema ::= type of this object : list/dict/str/int/float (can be tuple of types) or "const"/"enum"
OR
schema ::= dict - {
type : type of this object : "list/tuple/dict/str/int/float or "const"
"value" : need for obj type of
- list/tuple - is schema for all elements in list
- dict - dict[key -> schema]
- const - some value to be compared with using method
- enum - list/set/dict/tuple to check if obj __contains__ in "value"
"any_key" : need for obj type of dict - schema for all keys (ignores if value is set)
"default" : default value if this object does not exists (if callable will be called)
"filter" : any of
- Callable[value -> bool] - if false then raise error
- Iterable[Callable[value -> bool]] - if any of them return false then raise error
"pre_call" : any of
- Callable[value -> value] - will be called before checking type and call filter's functions
- Iterable[Callable[value -> value]] - will call all of them
"post_call" : any of
- Callable[value -> value] - will be called after checking type and call filter's functions
- Iterable[Callable[value -> value]] - will call all of them
"blank" : raise error if value is blank
"max_length" : extra check of length (len)
"min_length" : extra check of length (len)
"unexpected" : allow unexpected keys (for dict)
"errmsg" : will be in ValueError in case of error on this level
}
Extras
decorator_constructor
def decorator_constructor(getter, setter)
getter must:
- take same args as the function that'll be decorated
- return dict for the schema validator
setter must:
- take 3 args: validated dict, source positional args as tuple, sourse keyword args as dict
- return tuple and dict for positional and keywords args for the function tha'll be decorated
returns parameterized decorator, that expects schema
kw_validator
def kw_validator(schema)
Validate only keyword args and ignores all positional This decorator is the result of decorator_constructor
pos_validator
def pos_validator(schema)
Validate only positional args and ignores all keywords This decorator is the result of decorator_constructor
args_validator
def args_validator(pos_schema: Dict[str, Any], kw_schema: Dict[str, Any]):
Validate both positional and keywords args
Examples
from datetime import datetime, timedelta
from schema_checker import validate, kw_validator
# validate that obj is str
validate(
obj='12345',
schema={'type': str},
) # result: '12345'
validate(
obj=12345,
schema={'type': str},
) # raise ValueError
# check if value is int and less then 5
validate(
obj={'some_key': 10},
schema={
'type': dict,
'value': {
'some_key': {
'type': int,
'filter': lambda x: x < 5,
},
}
},
) # raise ValueError
# convert obj to datetime and compare with today's date
validate(
obj='10.12.19',
schema={
'type': datetime,
'pre_call': lambda x: datetime.strptime(x, '%d.%m.%y'),
'filter': lambda x: (datetime.today() - timedelta(year=1)) <= x <= datetime.today(),
},
) # result: datetime.datetime(2019, 12, 10, 0, 0)
@kw_validator({'type': dict, 'values': {'a': str}})
def func(a):
return a
func(123) # ok
func('123') # ok
func(a='123') # ok
func(a=123) # raise ValueError
Metadata
Release files for schema-checker 1.1.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 |
|---|---|---|---|---|
| schema_checker-1.1.1.tar.gz | 6.7 kB | Without provenance | Details for schema_checker-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| schema_checker-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for schema_checker-1.1.1-py3-none-any.whl |
Total release size: 15.5 kB
Release files / schema_checker-1.1.1.tar.gz
| Download URL | schema_checker-1.1.1.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3a06208f8a6fb7ab752887b4ab493c72c2dcda15c3de1f3544e618c37dfff83
|
|
BLAKE2b-256 checksum How to use checksums |
0e1019a8af0d3ec4cccdb4471144e04b706824f6d597d0722c8b2713a5908c43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
|
Release files / schema_checker-1.1.1-py3-none-any.whl
| Download URL | schema_checker-1.1.1-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e92e9211bffaaf2f3a1bf171742dcd1420de4b29b79e689ea3444b7935a38dbc
|
|
BLAKE2b-256 checksum How to use checksums |
a874fa6e181932919943b521fd691651a7c8527c318aaeb517b1b6c52c021a2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
|
Security for schema-checker 1.1.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.