Fastapi: Raising the error if input model has extra parameters?

Created on 21 Nov 2019  路  3Comments  路  Source: tiangolo/fastapi

Hello

How can I raise the error if input pydantic's model has extra parameters?
Right now all extra keys are just ignored.

Example:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class UpdateModel(BaseModel):
    name: str

@app.post("/items/")
async def update(item: UpdateModel):
    # do smth

If I send {"name": "FOO", "id": "BAR"} the ID key will be ignored.
Is it any common way to make method to raise error in that case?


Thanks for your work.

question

Most helpful comment

I believe the following should cause this to raise errors:

class UpdateModel(BaseModel):
    name: str
    class Config:
        extra = "forbid"

This should be documented in the pydantic docs.

All 3 comments

I believe the following should cause this to raise errors:

class UpdateModel(BaseModel):
    name: str
    class Config:
        extra = "forbid"

This should be documented in the pydantic docs.

Thank you! Missed this in docs.

Thanks for the help here @dmontagu ! :clap: :bow:

Thanks for reporting back and closing the issue @rappongy :+1:

Was this page helpful?
0 / 5 - 0 ratings