๐Ÿš” A Pythonic way for validate requested JSON payload of Flask ๐Ÿš”
Branch: master
Clone or download
Fetching latest commitโ€ฆ
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
flask_validation
.gitignore
.scrutinizer.yml
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
requirements.txt
setup.py
test.py

README.md

Flask-Validation Build Status Documentation Status Coverage Status

$ pip install flask-validation

Pythonic JSON payload validator for requested JSON payload of Flask

Flask๋ฅผ ์œ„ํ•œ view decorator ๊ธฐ๋ฐ˜์˜ JSON ์š”์ฒญ ๋ฐ์ดํ„ฐ validation ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Example Usages

json_required

from flask import Flask
from flask_validation import json_required, Validator

app = Flask(__name__)
Validator(app)


@json_required
@app.route('/', methods=('POST'))
def index():
    return 'hello!'
    
app.run()
$ curl -d '' -v http://localhost:5000
...
> POST / HTTP/1.1
> Content-Type: application/x-www-form-urlencoded
< HTTP/1.0 400 BAD REQUEST
$
$ curl -H "Content-Type: application/json" -d '' -v http://localhost:5000
...
> POST / HTTP/1.1
> Content-Type: application/x-www-form-urlencoded
< HTTP/1.0 200 OK

validate_keys

from flask import Flask
from flask_validation import validate_keys, Validator

app = Flask(__name__)
Validator(app)


@validate_keys(['name', 'age', { 'position': ['lati', 'longi'] }])
@app.route('/', methods=('POST'))
def index():
    return 'hello!'
    
app.run()
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB"}' -v http://localhost:5000
...
> POST / HTTP/1.1
> Content-Type: application/x-www-form-urlencoded
< HTTP/1.0 400 BAD REQUEST
$
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": 19, "position": {"lati": 35.24, "longi": 127.681146}}' -v http://localhost:5000
...
> POST / HTTP/1.1
> Content-Type: application/json
< HTTP/1.0 200 OK

others

from flask import Flask
from flask_validation import validate_common, validate_with_fields, validate_with_jsonschema, Validator

app = Flask(__name__)
Validator(app)


@validate_common({ 'name': str, 'age': int, { 'position': {'lati': float, 'longi': float} } })
@app.route('/1', methods=('POST'))
def validate_1():
    return 'hello!'


@validate_with_fields({
    'name': StringField(allow_empty=False, regex='[A-z ]+'),
    'age': IntField(min_value=0),
    'gender': StringField(required=False, enum=['M', 'F']),
    {
        'position': {
            'lati': NumberField(min_value=0),
            'longi': NumberField(min_value=0)
        }
    }
})
@app.route('/2', methods=('POST'))
def validate_2():
    return 'hello!'


@validate_with_jsonschema({
    'type': 'object',
    'properties': {
        'name': {'type': 'string'},
        'age': {'type': 'integer'},
        'position': {
            'type': 'object',
            'properties': {
                'lati': {'type': 'number'},
                'longi': {'type': 'number'}
            }
        }
    }
})
@app.route('/3', mehtods=('POST'))
def validate_3():
    return 'hello!'


app.run()
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": 19, "position": {"lati": 35.24, "longi": 127.681146}}' -v http://localhost:5000/1
< HTTP/1.0 200 OK
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": 19, "position": {"lati": 35.24, "longi": 127}}' -v http://localhost:5000/1
< HTTP/1.0 400 BAD REQUEST
$
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": 19, "position": {"lati": 35, "longi": 127}}' -v http://localhost:5000/2
< HTTP/1.0 200 OK
$ curl -H "Content-Type: application/json" -d '{"name": "", "age": 19, "position": {"lati": 35, "longi": 127}}' -v http://localhost:5000/2
< HTTP/1.0 400 BAD REQUEST
$
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": 19, "position": {"lati": 35, "longi": 127}}' -v http://localhost:5000/3
< HTTP/1.0 200 OK
$ curl -H "Content-Type: application/json" -d '{"name": "PlanB", "age": "19", "position": {"lati": 35, "longi": 127}}' -v http://localhost:5000/3
< HTTP/1.0 400 BAD REQUEST