-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DTO): introduce
forbid_unknown_fields
config (#3690)
feat(DTO): introduce forbid_unknown_fields config
- Loading branch information
1 parent
b3cf2fd
commit 23ba256
Showing
6 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
docs/examples/data_transfer_objects/factory/unknown_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from typing_extensions import Annotated | ||
|
||
from litestar import Litestar, post | ||
from litestar.dto import DataclassDTO, DTOConfig | ||
|
||
|
||
@dataclass | ||
class User: | ||
id: str | ||
|
||
|
||
UserDTO = DataclassDTO[Annotated[User, DTOConfig(forbid_unknown_fields=True)]] | ||
|
||
|
||
@post("/users", dto=UserDTO) | ||
async def create_user(data: User) -> User: | ||
return data | ||
|
||
|
||
app = Litestar([create_user]) | ||
|
||
|
||
# run: /users -H "Content-Type: application/json" -d '{"id": "1", "name": "Peter"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters