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.put("/")
@validate(
body=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")
class View(MethodView):
@validate(...)
def get(self):
return {}
app.cli.add_command(generate_schema_command)
virtualenv: flask schema swagger.json -> generate json swagger
FEATURES
- direct package/api/view_class name to export json-swagger
- direct tag to swagger html
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
schema_validator-0.1.8.tar.gz
(7.8 kB
view hashes)
Built Distribution
Close
Hashes for schema_validator-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 474a70d09d9374b7392c400faa4b4ff7229b248e2d3f9b593dd6d4df6f8d8880 |
|
MD5 | e9d3af34238f2f0b9e5d5fd574f52411 |
|
BLAKE2b-256 | 274a670fd51e63b5c58d7cdf73c06da1c24ae796b0a54bc903ad1e9e07ddb268 |