Last active
September 11, 2021 09:11
-
-
Save iamunr4v31/a47798daea9e57beb1ae6701c384f169 to your computer and use it in GitHub Desktop.
Response model verification
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
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