Skip to main content

JSON Data Definition Format support for Python

Project description

jddf-python PyPI version

Documentation on readthedocs.io: https://jddf.readthedocs.io

This package is a Python implementation of JSON Data Definition Format, a schema language for JSON.

Installation

Install this package using:

pip3 install jddf

Usage

Briefly, here's how you would use this package:

import Schema, Validator from jddf
import json

# jddf.Schema represents a JDDF Schema.
#
# You can parse a jddf.Schema from JSON using Schema.from_json in combination
# with the standard library's json module.
schema = Schema.from_json(json.loads("""
  {
    "properties": {
      "name": { "type": "string" },
      "age": { "type": "uint32" },
      "phones": {
        "elements": { "type": "string" }
      }
    }
  }
"""))

# jddf.Validator validates inputted data against schemas, and returns a list of
# validation errors.
#
# The exact data in validation errors is part of the JDDF specification, so
# every implementation of JDDF, across all languages, return the same sort of
# validation errors.
validator = Validator()

# This input is ok -- it passes all of the rules of the schema. So no validation
# errors are returned.
errors_ok = validator.validate(schema, json.loads("""
  {
    "name": "John Doe",
    "age": 43,
    "phones": ["+44 1234567", "+44 2345678"]
  }
"""))

print(errors_ok) # []

# This input has three problems, so three validation errors are returned.
errors_bad = validator.validate(schema, json.loads("""
  {
    "age": "43",
    "phones": ["+44 1234567", 442345678]
  }
"""))

print(len(errors_bad)) # 3

# "name" is required
#
# [{'instance_path': [], 'schema_path': ['properties', 'name']}]
print(errors_bad[0])

# "age" has wrong type
#
# [{'instance_path': ['age'], 'schema_path': ['properties', 'age', 'type']}]
print(errors_bad[1])

# "phones[1]" has wrong type
#
# [{'instance_path': ['phones', '1'], 'schema_path': ['properties', 'phones', 'elements', 'type']}]
print(errors_bad[2])

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

jddf-0.1.2.tar.gz (8.3 kB view details)

Uploaded Source

File details

Details for the file jddf-0.1.2.tar.gz.

File metadata

  • Download URL: jddf-0.1.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for jddf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8a87d700f5c2350b2a27019e0d220b0fae26fa6dff5fce624c393ec6f3bfc457
MD5 7376bbca6190d2c24c04942c3ee4c774
BLAKE2b-256 2240c9f8c06246310a69be05fd55e068bd53d5f9900e7e739f62c2adff50b6d1

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