Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-schema-matchers-0.0.6.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

json_schema_matchers-0.0.6-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file json-schema-matchers-0.0.6.tar.gz.

File metadata

  • Download URL: json-schema-matchers-0.0.6.tar.gz
  • Upload date:
  • Size: 2.8 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

Hashes for json-schema-matchers-0.0.6.tar.gz
Algorithm Hash digest
SHA256 edf093c505b37127ecaeadcc235711574e898912c2418224c4fb01a3b3fc86f7
MD5 5d5e6e6a9f9feb0fd8ec607e3944266f
BLAKE2b-256 bef1458afedb963466bdf41b60ce5386ae8757cea6149b7b1e181f1db70f31f0

See more details on using hashes here.

File details

Details for the file json_schema_matchers-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: json_schema_matchers-0.0.6-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

Hashes for json_schema_matchers-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0ce01d0eb8563766811e8d3a7312729fb4ba5d074c175bb6bc8dbcc26123218f
MD5 0dd48d0958f58adb6755944ac67a759d
BLAKE2b-256 47d0f26e0859f50be5aaaf0d94647956fc71af384c3a8b85ef921038a5bc2dfe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page