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 'foo' is a required property.

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

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

Usage

Successful validation example

from json_payload_validator import validate

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

payload = {'name': 'John Maus'}

error = validate(payload, schema)
print(error) # None

Validation failure example

from json_payload_validator import validate

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

payload = {'name': 555}

error = validate(payload, schema)
print(error) # Validation of property 'name' failed: {'minLength': 2, 'type': 'string', 'maxLength': 50}

Custom error message example

from json_payload_validator import validate

schema = {
    'type': 'object',
    'properties': {
        'name': {'type': 'string', 'message': 'Name must be a string'},
    },
    'required': [
        'name'
    ]
}

# example of validation
payload = {'name': 555}

error = validate(payload, schema)
print(error) # Validation of property 'name' failed: Name must be a string

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.1.1.tar.gz (3.4 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for json_payload_validator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 782b48ebd052afea7a42badb22cc1322c612f50a2c0ae4c711b8c1ddc95ceb6f
MD5 0ab1c9b36af18a0c36a458600504420b
BLAKE2b-256 a7abb95914a0453ca7c8c4a8865317e31628768117417a6d35474e4ff17be438

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