I had exactly this case recently, because my application is in Croatian 
language, so it was never good to have the model name automatically in the 
error messages (because it's rarely convenient for languages other than 
English), and I didn't want to write `Sequel.lit` all the time.

So, because my app is an API, and I wanted to raise custom validation 
errors so that my application can catch them easily and display as JSON, I 
did something like this:

module Utils
  module_function

  def valid!(object)
    if object.errors.any?
      errors = object.errors.flat_map { |column, messages| messages } # 
<=== this part
      raise MyCustomValidationError, errors
    end
  end
end

I'm sure you can do something similar by overriding 
`Sequel::Model::Errors#full_messages`, or by using it in your helper method 
for displaying the errors.

On Wednesday, May 27, 2015 at 6:13:57 AM UTC+2, Jon Mattingly wrote:
>
> I'm trying to set up validations for a couple of my models, and would like 
> to use a custom error message. However, sequel validations add the column 
> name before the message. 
>
> So if I do a validates_unique on the :username column, it will say 
> 'username is taken' or whatever the message is I specify. I'd like to 
> remove the username part of it and just return :message.
>
> I saw an example in the docs for  'errors.add(:name, Sequel.lit("Album 
> name is not valid"))' but that seems like a mass solution with no way to 
> limit it to a validates_unique error. I'd only want to do it for some 
> columns.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to