graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Make Decimal type return None if parsing an empty string

Open asnoeyink opened this issue 2 years ago • 1 comments

Would fix #927.

When using forms in the frontend, it's often useful for the "null" value of the form field to be an empty string, as HTML Input elements don't take null for a value. When converting that form value into a Graphene Decimal type, an empty string will currently cause a ConversionError. This PR makes empty strings a valid "null" value for Decimal.

asnoeyink avatar Aug 25 '21 14:08 asnoeyink

Hey @abs25, I'm not a contributor, but I also encountered this issue. I think the issue here is that the original author of Graphene's Decimal type assumed that a ValueError would be raised in the case of a value that couldn't be parsed. However, that's not the case.

Rather than checking for a blank string, I a better solution would be to change except ValueError to except decimal.InvalidOperation. This will handle scenarios where someone sends "abc" to the server as well.

rzane avatar Mar 01 '22 22:03 rzane