Possible to have a custom error format for a particular validator?

456 views
Skip to first unread message

Alex Evanczuk

unread,
Nov 13, 2017, 7:49:07 AM11/13/17
to Grape Framework Discussion

Hi --
We are using more or less the custom grape en.yml, which includes:
en:
  grape
:
    errors
:
      format
: ! '%{attributes} %{message}'

I'd like to keep this, but override it for one specific validator
module Internal
 
module Shared
   
module Validators
     
class MustAgree < Grape::Validations::Base
       
def validate_param!(attr_name, params)
         
unless @option && params[attr_name] == @option
           
raise Grape::Exceptions::Validation, params: @attrs, message: 'Please agree to continue'
         
end
       
end
     
end
   
end
 
end
end

The field is called "Agree" and we end up getting an error message on the frontend that says "Agree Please agree to continue"
I'd like to get rid of this leading Agree without changing the format for all error messages.  Is this possible with custom validators?

Daniel D.

unread,
Nov 13, 2017, 6:26:11 PM11/13/17
to ruby-...@googlegroups.com
I think you just want to handle the validation error:

rescue_from Grape::Exceptions::ValidationErrors do |e|
  // return whatever you want
end

Or maybe a custom error formatter?

Give https://github.com/ruby-grape/grape#exception-handling a read, see if you can get it to work, and if not write a spec with what you expect in a Grape fork and we can try to help.

cheers
dB.


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages