Skip to main content

A Collection of Methods for Validating JSON Structured Data

Project description

https://img.shields.io/pypi/v/jsonmodel.svg https://img.shields.io/pypi/l/jsonmodel.svg

jsonModel

A Collection of Methods for Validating JSON Structured Data

Downloads:

http://pypi.python.org/pypi/jsonModel

Source:

https://github.com/collectiveacuity/jsonModel

Documentation:

https://collectiveacuity.github.io/jsonModel/

Introduction

Json Model is designed to facilitate the process of implementing data validation against a declared json data model. The jsonModel class offers a more intuitive declaration process than other schema enforcement modules currently available by relying upon the architecture of json itself to validate datatypes, requirements and defaults.

Installation

From PyPi:

$ pip install jsonmodel

From GitHub:

$ git clone https://github.com/collectiveacuity/jsonmodel
$ cd jsonmodel
$ python setup.py install

Getting Started

This module uses self-valid schema declarations as a method to describe data requirements. As a result, for many data models, full validation can be achieved from an example declaration using the schema key:

{
  "schema": {
    "userID": "gY3Cv81QwL0Fs",
    "datetime": 1456000345.543713,
    "active": true,
    "address": {
      "city": "New Orleans",
      "region": "LA",
      "postal_code": "",
      "country": "United States"
    }
  }
}

[In this model, the input must contain values for all four top level keys and each value must correspond to the datatype in the model. So, the input must have a userID field with a string, a datetime field with a number, an active key with a boolean and the address field must be a dictionary which itself contains city, region and country values. Since it is empty, postal_code is optional. If a value is provided for postal_code however, it must be a string.]

In addition to intuitive self-valid schema declarations, jsonModel also offers a rich way to further refine the conditionality of any property in the model through an accompanying components map whose key names correspond to the path to the schema property which requires additional validation:

{
  "schema": { ... },
  "components": {
    "userID": {
      "min_length": 13,
      "max_length": 13,
      "must_not_contain": [ "[^\\w]", "_" ]
    },
    "address.city": {
      "discrete_values": [ "New Orleans", "New York", "Los Angeles", "Miami" ],
      "required_field": false
    }
  }
}

[In this model, the process of checking the inputs will also check the paths designated in the components dictionary to make sure that values do not violate any of the additional attributes of the property declared in the components. Whenever they may conflict with the attributes declared in the schema example, the conditions in the components map supersedes. So, in this case, the requirement that an address contain a city key-value has been turned off. But if a city is provided, it must match one of the four city values provided. Likewise, any value provided in userID must be no more than nor less than 13 characters long and can only be composed of alphanumerical characters.]

This module also validates the architecture of the model declarations themselves to facilitate the model design process and ensure that no models break the rules of the module. Error reports are provided to identity the scope of conditionals applicable to any given property in addition to the module documentation.

To declare the model:

{
    "schema": {
        "property": "value"
    },
    "components": {},
    "title": "my cool data model",
    "description": "model for performance analytics records of my sweet app",
    "metadata": { "version": "1.1.1" },
    "url": "https://collectiveacuity.com/api/mycoolresource?jsonmodel=true",
    "max_size": 1024,
}

[all fields except schema are optional]

To import the schema:

import json

sample_schema = json.loads(open('sample-schema.json').read())

To initialize the class object:

from jsonmodel.validators import jsonModel

valid_model = jsonModel(sample_schema)

To validate input against model declaration:

valid_model.validate(input)

To validate input against an individual component:

path_to_root = 'dot.path[2].field'
valid_model.validate(input, path_to_root)

To handle invalid inputs:

try:
    valid_model.validate(invalid_input)
except InputValidationError as err:
    assert err.error['error_code'] > 4000

To generate a colloquial explanation of error:

try:
    valid_model.validate(invalid_input)
except InputValidationError as err:
     print(err.explain())

To customize error message:

input_title = 'Property field in input'
valid_model.validate(input, path_to_root, input_title)

To filter valid input based upon query criteria:

query_criteria = { 'dot.path[2].field': 'exact value' }
assert valid_model.query(query_criteria, valid_input)

query_criteria = { 'dot.path[2].field': { 'excluded_values': [ 'exact value' ] } }
assert not valid_model.query(query_criteria, valid_input)

To produce html documentation of model criteria:

from jsonmodel.extensions import tabulate
tabulate(valid_model)
html_table = valid_model.tabulate()

Further Reading

For more details about how to use jsonModel, refer to the Reference Documentation on GitHub

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

jsonmodel-3.3.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

jsonmodel-3.3-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file jsonmodel-3.3.tar.gz.

File metadata

  • Download URL: jsonmodel-3.3.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for jsonmodel-3.3.tar.gz
Algorithm Hash digest
SHA256 f3096f5c89e27fcbb378053a45c0c438a1db872f410230e70d4ce8a8dd592a8e
MD5 d505d998949774d5f2099ccb1cd2e57a
BLAKE2b-256 23ebd56251f2d30a2796eb26a9a0be8d1e16941e37e76e091583ca82e59098bf

See more details on using hashes here.

File details

Details for the file jsonmodel-3.3-py3-none-any.whl.

File metadata

  • Download URL: jsonmodel-3.3-py3-none-any.whl
  • Upload date:
  • Size: 22.9 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/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for jsonmodel-3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1d6706d8a15d1fdbef9debedaae1a2b4672ec0829e77fd0b2a34034d72f57154
MD5 2e25b9d715e6c3be339601a6356dbafb
BLAKE2b-256 d9e49110ffdb144c911909a1b98a524ae41c1ef53f42b5b2fe92013a26bf9444

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