Skip to main content

Simple data validation library

Project description

https://travis-ci.org/DKorytkin/json_checker.svg?branch=master https://codecov.io/gh/DKorytkin/Checker/branch/master/graph/badge.svg https://img.shields.io/badge/python-2.7%2C%203.3%2C%203.4%2C%203.5%2C%203.6-blue.svg https://img.shields.io/badge/version-1.1.5-lightgrey.svg

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_data = {'first_key': int, 'second_key': str}


>>> checker = Checker(expected_data)
>>> 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" 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 is not str
current value 2 is not str
current value 3 is not str

>>> Checker([str]).validate((1, 2, 3))
Traceback (most recent call last):
...
checker_exceptions.ListCheckerError:
current value 1 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" 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>')

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 "122" is not int
    current value "122" is not None

If you need validate no required dict key, use OptionalKey

>>> from json_checker import Checker, OptionalKey

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

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

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_checker-1.1.5.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file json_checker-1.1.5.tar.gz.

File metadata

  • Download URL: json_checker-1.1.5.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for json_checker-1.1.5.tar.gz
Algorithm Hash digest
SHA256 c670ee165f6376e43865293c33c1e8a7c311cea735885e3f25545c994ad774e8
MD5 03aaa72060a3f51bdc87a205bf7dafd1
BLAKE2b-256 fcc08f7e182830c1d24ee4c8e2a583d0bd920fdffcbe7aff44e6566102231b10

See more details on using hashes here.

Supported by

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