Is there a way to use pydantic manually (without BaseModel) #2556
-
Hi, Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
PrettyWood
Mar 22, 2021
Replies: 1 comment 1 reply
-
Hi @erezinman from pydantic import ValidationError, parse_obj_as
def is_valid(value, type_):
try:
parse_obj_as(type_, value)
return True
except ValidationError:
return False
assert is_valid((1, 'q'), list[str]) If you want strictness, for now it's probably easier to use another lib like https://github.com/PrettyWood/typingx. Note there are probably better libs out there than mine. Hope it helps |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
erezinman
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @erezinman
If you want to keep the default behaviour of pydantic with coercion, there you can use the
parse_obj_as
utilIf you want strictness, for now it's probably easier to use another lib like https://github.com/PrettyWood/typingx. Note there are probably better libs out there than mine.
Hope it helps