Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ponytailer/schema-validator
Flask/Quart pydantic/dataclass validator
https://github.com/ponytailer/schema-validator
flask quart swagger validator
Last synced: about 1 month ago
JSON representation
Flask/Quart pydantic/dataclass validator
- Host: GitHub
- URL: https://github.com/ponytailer/schema-validator
- Owner: ponytailer
- License: mit
- Archived: true
- Created: 2021-09-26T11:18:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-19T07:31:34.000Z (almost 3 years ago)
- Last Synced: 2024-11-15T06:08:17.284Z (2 months ago)
- Topics: flask, quart, swagger, validator
- Language: Python
- Homepage:
- Size: 125 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rst
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - ponytailer/schema-validator - Flask/Quart pydantic/dataclass validator (Python)
README
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 BaseModelfrom flask import Flask
from quart import Quart
from schema_validator import SchemaValidator
from schema_validator.flask import validate
# from schema_validator.quart import validateapp = Flask(__name__)
# or
app = Quart(__name__)
OR
schema = SchemaValidator(app)
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/swagger/docs -> show the all swagger
http://yourhost/swagger/docs/{tag} -> show the swagger which include tag
```
How to export the swagger
```
add command in flask/quart:
app.cli.add_command(generate_schema_command)Export all swagger to json file:
- flask/quart schema -o swagger.json
Export the swagger which include the ACCOUNT tag:
- flask/quart schema -o swagger.json -t ACCOUNT
```