Skip to main content

JSON validator package based on jsonschema that returns nicer validation errors for end users

Project description

CircleCI PyPI version shields.io PyPI license PyPI pyversions

Little Python package that formats validation error messages from jsonschema. You should use this package if you want a stand alone json validator decoupled from any framework.

Reason of being

jsonschema is really cool but its validation error messages suck as they’re meant for developers, not end users. So if you have an API that uses jsonschema to validate json payloads and want to return nicer error messages to your end users you can use this package :)

How it works

3 simple rules:

  • If you don’t send a required property in the payload you’ll get the message All required properties must be present: foo, bar, baz.

  • If validation fails the validation rule will be returned Validation of property 'name' failed: {'minLength': 2, 'type': 'string', 'maxLength': 50}.

  • If you add message property in your validation rule its value will be returned instead of the rule Validation of property 'foo' failed: Custom message will go in here.

Usage

To use, simply do:

from json_payload_validator import validate

schema = {
    'type': 'object',
    'properties': {
        'name': {'type': 'string', 'minLength': 2, 'maxLength': 50},
    },
    'required': [
        'name'
    ]
}

# example of validation
payload = {'name': 'John Maus'}

error = validate(payload, schema)

if error is None:
    print('Validation passed :-)')
else:
    print(error)
    # Validation of property 'name' failed: {'minLength': 2, 'type': 'string', 'maxLength': 50}

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

json_payload_validator-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file json_payload_validator-0.0.2.tar.gz.

File metadata

File hashes

Hashes for json_payload_validator-0.0.2.tar.gz
Algorithm Hash digest
SHA256 deaaf800243e3ee7bdc702f9fe43f06ed5383df22196fc9196386c2519b96b0b
MD5 4a90a8f3bb067485d6dd6ab27b5ee047
BLAKE2b-256 dc6823ef7b63bc0bd7ce15c048d4241f0cbaf2a52b227c3ba1187d65c98b4c2b

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