Skip to content

Instantly share code, notes, and snippets.

@lcs-felix
Created August 17, 2020 14:39
Show Gist options
  • Save lcs-felix/c6d78f41a9155e6f126c988bd9fa01a6 to your computer and use it in GitHub Desktop.
Save lcs-felix/c6d78f41a9155e6f126c988bd9fa01a6 to your computer and use it in GitHub Desktop.
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