Fastapi: [QUESTION] Undefined fields

Created on 19 Jan 2020  路  3Comments  路  Source: tiangolo/fastapi

First check

  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.

Description

Hello. I have BaseModel from pydantic with some fields. From postman I pass another field. My application ignore them. But I want to my application trow exception for this case. How I do this type validation?

question

Most helpful comment

@Hedgehogues
Hi! What you are looking for is an extra setting in the pydantic model config.

This works as follows:

from fastapi import FastAPI
from pydantic import BaseModel, BaseConfig

app = FastAPI()


class MyModel(BaseModel):
    field1: str
    field2: int

    class Config(BaseConfig):
        extra = "forbid"


@app.post("/my-route")
async def my_route(model_info: MyModel) -> None:
    # do something
    print(model_info.dict())

Using the httpie client and sending a request to this application, you'll get the following response:

$ http -v POST :8000/my-route field1=fastapi field2=42 field3=extra   
POST /my-route HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 56
Content-Type: application/json
Host: localhost:8000
User-Agent: HTTPie/1.0.3

{
    "field1": "fastapi",
    "field2": "42",
    "field3": "extra"
}

HTTP/1.1 422 Unprocessable Entity
content-length: 113
content-type: application/json
date: Sun, 19 Jan 2020 11:55:56 GMT
server: uvicorn

{
    "detail": [
        {
            "loc": [
                "body",
                "model_info",
                "field3"
            ],
            "msg": "extra fields not permitted",
            "type": "value_error.extra"
        }
    ]
}

All 3 comments

@Hedgehogues
Hi! What you are looking for is an extra setting in the pydantic model config.

This works as follows:

from fastapi import FastAPI
from pydantic import BaseModel, BaseConfig

app = FastAPI()


class MyModel(BaseModel):
    field1: str
    field2: int

    class Config(BaseConfig):
        extra = "forbid"


@app.post("/my-route")
async def my_route(model_info: MyModel) -> None:
    # do something
    print(model_info.dict())

Using the httpie client and sending a request to this application, you'll get the following response:

$ http -v POST :8000/my-route field1=fastapi field2=42 field3=extra   
POST /my-route HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 56
Content-Type: application/json
Host: localhost:8000
User-Agent: HTTPie/1.0.3

{
    "field1": "fastapi",
    "field2": "42",
    "field3": "extra"
}

HTTP/1.1 422 Unprocessable Entity
content-length: 113
content-type: application/json
date: Sun, 19 Jan 2020 11:55:56 GMT
server: uvicorn

{
    "detail": [
        {
            "loc": [
                "body",
                "model_info",
                "field3"
            ],
            "msg": "extra fields not permitted",
            "type": "value_error.extra"
        }
    ]
}

Yep. All what @nsidnev said :point_up:

Does that solve your issue @Hedgehogues ? If so, you can close it.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings