0
votes

How do you change the default error messages given by Grape on Validation Errors?

For Example -

params do
    requires :email, allow_blank: false
end

If I don't pass the :email in the API call, grape will give error message as ['email is missing', 'email is empty'] but I want to override the message as ['Oops! Email is required.']

So, how can I override the default error messages for Grape default Validation Rules.

2
try to read gem documentation - Ilya
I have read the docs a few times, but it doesn't say anything about Custom Error messages. - Dwijen

2 Answers

1
votes
format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
  error!({ messages: e.full_messages.map { |msg| "Oops!" + msg }}, 400)
end

Update:

If you want to customize the complete message you can manually edit the grape locale file and override it in your application.

Grape locale en.yml

0
votes

It seems like the original answer is plain wrong:

https://github.com/ruby-grape/grape#custom-validation-messages

Which should work something like this for OP's example:

params do
    requires :email, allow_blank: { false, message: '' }, message 'Oops! Email is required.'
end

The tricky part is that OP is violating 2 validations but wants to have one message. Maybe the workaround above will work, though.