3 changed files with 49 additions and 7 deletions
@ -0,0 +1,32 @@ |
|||||
|
import yaml |
||||
|
from fastapi import FastAPI, HTTPException, Request |
||||
|
from pydantic import BaseModel, ValidationError |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
class Item(BaseModel): |
||||
|
name: str |
||||
|
tags: list[str] |
||||
|
|
||||
|
|
||||
|
@app.post( |
||||
|
"/items/", |
||||
|
openapi_extra={ |
||||
|
"requestBody": { |
||||
|
"content": {"application/x-yaml": {"schema": Item.model_json_schema()}}, |
||||
|
"required": True, |
||||
|
}, |
||||
|
}, |
||||
|
) |
||||
|
async def create_item(request: Request): |
||||
|
raw_body = await request.body() |
||||
|
try: |
||||
|
data = yaml.safe_load(raw_body) |
||||
|
except yaml.YAMLError: |
||||
|
raise HTTPException(status_code=422, detail="Invalid YAML") |
||||
|
try: |
||||
|
item = Item.model_validate(data) |
||||
|
except ValidationError as e: |
||||
|
raise HTTPException(status_code=422, detail=e.errors(include_url=False)) |
||||
|
return item |
||||
Loading…
Reference in new issue