Custom hamcrest matchers for json schema validation
Project description
json-schema-matchers
Custom matchers 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"
},
"last name": {
"type": "string"
},
"phone number": {
"type": "number"
}
},
"required": [
"first",
"last",
]
}
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 *
from 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": 'Jefferson'
}
]
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'
------------
None is not of type 'string'
Failed validating 'type' in schema['items']['properties']['first_name']:
{'type': 'string'}
On instance[1]['first_name']:
None
------------
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
File details
Details for the file json-schema-matchers-0.0.4.tar.gz
.
File metadata
- Download URL: json-schema-matchers-0.0.4.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41d10b4aed04415ed7ae99e519ba24afe938976a612eee2e593c654333508af7 |
|
MD5 | 2e65d90b7f4585bf62d57b9461c8172e |
|
BLAKE2b-256 | a1f5638e384a588a3839e8a64695202d13450f42683f5b9be25ac970b5289569 |
File details
Details for the file json_schema_matchers-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: json_schema_matchers-0.0.4-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeebe3ec462d02cec3f79f6fc0344108cb98b72905c74afaf2d545a7821c324c |
|
MD5 | a431e87864aaeb9c7ffaf9bed7c41fad |
|
BLAKE2b-256 | 8d177cd88c193452021d03096a11ff27d8a689663dbe00fd5be01fc528cb68ea |