You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to register a structure hook only for optional types. I've search both existing issues and the docs, but please let me know if I missed this somehow.
What I Did
Suppose I receive some data where values can be "" and I want to convert them to None. I only want to convert the fields that are annotated as optional on my model though, fields that are not annotated as optional should raise a validation error.
I've tried both these, but it seems like they aren't registered at all:
converter.register_structure_hook(Optional[int], _int_or_none)
converter.register_structure_hook(int|None, _int_or_none)
d= {"bar": "", "baz": "2"}
converter.structure(d, Foo) # with either of the above, tries to convert "" to int
This works, but it also converts non-optional fields to None:
Description
I'd like to register a structure hook only for optional types. I've search both existing issues and the docs, but please let me know if I missed this somehow.
What I Did
Suppose I receive some data where values can be
""
and I want to convert them toNone
. I only want to convert the fields that are annotated as optional on my model though, fields that are not annotated as optional should raise a validation error.I've tried both these, but it seems like they aren't registered at all:
This works, but it also converts non-optional fields to
None
:The text was updated successfully, but these errors were encountered: