-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert all empty strings to None before validation
- Loading branch information
Showing
5 changed files
with
34 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
This module contains a generic plugin for Pydantic models that removes | ||
empty strings and converts them to None. | ||
""" | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, model_validator | ||
|
||
|
||
class EmptyStringsToNone(BaseModel): | ||
""" | ||
Mixin class for Pydantic models | ||
""" | ||
|
||
@model_validator(mode="before") | ||
def empty_string_to_none(cls, data: Any) -> Any: | ||
""" | ||
Pydantic model validator, converting empty strings to None | ||
""" | ||
if isinstance(data, dict): | ||
for k, v in data.items(): | ||
if isinstance(v, str) and v == "": | ||
data[k] = None | ||
return data |