PyHamcrest matchers extension for json schema validation
The schema should be implemented following syntax of Draft-07 to 2019-09 version. Schema example:
single_user_schema = {
"title": "Single User Info",
"type": "object",
"properties": {
"first_name": {
"type": "string",
"minLength": 3
},
"last_name": {
"type": "string",
"minLength": 3
},
"phone_number": {
"type": "number"
}
},
"required": [
"first_name",
"last_name",
]
}
all_users_info_schema = {
"title": "All Users Info",
"type": "array",
"items": single_user_schema
}
Then you can use matches_json_schema matcher with all hamcrest matchers:
from hamcrest import assert_that
from json_schema_matchers.common_matcher import matches_json_schema
users_list_json_obj = [
{
"first_name": 'John',
"last_name": "Johnson",
"phone_number": 123456789
},
{
"first_name": 'Jim',
"last_name": 'Jefferson'
}
]
assert_that(users_list_json_obj, matches_json_schema(all_users_info_schema))
assert_that(users_list_json_obj[0], matches_json_schema(single_user_schema))
In case of validation fail the matcher will return all mismatches iteratively:
users_list_json_obj = [
{
"first_name": 'John',
"last_name": "Johnson",
"phone_number": '123456789'
},
{
"first_name": None,
"last_name": 'Je'
}
]
assert_that(users_list_json_obj, matches_json_schema(all_users_info_schema))
AssertionError:
Expected:
JSON object should match schema "All Users Info"
but:
mismatches occurred:
'123456789' is not of type 'number'
Failed validating 'type' in schema['items']['properties']['phone_number']:
{'type': 'number'}
On instance[0]['phone_number']:
'123456789'
------------
'Je' is too short
Failed validating 'minLength' in schema['items']['properties']['last_name']:
{'minLength': 3, 'type': 'string'}
On instance[1]['last_name']:
'Je'
------------
None is not of type 'string'
Failed validating 'type' in schema['items']['properties']['first_name']:
{'minLength': 3, 'type': 'string'}
On instance[1]['first_name']:
None
------------
Dependencies:
jsonschema==3.2.0
PyHamcrest==2.0.2
Metadata
Release files for json-schema-matchers 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json-schema-matchers-0.1.3.tar.gz | 3.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_schema_matchers-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.4 kB
Release files / json-schema-matchers-0.1.3.tar.gz
| Download URL | json-schema-matchers-0.1.3.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64ab63ad3423d72d4f538f7c63028141dca8d8503b3b8dcb13749ecc31a179f2
|
|
BLAKE2b-256 checksum How to use checksums |
527b49cf70d0fca7ac298dac519f05822fe154511112cf37a5d2c517a8d4a2e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.15
|
Release files / json_schema_matchers-0.1.3-py3-none-any.whl
| Download URL | json_schema_matchers-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e447a1be0711e281ab87ac4e34591b7cd987ba82a225a884c00d4eab412a08b5
|
|
BLAKE2b-256 checksum How to use checksums |
8042fac346495b4eb8aeb55f7e3ab09b60245847edf8b131469bde994eab60a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.15
|