JSON validator package based on jsonschema that returns nicer validation errors for end users
Project description
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 '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 error message.
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
File details
Details for the file json_payload_validator-0.1.0.tar.gz
.
File metadata
- Download URL: json_payload_validator-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c37641eacfe4646bc954691b27e2a9165d0bfc4bd37cf1173b2efa2891322cf |
|
MD5 | 85bb2f87ca63f9ff4a7b470cc63e6478 |
|
BLAKE2b-256 | e34970d4432be8de54adbf23359bd6957351b68326c78852c4b4b0d71449e26f |