A Collection of Methods for Validating JSON Structured Data
Project description
jsonModel
A Collection of Methods for Validating JSON Structured Data
- Downloads:
- Source:
- Documentation:
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 = '.property' 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 customize error message:
input_title = ‘Property field in input’ valid_model.validate(input, path_to_root, input_title)
Further Reading
For more details about how to use jsonModel, refer to the Reference Documentation on GitHub
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jsonmodel-2.8.tar.gz
.
File metadata
- Download URL: jsonmodel-2.8.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 228dee4fc859e616841d21eb42db04e936789aa15e7bc17d88cbf3ebfff715b8 |
|
MD5 | 03b102acf12594dc99701d1a860d1464 |
|
BLAKE2b-256 | 7556c022e16821fbbd8f992ed06ae0a0dc2f9dfebfdad64a7be48b0d9c46fb1e |
File details
Details for the file jsonmodel-2.8-py3-none-any.whl
.
File metadata
- Download URL: jsonmodel-2.8-py3-none-any.whl
- Upload date:
- Size: 26.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e33c1f37ac1564950583d9876552cedc506c6df0c0805fdd8836698fb905d7dc |
|
MD5 | bfa24e81a4fd1a090699bc6e821e557d |
|
BLAKE2b-256 | 20229b6e9937f19e7d0e1126c5841e1f860faecffeccd49863a7ff6ad092d667 |