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/dm/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

Top-Level Classes

  • jsonModel: a schema-enforced class for json data validation

Features

  • Validates native json datatypes

  • Validates byte data as base64 encoded strings

  • Alternative to json schema module

  • Schema declaration is self-valid

  • Built-in validation of model declaration

  • Flat structure to object property attribute declarations

  • Validates size of data object inputs #TODO

  • Accommodates function extension for object property validation #TODO

Installation

>From PyPi:

$ pip install jsonmodel

>From GitHub:

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

Getting Started

This module is designed to facilitate the process of implementing data validation against a declared json data model. jsonModel 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. For many data models, full validation can be achieved from an example declaration:

"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:

"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",
    "url": "https://collectiveacuity.com/api/mycoolresource?jsonmodel=true",
    "max_size": 1024,
}

[all fields except schema are optional]

To import the model:

import json

sampleModel = json.loads(open('sample-model.json').read())

To initialize the class object:

from jsonmodel.validators import jsonModel

validModel = jsonModel(sampleModel)

To validate input against model declaration:

validModel.validate(input)

To handle invalid inputs:

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

To validate input against specific component declaration:

path_to_root = '.property'
validModel.component(input, path_to_root)

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 Distributions

jsonmodel-1.2.zip (22.6 kB view details)

Uploaded Source

jsonmodel-1.2.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

jsonmodel-1.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonmodel-1.2.zip.

File metadata

  • Download URL: jsonmodel-1.2.zip
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsonmodel-1.2.zip
Algorithm Hash digest
SHA256 1d74b1bb047cbeb1aeaa75c181e8970714991938caf1c37bf18d78b2df7977c5
MD5 0496d6155a7c021047af539e850c0dc8
BLAKE2b-256 bf2abc92312a6feb97207b52ffcaa2d3988897bd8c1f96b2374994f1b44b4cb6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonmodel-1.2.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsonmodel-1.2.tar.gz
Algorithm Hash digest
SHA256 1c7d2aa2c19d1c22311d39e888f044511def3a11cb27a23ec9556f96cb6bc5c0
MD5 78cb81ca31fce8c34ffe48b340d2332d
BLAKE2b-256 d2b62d78ca67858533ab4614a01cfb167614f2f2207043072bd87165e334db89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonmodel-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7d96bb06d1cc38f9314414ceea87d3b0b2409855bf18fc660297becc74d23948
MD5 41b4d67766c9f719f983a45d627a2c15
BLAKE2b-256 c71509061c383bbf52be2745ef6a465acf0e19c2490c945f55b24941b449fb50

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