Json Payload Validator
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
pip install json_payload_validator
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
Required property example
from json_payload_validator import validate
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
},
'required': [
'name'
]
}
payload = {}
error = validate(payload, schema)
print(error) # 'name' is a required property
Validation rule 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'
]
}
payload = {'name': 555}
error = validate(payload, schema)
print(error) # Validation of property 'name' failed: Name must be a string
Metadata
Release files for json_payload_validator 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json_payload_validator-0.1.2.tar.gz | 3.4 kB | Details |
Release files / json_payload_validator-0.1.2.tar.gz
| Download URL | json_payload_validator-0.1.2.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
adac378f99bed1532392951dc4ef73610f1d022a049fe8b24da2f9384f48808d
|
|
BLAKE2b-256 checksum How to use checksums |
9fc287e168fc64bcb392a308a063e33999ddc62dd813ebafdddf319ee61ccc45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |