Skip to main content

more.jsonschema: JSON Schema support for Morepath

This package provides Morepath integration for JSON Schema, implemented with jsonschema:

JSON Schema can automate user input validation in a HTTP API.

Schema

The JSON schema need to be load into a Python dict (you can use json.load()):

user_schema = {
    'type': 'object',
    'properties': {
        'name': {
            'type': 'string',
            'minLength': 3
        },
        'age': {
            'type': 'integer',
            'minimum': 10
        }
    },
    'required': ['name', 'age']
}

If you want to define JSON schemas in Python, you can use jsl for instance.

Validate

The more.jsonschema integration helps with validation of the request body as it is POSTed to a view. First we must create a loader for our schema:

from more.jsonschema import loader

user_schema_load = loader(user_schema)

We can use this loader to handle a POST request for instance:

@App.json(model=User, request_method='POST', load=user_schema_load)
def user_post(self, request, json):
    # json is now a validated and normalized dict of whatever got
    # POST onto this view that you can use to update
    # self

Customize the Validator

By default more.jsonschema uses the Draft7Validator. But you can also use Draft3Validator, Draft4Validator or Draft6Validator. You can even create your own Validator or extend an existent one. Just pass the Validator to the loader:

from jsonschema import Draft4Validator
from more.jsonschema import loader

  user_schema_load = loader(user_schema, validator=Draft4Validator)

More information about creating or extending Validator classes you can find in the jsonschema documentation.

Error handling

If validation fails due to a validation error (a required field is missing, or a field is of the wrong datatype, for instance), you want to show some kind of error message. The load function created by more.jsonschema raise the more.jsonschema.ValidationError exception in case of errors.

This exception object has an errors attribute with the validation errors. You must define an exception view for it, otherwise validation errors are returned as “500 internal server error” to API users.

This package provides a default exception view implementation. If you subclass your application from more.jsonschema.JsonSchemaApp then you get a default error view for ValidationError that has a 422 status code with an error message:

from more.jsonschema import JsonSchemaApp

class App(JsonSchemaApp):
    pass

Now your app has reasonable error handling built-in.

CHANGES

0.2 (2020-04-26)

  • Removed: Removed support for Python 2 and Python 3.4 and 3.5.

    You have to upgrade to Python 3 if you want to use this version.

  • Added support for Python 3.6, 3.7 and 3.8 and PyPy 3.6.

  • Make Python 3.7 the default testing environment.

  • Upgrade jsonschema to version 3.2.0.

  • Make JSON Schema Draft 7 the default validator.

  • Add integration for the Black code formatter.

0.1 (2017-03-17)

  • initial public release.

Release files for more.jsonschema 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for more.jsonschema 0.2
File Size Uploaded
more.jsonschema-0.2.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for more.jsonschema 0.2
File Interpreter ABI Platform
more.jsonschema-0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 12.9 kB

Release files / more.jsonschema-0.2.tar.gz

Download URL more.jsonschema-0.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d2e2b23fa78940f8897947847930adc6bb2a76e905a905529397184e582c5657
BLAKE2b-256 checksum
How to use checksums
1606a59832bbabe632916f6868c3e93a441708d27025b8be044cbecbf485abcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Release files / more.jsonschema-0.2-py2.py3-none-any.whl

Download URL more.jsonschema-0.2-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
560b7e35e1e5982a097ca254ec06ee595c1fdbfe110a60092a10f2475ed3ae67
BLAKE2b-256 checksum
How to use checksums
167cf850583599a01ebe2f0fc6c98fc4a4c0167c65ef156ca0279962905fcece
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page