Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/callsign-viper/Flask-Validation
π A Pythonic way for validate requested JSON payload of Flask π
https://github.com/callsign-viper/Flask-Validation
decorator extension flask flask-extension jsonschema library validation
Last synced: 3 months ago
JSON representation
π A Pythonic way for validate requested JSON payload of Flask π
- Host: GitHub
- URL: https://github.com/callsign-viper/Flask-Validation
- Owner: callsign-viper
- License: apache-2.0
- Created: 2018-07-17T13:04:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T02:20:27.000Z (about 2 years ago)
- Last Synced: 2024-10-06T21:45:54.939Z (3 months ago)
- Topics: decorator, extension, flask, flask-extension, jsonschema, library, validation
- Language: Python
- Homepage:
- Size: 63.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Flask-Validation [![Build Status](https://travis-ci.org/callsign-viper/Flask-Validation.svg?branch=master)](https://travis-ci.org/callsign-viper/Flask-Validation) [![Documentation Status](https://readthedocs.org/projects/flask-validate/badge/?version=latest)](https://flask-validate.readthedocs.io/en/latest/?badge=latest) [![Coverage Status](https://coveralls.io/repos/github/callsign-viper/Flask-Validation/badge.svg?branch=master)](https://coveralls.io/github/callsign-viper/Flask-Validation?branch=master)
```bash
$ pip install flask-validation
```Pythonic JSON payload validator for requested JSON payload of Flask
Flaskλ₯Ό μν view decorator κΈ°λ°μ JSON μμ² λ°μ΄ν° validation λΌμ΄λΈλ¬λ¦¬
## Example Usages
### json_required```python
from flask import Flask
from flask_validation import json_required, Validatorapp = Flask(__name__)
Validator(app)@json_required
@app.route('/', methods=('POST'))
def index():
return 'hello!'
app.run()
``````bash
$ 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
```python
from flask import Flask
from flask_validation import validate_keys, Validatorapp = Flask(__name__)
Validator(app)@validate_keys(['name', 'age', { 'position': ['lati', 'longi'] }])
@app.route('/', methods=('POST'))
def index():
return 'hello!'
app.run()
``````bash
$ 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
```python
from flask import Flask
from flask_validation import validate_common, validate_with_fields, validate_with_jsonschema, Validatorapp = 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()
``````bash
$ 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
```