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

------------


Process finished with exit code 1

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.3.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: json-schema-matchers-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 35423ff9eeb382218f6159039f165aab48763f021b7d835aae20ce554d9dc740
MD5 c9eb41f7e5e0820e53799bb43b9c2512
BLAKE2b-256 420e7726363485ac147da39dadeb27286490c3a7e3ce1ba998448e6200ff486a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_schema_matchers-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d8fb6e00be054bdcc144019cb265c6a41f650da29825f26581fc769da865aceb
MD5 78fd421cc6620caa37cfb4211100836e
BLAKE2b-256 026767f1ff6939a916f52fe9f133db780b0a907e29ae0d9325d91d94850aa6fb

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