A flask extension to provide schema validation with pydantic.
Project description
schema-validator
Generate from quart-schema
Install
pip install schema-validator
How to use
from dataclasses import dataclass
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from flask import Flask
from schema_validator import FlaskSchema, validate
app = Flask(__name__)
FlaskSchema(app)
OR
schema = FlaskSchema()
schema.init_app(app)
@dataclass
class Todo:
task: str
due: Optional[datetime]
class TodoResponse(BaseModel):
id: int
name: str
@app.post("/")
@validate(body=Todo, responses=TodoResponse)
def create_todo():
# balabala
return dict(id=1, name="2")
@app.get("/")
@validate(
query=Todo,
responses={200: TodoResponse, 400: TodoResponse}
)
def update_todo():
# balabala
return TodoResponse(id=1, name="123")
@app.delete("/")
@validate(
body=Todo,
responses={200: TodoResponse}
)
def delete():
# balabala
return jsonify(id=1)
@tags("SOME-TAG", "OTHER-TAG") # only for swagger
class View(MethodView):
@validate(...)
def get(self):
return {}
How to show the swagger
app.config["SWAGGER_ROUTE"] = True
http://yourhost/docs -> show the all swagger
http://yourhost/docs/{tag} -> show the swagger which include tag
How to export the swagger
add command in flask:
app.cli.add_command(generate_schema_command)
Export all swagger to json file:
- flask schema -o swagger.json
Export the swagger which include the ACCOUNT tag:
- flask schema -o swagger.json -t ACCOUNT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for schema_validator-0.1.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66c271c560f6796e78ec3fef9b38581e8197fa532d23d0db1f96dab4b1881419 |
|
MD5 | e0ea6c874554fa3884f587dad127b386 |
|
BLAKE2b-256 | 01a1ef8c946b8bafd4d03a7584ebf60b5938fe4efd4dfffbcefd8c9ef2192d4d |