Method: Sequel::Plugins::ValidationClassMethods::ClassMethods#validates_format_of

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

#validates_format_of(*atts) ⇒ Object

Validates the format of an attribute, checking the string representation of the value against the regular expression provided by the :with option.

Possible Options:

:message

The message to use (default: ‘is invalid’)

:with

The regular expression to validate the value with (required).



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/sequel/plugins/validation_class_methods.rb', line 233

def validates_format_of(*atts)
  opts = {
    :message => 'is invalid',
    :tag => :format,
  }.merge!(extract_options!(atts))
  
  unless opts[:with].is_a?(Regexp)
    raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
  end
  
  reflect_validation(:format, opts, atts)
  atts << opts
  validates_each(*atts) do |o, a, v|
    o.errors.add(a, opts[:message]) unless v.to_s =~ opts[:with]
  end
end