Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashidSerializerCharField returns the non formatted message "'%(value)s' value must be a positive integer or a valid Hashids string." #70

Closed
yuu-eguci opened this issue Jun 17, 2022 · 2 comments

Comments

@yuu-eguci
Copy link

Premise

  • django: v3.2
  • django-hashid-field: v3.3.4
  • djangorestframework: 3.13.1

Description

Hello.
When I pass invalid hashid str, 'foo' for example, HashidSerializerCharField returns a message below.

"'%(value)s' value must be a positive integer or a valid Hashids string."

What I expect is below.

"'foo' value must be a positive integer or a valid Hashids string."

My serializer is defined like below.

class FooSerializer(serializers.Serializer):
    foo = HashidSerializerCharField(source_field='FooModel.id', required=True)

Is there the best way to get a valid message?

What causes the problem

I researched what causes the problem. I hope it helps you.

The original message is defined here below.
https://github.com/nshafer/django-hashid-field/blob/master/hashid_field/rest.py#L24

'invalid': _("'%(value)s' value must be a positive integer or a valid Hashids string."),

When ValueError occurs, the invalid str is sent as 'value' to fail method.
https://github.com/nshafer/django-hashid-field/blob/master/hashid_field/rest.py#L65

except ValueError:
    self.fail('invalid', value=data)

Then it is expected that the message is going to be formatted like below.

msg % {'value': value}

Though it is formatted in the rest_framework actually like below.
https://github.com/encode/django-rest-framework/blob/master/rest_framework/fields.py#L642

message_string = msg.format(**kwargs)

This way of formatting doesn't format %(value)s.
Thus the message "'%(value)s' value must be a positive integer or a valid Hashids string." is returned.
If the origial message were like "'{value}' value must be a positive integer or a valid Hashids string." for example, formatting functions well.

nshafer added a commit that referenced this issue Jun 17, 2022
@nshafer
Copy link
Owner

nshafer commented Jun 17, 2022

Thank you for the detailed bug report, it made it very easy to see what you're talking about. I guess DRF changed the error formatting at some point, so I have updated to match. 3.3.5 released. Thanks!

@nshafer nshafer closed this as completed Jun 17, 2022
@yuu-eguci
Copy link
Author

yuu-eguci commented Jun 18, 2022

Happy to hear that!

I guess DRF changed the error formatting at some point

I understand. Thank you so much for updating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants