Validation library for sanic

I’ve recently discovered sanic - a fast, async web server framework.
I couldn’t find a good validation library for it, so I wrote my own.
Under the hood it uses cerberus to validate data against the supplied schema.
Generated error messages provide full path to the offending parameter, and the constraint that was broken.

Installation

1
pip install sanic-validation

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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')

Example error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"error": {
"invalid": [
{
"constraint": true,
"entry": "name",
"entry_type": "json_data_property",
"rule": "required"
}
],
"message": "Validation failed.",
"type": "validation_failed"
}
}