Skip to content

Instantly share code, notes, and snippets.

@BigNerd
Created March 29, 2022 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigNerd/7edca52cac381ff5e2ce4787b3ba5056 to your computer and use it in GitHub Desktop.
Save BigNerd/7edca52cac381ff5e2ce4787b3ba5056 to your computer and use it in GitHub Desktop.
strict pydantic base model validating that all fields are defined
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