Created
August 17, 2020 14:39
-
-
Save lcs-felix/c6d78f41a9155e6f126c988bd9fa01a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional | |
from fastapi import FastAPI | |
from pydantic import BaseModel, validator | |
class Item(BaseModel): | |
name: str | |
description: Optional[str] = None | |
price: float | |
tax: Optional[float] = None | |
@validator('name') | |
def name_must_contain_space(cls, v): | |
if ' ' not in v: | |
raise ValueError('must contain a space') | |
return v.title() | |
@validator('description') | |
def description_length(cls, v): | |
if len(v) < 10: | |
raise ValueError('description must be 3 chars long') | |
return v | |
app = FastAPI() | |
@app.post("/items/") | |
async def create_item(item: Item): | |
return item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment