Method: Sequel::Plugins::ValidationClassMethods::ClassMethods#validates_schema_type

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

#validates_schema_type(*atts) ⇒ Object

Validates whether an attribute has the correct ruby type for the associated database type. This is generally useful in conjunction with raise_on_typecast_failure = false, to handle typecasting errors at validation time instead of at setter time.

Possible Options:

:message

The message to use (default: ‘is not a valid (integer|datetime|etc.)’)



358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/sequel/plugins/validation_class_methods.rb', line 358

def validates_schema_type(*atts)
  opts = {
    :tag => :schema_type,
  }.merge!(extract_options!(atts))
  reflect_validation(:schema_type, opts, atts)
  atts << opts
  validates_each(*atts) do |o, a, v|
    next if v.nil? || (klass = o.send(:schema_type_class, a)).nil?
    if klass.is_a?(Array) ? !klass.any?{|kls| v.is_a?(kls)} : !v.is_a?(klass)
      message = opts[:message] || "is not a valid #{Array(klass).join(" or ").downcase}"
      o.errors.add(a, message)
    end
  end
end