Skip to main content

Human-friendly schema definition language and schema validator

Project description

PyPI - Status PyPI GitHub PyPI - Python Version

Schema Markdown is a human-friendly schema definition language and schema validator. Here are its features at a glance:

  • Schema-validate JSON objects

  • Human-friendly schema definition

  • Validates member value and length contraints

  • Validation type-massages string member values

  • Pure Python

Usage

To schema-validate an object, first parse its Schema Markdown using the SchemaMarkdownParser class:

>>> import schema_markdown
...
>>> parser = schema_markdown.SchemaMarkdownParser('''\
... # An aggregation function
... enum Aggregation
...     Average
...     Sum
...
... # An aggregate numerical operation
... struct Operation
...     # The aggregation function - default is "Sum"
...     optional Aggregation aggregation
...
...     # The numbers to operate on
...     int[len > 0] numbers
... ''')

Then, validate a dictionary object (or other object) using the validate_type function:

>>> schema_markdown.validate_type(parser.types, 'Operation', {
...     'numbers': [1, 2, '3', 4]
... })
{'numbers': [1, 2, 3, 4]}

Notice that the numerical input ‘3’ above is type-massaged to the integer 3 by validation. Validation fails if the object does not match the schema:

>>> try:
...     schema_markdown.validate_type(parser.types, 'Operation', {
...         'numbers': [1, 2, 'asdf', 4]
...     })
... except schema_markdown.ValidationError as exc:
...     str(exc)
"Invalid value 'asdf' (type 'str') for member 'numbers.2', expected type 'int'"

Validation also fails if a member contraint is violated:

>>> try:
...     schema_markdown.validate_type(parser.types, 'Operation', {
...         'numbers': []
...     })
... except schema_markdown.ValidationError as exc:
...     str(exc)
"Invalid value [] (type 'list') for member 'numbers', expected type 'array' [len > 0]"

Development

This project is developed using Python Build. Refer to the Python Build documentation for development instructions.

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

schema-markdown-0.9.11.tar.gz (17.4 kB view hashes)

Uploaded Source

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