Undefined fields #7723
Unanswered
Hedgehogues
asked this question in
Questions
Replies: 3 comments
-
@Hedgehogues 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 $ 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"
}
]
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yep. All what @nsidnev said ☝️ Does that solve your issue @Hedgehogues ? If so, you can close it. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First check
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?
Beta Was this translation helpful? Give feedback.
All reactions