sanic-validation
Validation for sanic endpoints.
Installation
pip install sanic-validation
Usage example
Body JSON validation
from sanic import Sanic
from sanic.response import json
from sanic_validation import validate_json
app = Sanic()
schema = {'name': {'type': 'string', 'required': True}}
@app.route('/')
@validate_json(schema)
async def _simple_endpoint(request):
return json({'message': 'Hello ' + request.json['name']})
app.run('0.0.0.0')
Query parameters validation
from sanic import Sanic
from sanic.response import json
from sanic_validation import validate_args
app = Sanic()
schema = {'name': {'type': 'string', 'required': True}}
@app.route('/')
@validate_args(schema)
async def _simple_endpoint(request):
return json({'message': 'Hello ' + request.raw_args['name']})
app.run('0.0.0.0')
Schema format
Internally, sanic-validation uses cerberus as a validation provider. Detailed format documentation can be found here.
Example validation success
Request
GET / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 16
Content-Type: application/json
Host: localhost:8000
User-Agent: HTTPie/0.9.9
{
"name": "John"
}
Response
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Keep-Alive: 5
{
"message": "Hello John"
}
Example validation failure
Request
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8000
User-Agent: HTTPie/0.9.9
Response
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 168
Content-Type: application/json
Keep-Alive: 5
{
"error": {
"invalid": [
{
"constraint": true,
"entry": "name",
"entry_type": "json_data_property",
"rule": "required"
}
],
"message": "Validation failed.",
"type": "validation_failed"
}
}
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic-validation-0.2.0.tar.gz
(16.5 kB
view details)
File details
Details for the file sanic-validation-0.2.0.tar.gz.
File metadata
- Download URL: sanic-validation-0.2.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe14b3498c68143d5d6c4b62a99044c3cb8090aa91066b2bce69709c89d99c26
|
|
| MD5 |
54fb4eb1714e2ee2861d65ce2158b817
|
|
| BLAKE2b-256 |
93e97bc2dfc0b4e03a192b2c3a6089c8db96a4c94f6d7629ecb20c7ecc8ede1e
|