Created
March 29, 2022 08:46
-
-
Save BigNerd/7edca52cac381ff5e2ce4787b3ba5056 to your computer and use it in GitHub Desktop.
strict pydantic base model validating that all fields are defined
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
from pydantic import BaseModel, root_validator | |
class StrictBaseModel(BaseModel): | |
@root_validator(pre=True) | |
def check_all_fields_are_defined(cls, values): | |
for v in values: | |
if v not in cls.__fields__: | |
raise ValueError(f"{v} is not a field of {cls}") | |
return values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment