Module: Sequel::Plugins::ValidationHelpers

Defined in:
lib/sequel/plugins/validation_helpers.rb

Overview

The validation_helpers plugin contains validate_* methods designed to be called inside Model#validate to perform validations:

Sequel::Model.plugin :validation_helpers
class Album < Sequel::Model
  def validate
    super
    validates_min_length 1, :num_tracks
  end
end

The validates_unique method has a unique API, but the other validations have the API explained here:

Arguments:

atts

Single attribute symbol or an array of attribute symbols specifying the attribute(s) to validate.

Options:

:allow_blank

Whether to skip the validation if the value is blank.

:allow_missing

Whether to skip the validation if the attribute isn’t a key in the values hash. This is different from allow_nil, because Sequel only sends the attributes in the values when doing an insert or update. If the attribute is not present, Sequel doesn’t specify it, so the database will use the table’s default value. This is different from having an attribute in values with a value of nil, which Sequel will send as NULL. If your database table has a non NULL default, this may be a good option to use. You don’t want to use allow_nil, because if the attribute is in values but has a value nil, Sequel will attempt to insert a NULL value into the database, instead of using the database’s default.

:allow_nil

Whether to skip the validation if the value is nil.

:from

Set to :values to pull column values from the values hash instead of calling the related method. Allows for setting up methods on the underlying column values, in the cases where the model transforms the underlying value before returning it, such as when using serialization.

:message

The message to use. Can be a string which is used directly, or a proc which is called. If the validation method takes a argument before the array of attributes, that argument is passed as an argument to the proc.

:skip_invalid

Do not try to validate columns that are already invalid.

The default validation options for all models can be modified by overridding the Model#default_validation_helpers_options private method. By changing the default options, you can setup internationalization of the error messages. For example, you would modify the default options:

class Sequel::Model
  private

  def default_validation_helpers_options(type)
    case type
    when :exact_length
      {message: lambda{|exact| I18n.t("errors.exact_length", exact: exact)}}
    when :integer
      {message: lambda{I18n.t("errors.integer")}}
    else
      super
    end
  end
end

and then use something like this in your yaml translation file:

en:
  errors:
    exact_length: "is not %{exact} characters"
    integer: "is not a number"

Note that if you want to support internationalization of Errors#full_messages, it is easiest to override Errors#full_message (note singular form and not plural form). Here’s an example:

class Sequel::Model::Errors
  private
  def full_message(attribute, error_msg)
    "#{Array(attribute).join(I18n.t('errors.joiner'))} #{error_msg}"
  end
end

It is recommended that users of this plugin that use validates_schema_types also use the validation_helpers_generic_type_messages plugin for more useful type validation failure messages.

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :exact_length=>{:message=>lambda{|exact| "is not #{exact} characters"}},
  :format=>{:message=>lambda{|with| 'is invalid'}},
  :includes=>{:message=>lambda{|set| "is not in range or set: #{set.inspect}"}},
  :integer=>{:message=>lambda{"is not a number"}},
  :length_range=>{:message=>lambda{|range| "is too short or too long"}},
  :max_length=>{:message=>lambda{|max| "is longer than #{max} characters"}, :nil_message=>lambda{"is not present"}},
  :max_value=>{:message=>lambda{|max| "is greater than maximum allowed value"}},
  :min_length=>{:message=>lambda{|min| "is shorter than #{min} characters"}},
  :min_value=>{:message=>lambda{|min| "is less than minimum allowed value"}},
  :not_null=>{:message=>lambda{"is not present"}},
  :no_null_byte=>{:message=>lambda{"contains a null byte"}},
  :numeric=>{:message=>lambda{"is not a number"}},
  :operator=>{:message=>lambda{|operator, rhs| "is not #{operator} #{rhs}"}},
  :type=>{:message=>lambda{|klass| klass.is_a?(Array) ? "is not a valid #{klass.join(" or ").downcase}" : "is not a valid #{klass.to_s.downcase}"}},
  :presence=>{:message=>lambda{"is not present"}},
  :unique=>{:message=>lambda{'is already taken'}}
}.freeze