oxan/djangorestframework-dataclasses

Translation of optional fields on a dataclass

agorer opened this issue · 4 comments

Hi,
what is the motivation behind converting an optional field into allow_null=True instead of using required=False, allow_null=True, allow_blank=True? Even having an empty string does raise an error when validating an optional field.

oxan commented

Oversight. I agree that setting required=False is a good idea. Regarding allow_blank, I'm not so sure: it will allow the empty string, which is kind of orthogonal to whether None should be allowed. Especially since allowing both can create subtle differences in behaviour depending on whether None or an empty string is used. Note that whether a field is optional is detected from the type being nullable, which might still be a type that doesn't accept the empty string (e.g. Literal[None, "male", "female"]).

Do you agree?

Upon working more on my current problem (a list of parameters for a search) in my case it does not make much sense to use empty strings (either the parameter is there or is not) so I have just added the required=False to the extra_kwargs. So at least currently I agree with you that allow_blank is not necessary.

oxan commented

Ok great, I've merged #17 with that change.

Thanks! :)