Skip to main content

sanic-validation

Build Status

Validation for sanic endpoints.

Installation

pip install sanic-validation

Usage example

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')

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

Uploaded Source

File details

Details for the file sanic-validation-0.1.4.tar.gz.

File metadata

File hashes

Hashes for sanic-validation-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a5e12ef7393f9800a152f7a6a91b7aee541d2009ed6a522f7e00a18dfd2a8318
MD5 0490ed8dd932bbc4bc508997a39ae18a
BLAKE2b-256 eddc9d8a68e9a5727c362d7af3bb265fabd7dd53217ccd989c2214bc382cf901

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page