Giter Club home page Giter Club logo

schema-validator's Introduction

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 quart import Quart
    from schema_validator import SchemaValidator
    from schema_validator.flask import validate
    # from schema_validator.quart import validate


    app = 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

schema-validator's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xerxes-cn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.