A flask extension to provide schema validation with pydantic.
Project description
flask-dantic-schema
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():
... # Do something with data, e.g. save to the DB
return dict(id=1, name="2")
@app.put("/")
@validate(
body=Todo,
responses={200: TodoResponse, 400: TodoResponse}
)
def update_todo():
... # Do something with data, e.g. save to the DB
return TodoResponse(id=1, name="123")
app.cli.add_command(generate_schema_command)
virtualenv: flask schema swagger.json -> generate json swagger
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.2.tar.gz
(7.7 kB
view hashes)
Built Distribution
Close
Hashes for schema_validator-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66129ba3fc4ece86c046b6714c23a113c05323e7691a1d7c7acf8abfb134d05e |
|
MD5 | 8de588dd4bc23e75767e8136e6eb7f3f |
|
BLAKE2b-256 | c218b309f23552ebc02669f49d1ed2ec4256f1392a2f444165a602d3be4eb828 |