Skip to main content

json_checker

Build Cov Python versions PyPI PyPI - Downloads

json_checker is a library for validating Python data structures, such as those obtained from JSON (or something else) to Python data-types. json_checker has a parameter (soft=True) that allows you validate all json and raise all errors after validation done, it`s very profitable from API testing:

>>> import requests
>>>
>>> from json_checker import Checker
>>>
>>>
>>> def test_api():
>>>     res = requests.get(API_URL).json()
>>>     assert Checker(EXPECTED_RESPONSE, soft=True).validate(res) == res

Installation

$ pip install json_checker

Example

Here is a quick example to get a feeling of json_checker, validating a list of entries with personal information:

>>> from json_checker import Checker

>>> current_data = {'first_key': 1, 'second_key': '2'}
>>> expected_schema = {'first_key': int, 'second_key': str}


>>> checker = Checker(expected_schema)
>>> result = checker.validate(current_data)


>>> assert result == current_data

If data is valid, Checker.validate will return the validated data

If data is invalid, Checker will raise CheckerError.

How json_checker validates data

Types

If Checker(...) encounters a type (such as int, str), it will check if the corresponding piece of data is an instance of that type, otherwise it will raise CheckerError.

>>> from json_checker import Checker

>>> Checker(int).validate(123)
123

>>> Checker(int).validate('123')
Traceback (most recent call last):
...
checker_exceptions.TypeCheckerError:
current value '123' (str) is not int

Lists, similar containers

If Checker(...) encounters an instance of list, tuple, set or frozenset, it will validate contents of corresponding data container against schemas listed inside that container: if param soft is True validate all data, and if have not valid data raise exception after validation

>>> Checker([int]).validate([1, 1, 0, 1])
[1, 1, 0, 1]

>>> Checker([str], soft=True).validate((1, 2, 3))
Traceback (most recent call last):
...
checker_exceptions.CheckerError:
ListCheckerErrors:
current value 1 (int) is not str
current value 2 (int) is not str
current value 3 (int) is not str

>>> Checker([str]).validate((1, 2, 3))
Traceback (most recent call last):
...
checker_exceptions.ListCheckerError:
current value 1 (int) is not str

Dictionaries

If Checker(...) encounters an instance of dict, it will validate data key-value pairs:

>>> current_dict = {'first_key': 1, 'second_key': '2'}
>>> checker = Checker({'first_key': int, 'second_key': int})
>>> checker.validate(current_dict)

Traceback (most recent call last):
...
checker_exceptions.DictCheckerError:
From key="second_key"
    current value '2' (str) is not int

Operators Or, And, OptionalKey

If you needed validate data from some conditions, use And operator for example current data must be int instance and greater than 0 and less 99 try it:

>>> from json_checker import Checker, And

>>> checker = Checker(And(int, lambda x: 0 < x < 99))
>>> checker.validate(12)
12

>>> checker.validate(100)
Traceback (most recent call last):
...
checker_exceptions.CheckerError:
Not valid data And(int, <lambda>),
    function error

If you need validation not required data value, use Or operator for example current data must be int or None try it:

>>> from json_checker import Checker, Or

>>> checker = Checker(Or(int, None))
>>> checker.validate(122)
122

>>> checker.validate('666')
Traceback (most recent call last):
...
checker_exceptions.CheckerError:
Not valid data Or('int', None),
    current value '666' (str) is not int, current value '666' (str) is not None

If you need validate no required dict key, use OptionalKey

>>> from json_checker import Checker, OptionalKey

>>> expected_schema = {'key1': str, OptionalKey('key2'): int}
>>> Checker(expected_schema).validate({'key1': 'value'})
{'key1': 'value'}

>>> Checker(expected_schema).validate({'key1': 'value', 'key2': 'value2'})
Traceback (most recent call last):
...
checker_exceptions.DictCheckerError:
From key="OptionalKey(key2)"
    current value 'value2' (str) is not int

More logs for debug

>>> import logging
>>> from json_checker import Checker

>>> logging.basicConfig(level=logging.DEBUG)

>>> Checker({'k': str}, soft=True).validate({'k': 1})
DEBUG:json_checker.app:Checker settings: ignore_extra_keys=False, soft=True
DEBUG:json_checker.app:DictChecker({'k': <class 'str'>} (dict)) start with: {'k': 1}
DEBUG:json_checker.app:TypeChecker(str) start with: 1
DEBUG:json_checker.app:TypeChecker(str) error current value 1 (int) is not str
DEBUG:json_checker.app:DictChecker({'k': <class 'str'>} (dict)) error From key="k": current value 1 (int) is not str
Traceback (most recent call last):
...
CheckerError:
From key="k": current value 1 (int) is not str

Metadata

Release files for json-checker 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-checker 2.0.0
File Size Uploaded Provenance Actions
json_checker-2.0.0.tar.gz 14.9 kB Without provenance Details for json_checker-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-checker 2.0.0
File Interpreter ABI Platform Actions
json_checker-2.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for json_checker-2.0.0-py2.py3-none-any.whl

Total release size: 34.0 kB

Release files / json_checker-2.0.0.tar.gz

Download URL json_checker-2.0.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ef32754928b178c74b1f93fb2dbbe7f22f1ce9bc781ea68c28843d9f04d4a8e1
BLAKE2b-256 checksum
How to use checksums
9f22d742329cf03f9edcb58dc673f39608d0bf57046e99770d2c45ea88709986
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

Release files / json_checker-2.0.0-py2.py3-none-any.whl

Download URL json_checker-2.0.0-py2.py3-none-any.whl
Size 19.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2398f761549709198f8cbd778a731c78cc0ab70e3299e1b8898f8bc8c7dd2b57
BLAKE2b-256 checksum
How to use checksums
fd3c2f5cb8880a05aa6a2456a6a55f2694007cde08baed3732b34a070132462e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

Security for json-checker 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page