Skip to content

πŸš” A Pythonic way for validate requested JSON payload of Flask πŸš”

License

Notifications You must be signed in to change notification settings

callsign-viper/Flask-Validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

84 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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

About

πŸš” A Pythonic way for validate requested JSON payload of Flask πŸš”

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages