A dead-simple utility that validates if object has a certain structure.
Project description
Simple schema validator
A dead-simple utility that validates if object has a certain structure. Used in some of our projects.
Basic usage
pip install simple_schema_validator
An example:
Lets say we have an API that returns the following data:
{
"user": 1,
"profile": {
"email": "some@user.com",
"name": "Some User",
"age": 20
},
"tokens": {
"jwt": "...",
"refresh": "...",
"firebase": "...",
}
}
And we are writing a simple integration test, that wants to assure the response has a certain structure.
Then we can use the schema validator like so:
from simple_schema_validator import schema_validator
data = get_data_from_api()
schema = {
'user': Any,
'profile': {
'email': Any,
'name': Any,
'age': Any
},
'tokens': {
'jwt': Any,
'refresh': Any,
'firebase': Any
}
}
validation = schema_validator(schema, data)
if not result:
print(f'Keys in data, but not in schema: {validation.additional_keys}')
print(f'Keys in schema, but not in data: {validation.missing_keys}')
print(f'Keys with different type from schema {validation.type_errors}')
missing_keys
are those keys that are required in theschema
, but not found indata
.additional_keys
are those keys present indata
, but not required by theschema
.validation_errors
are those keys, that are having a different type indata
, from the defined inschema
.
Nested keys are represented with "dot" notation - profile.email
, tokens.jwt
, etc.
Type checking
The util supports simple schema type checking.
Currently, the supported types in the schema are:
int
float
str
bool
typing.Any
(from Pythontyping
library)simple_schema_validator.types.Optional
(custom type, define in the package)
If the type is Any
, no type checking is done.
If there's a type mismatch, the errors are placed in the type_errors
attribute of the result, which is a list of type errors.
The general format of a single type error is:
{
'path': 'the.path.to.the.value.in.data',
'expected': the_expected_type_as_defined_in_the_schema,
'actual': the_actual_type_of_the_value
}
Here's an example:
from simple_schema_validator import schema_validator, types
schema = {
'user': str,
'profile': {
'email': str,
'name': str,
'age': int
},
'tokens': {
'jwt': str,
'refresh': str,
'firebase': str
}
}
data = {
'user': 'Some User',
'profile': {
'email': 'someuser@hacksoft.io',
'name': 'Some User',
'age': "29"
},
'tokens': {
'jwt': 'some token value',
'refresh': 'some token value',
'firebase': 'some token value'
}
}
result = schema_validator(schema, data)
assert bool(result) is False
assert result.type_errors == [{'path': 'profile.age', 'expected': int, 'actual': str}]
Optional types
The schema validator support optional types.
You can do the following:
from simple_schema_validator import schema_validator, types
schema = {
'a': types.Optional[int]
}
data_1 = {
'a': None
}
data_2 = {
'a': 1
}
data_3 = {
'a': 'some_string'
}
assert bool(schema_validator(schema, data_1)) is True
assert bool(schema_validator(schema, data_2)) is True
assert bool(schema_validator(schema, data_3)) is False
Additionally, you can define optional branches in the schema:
from simple_schema_validator import schema_validator, types
schema = {
'a': types.Optional[{
'b': int
}]
}
data_1 = {
'a': None
}
data_2 = {
'a': 1
}
data_3 = {
'a': {
'b': 1
}
}
data_4 = {
'a': {
'b': 'some_string'
}
}
assert bool(schema_validator(schema, data_1)) is True
assert bool(schema_validator(schema, data_2)) is False
assert bool(schema_validator(schema, data_3)) is True
assert bool(schema_validator(schema, data_4)) is False
Examples
For examples, check the examples folder or the tests for the project.
Project details
Release history Release notifications | RSS feed
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 simple_schema_validator-0.0.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | baf6db4b9bc31b6543d817a594dd3ab2ad0d3583f758df42830baad6265494d7 |
|
MD5 | d8f7cb06eaeb1df0b32508d2f807d182 |
|
BLAKE2b-256 | 33ead958e1b31804d2597ebf6f2311934491b9a6fadce1e68c02007ef43372a5 |
Hashes for simple_schema_validator-0.0.8-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af180cf17452eb09990335f70ecb7f66d19923b7a2640aa513c45b764f4ef509 |
|
MD5 | f838271f1e7d8e65a60ef4750ee4734f |
|
BLAKE2b-256 | 12f3fd6e0012d8e1b3f2a0dafd5080f4ff22111576846197845504a459ebe09d |