Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ludusrusso/flask_voluptuous

A simple flask extension for data validation with Voluptuous
https://github.com/ludusrusso/flask_voluptuous

Last synced: 20 days ago
JSON representation

A simple flask extension for data validation with Voluptuous

Awesome Lists containing this project

README

        

# flask_voluptuous
A simple flask extension for data validation with Voluptuous

## Usage

```python
from flask import Flask
from flask_voluptuous import expect, Schema, Required

app = Flask(__name__)

@app.route('/')
@expect(Schema({
Required('name'): str
}), 'args')
def index(args):
return 'name: {}'.format(args['name'])

if __name__ == '__main__':
app.run(debug=True)
```

The `/` route requires a `name` param in the request arguments.
This data are accesible throw the `args` arguments passed to the function.

If the data is not submitted, we get an 406 error:
```
$ http http://127.0.0.1:5000/?other=ludusrusso
HTTP/1.0 406 NOT ACCEPTABLE
Content-Length: 160
Content-Type: text/html
Date: Mon, 02 Oct 2017 10:44:33 GMT
Server: Werkzeug/0.12.2 Python/3.6.1

406 Not Acceptable

Not Acceptable


extra keys not allowed @ data['other']


```

```
$ http http://127.0.0.1:5000/?name=ludusrusso
HTTP/1.0 200 OK
Content-Length: 16
Content-Type: text/html; charset=utf-8
Date: Mon, 02 Oct 2017 10:42:18 GMT
Server: Werkzeug/0.12.2 Python/3.6.1

name: ludusrusso
```