Skip to content

Instantly share code, notes, and snippets.

@iamunr4v31
Last active September 11, 2021 09:11
Show Gist options
  • Save iamunr4v31/a47798daea9e57beb1ae6701c384f169 to your computer and use it in GitHub Desktop.
Save iamunr4v31/a47798daea9e57beb1ae6701c384f169 to your computer and use it in GitHub Desktop.
Response model verification
def validate(model, data):
if not isinstance(model, dict):
model = model.__dict__["__annotations__"]
for k, v in model.items():
assert k in data, f"Key {k} is not available in data provided"
if 'typing.' in str(model[k]):
types = model[k].__args__
assert any(map(lambda x: isinstance(data[k], x), types)), f"Value of key {k}: {data[k]} is not of type(s) {types}"
elif issubclass(v, BaseModel):
validate(model[k], data[k])
else:
assert isinstance(data[k], model[k]), f"Value of key {k}: {data[k]} is not of type {model[k]} but of type {type(data[k])}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment