Skip to main content

PyHamcrest matchers extension for json schema validation

Project description

json-schema-matchers

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

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

Uploaded Source

Built Distribution

json_schema_matchers-0.1.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json-schema-matchers-0.1.2.tar.gz
  • Upload date:
  • Size: 3.0 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.1.2.tar.gz
Algorithm Hash digest
SHA256 7ef46f0ccaba3aae5a8fbaf5b76c6f661204c735fc441bb2d09abeca944e91ff
MD5 66a725e9a22670688a5cfe84cb843847
BLAKE2b-256 3a32b5bbbe4480bb1e4c2cc122e691b9fc54b181e579b9f299321c4928abf7d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_schema_matchers-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d98570fb3329f5565d0b18df199c06a162b1e93d00eac538d938a8abc520384d
MD5 06122678590451543daae5aad8db2077
BLAKE2b-256 81abfef766309e42b3eef1cd995a02029118756938275a0da393895877d5a74c

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