Replies: 2 comments 1 reply
-
Hmm, good question. Letting the user know these are invalid is useful and needs to raise an error to let the user know. That can help debugging when things go wrong like something manipulated the ID on accident. Returning |
Beta Was this translation helpful? Give feedback.
1 reply
-
I just got this on my app as well. The user updated their username to foo-bar, with a dash. Does it make sense to validate against dashes for something like this? Why are dashes not allowed with hash ids? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using the finder methods, either the overridden
find
orfind_by_prefix_id
andfind_by_prefix_id!
, the value we are trying to find is eventually delegated to HashIds to decode (https://github.com/excid3/prefixed_ids/blob/master/lib/prefixed_ids/prefix_id.rb#L21). If this value contains any characters outside of the specific HashId alphabet, HashId will raise aHashids::InputError
with the messageunable to unhash
.In a lot of web-apps, the
id
parameter to thisfind
action would come from user-input, likely from the URL. If the user makes any typo, such as adding a dash instead of an underscore, or happens to use UUID-based IDs (which contain a dash), then the finder method will raise and cause a 500... even for the non-bang find methods.Could be solved in app with a
rescue_from
in the Controller, or validating the format ofid
but for a finder method specifically, it seems more appropriate to returnnil
or raiseActiveRecord::RecordNotFound
rather than raiseHashids::InputError
.I've monkey patched my app using the following:
If this is something you'd be interested in accepting, I can throw together a PR.
Cheers!
Beta Was this translation helpful? Give feedback.
All reactions