Skip to content

Commit d17d0fe

Browse files
Replace custom field validators with Extra.forbid by @anton-shum
1 parent cab4b1a commit d17d0fe

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

README.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -664,49 +664,32 @@ print(type(post.content))
664664
# OUTPUT: Article
665665
# Article is very inclusive and all fields are optional, allowing any dict to become valid
666666
```
667-
**Not Terrible Solutions:**
668-
1. Order field types properly: from the most strict ones to loose ones.
667+
**Solutions:**
668+
1. Validate input has only valid fields
669669
```python
670-
class Post(BaseModel):
671-
content: Video | Article
672-
```
673-
2. Validate input has only valid fields
674-
```python
675-
from pydantic import BaseModel, root_validator
670+
from pydantic import BaseModel, Extra, root_validator
676671

677672
class Article(BaseModel):
678673
text: str | None
679674
extra: str | None
680675

681-
@root_validator(pre=True) # validate all values before pydantic
682-
def has_only_article_fields(cls, data: dict):
683-
"""Silly and ugly solution to validate data has only article fields."""
684-
fields = set(data.keys())
685-
if fields != {"text", "extra"}:
686-
raise ValueError("invalid fields")
687-
688-
return data
676+
class Config:
677+
extra = Extra.forbid
689678

690679

691680
class Video(BaseModel):
692681
video_id: int
693682
text: str | None
694683
extra: str | None
695684

696-
@root_validator(pre=True)
697-
def has_only_video_fields(cls, data: dict):
698-
"""Silly and ugly solution to validate data has only video fields."""
699-
fields = set(data.keys())
700-
if fields != {"text", "extra", "video_id"}:
701-
raise ValueError("invalid fields")
702-
703-
return data
685+
class Config:
686+
extra = Extra.forbid
704687

705688

706689
class Post(BaseModel):
707690
content: Article | Video
708691
```
709-
3. Use Pydantic's Smart Union (>v1.9) if fields are simple
692+
2. Use Pydantic's Smart Union (>v1.9) if fields are simple
710693

711694
It's a good solution if the fields are simple like `int` or `bool`,
712695
but it doesn't work for complex fields like classes.
@@ -748,6 +731,16 @@ print(type(p.field_2))
748731
print(type(p.content))
749732
# OUTPUT: Article, because smart_union doesn't work for complex fields like classes
750733
```
734+
735+
**Fast Workaround:**
736+
737+
Order field types properly: from the most strict ones to loose ones.
738+
739+
```python
740+
class Post(BaseModel):
741+
content: Video | Article
742+
```
743+
751744
### 19. SQL-first, Pydantic-second
752745
- Usually, database handles data processing much faster and cleaner than CPython will ever do.
753746
- It's preferable to do all the complex joins and simple data manipulations with SQL.

0 commit comments

Comments
 (0)