grape icon indicating copy to clipboard operation
grape copied to clipboard

set custom message when using proc to validate value

Open blainejohnson17 opened this issue 2 years ago • 4 comments

How can I use a proc to validate and use a custom message?

This way is deprecated:

params do
  requires :e, values: { proc: -> (v) { v.even? }, message: 'must be even' }
end

The new way doesn't support a custom message:

params do
  requires :e, values: -> (v) { v.even? }
end

blainejohnson17 avatar Sep 09 '22 21:09 blainejohnson17

I don't believe it's possible. Would be a good feature to add.

dblock avatar Sep 10 '22 13:09 dblock

@dblock , what do you think about enhancing the values[:value] to allow proc with arity 1 to support this use case?

params do
  requires :e, values: { value: -> (v) { v.even? }, message: 'must be even' }
end

blainejohnson17 avatar Sep 15 '22 17:09 blainejohnson17

I like it. Give the code a try?

dblock avatar Sep 15 '22 22:09 dblock

sure, I'll give it a go.

blainejohnson17 avatar Sep 16 '22 16:09 blainejohnson17

Turns out my PR description didn't close this automatically after the merge, @dblock could you close this for us?

TheDevJoao avatar Jun 05 '23 18:06 TheDevJoao