Reputation: 590
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.
Upvotes: 0
Views: 3215
Reputation: 1301
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.
Upvotes: 0
Reputation: 1175
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.
Upvotes: 2