Module Sequel::Plugins::ValidationClassMethods::ClassMethods
In: lib/sequel/plugins/validation_class_methods.rb

Methods

Classes and Modules

Class Sequel::Plugins::ValidationClassMethods::ClassMethods::Generator

Attributes

validation_reflections  [R]  A hash of validation reflections for this model class. Keys are column symbols, values are an array of two element arrays, with the first element being the validation type symbol and the second being a hash of validation options.
validations  [R]  A hash of validations for this model class. Keys are column symbols, values are arrays of validation procs.

Public Instance methods

Returns true if validations are defined.

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 63
63:         def has_validations?
64:           !validations.empty?
65:         end

Instructs the model to skip validations defined in superclasses

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 70
70:         def skip_superclass_validations
71:           superclass.validations.each do |att, procs|
72:             if @validations[att]
73:               @validations[att] -= procs
74:             end
75:           end
76:           @skip_superclass_validations = true
77:         end

Instructs the model to skip validations defined in superclasses

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 80
80:         def skip_superclass_validations?
81:           @skip_superclass_validations
82:         end

Validates the given instance.

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 104
104:         def validate(o)
105:           validations.each do |att, procs|
106:             v = case att
107:             when Array
108:               att.collect{|a| o.get_column_value(a)}
109:             else
110:               o.get_column_value(att)
111:             end
112:             procs.each {|tag, p| p.call(o, att, v)}
113:           end
114:         end

Defines validations by converting a longhand block into a series of shorthand definitions. For example:

  class MyClass < Sequel::Model
    validates do
      length_of :name, :minimum => 6
      length_of :password, :minimum => 8
    end
  end

is equivalent to:

  class MyClass < Sequel::Model
    validates_length_of :name, :minimum => 6
    validates_length_of :password, :minimum => 8
  end

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 99
 99:         def validates(&block)
100:           Generator.new(self, &block)
101:         end

Validates acceptance of an attribute. Just checks that the value is equal to the :accept option. This method is unique in that :allow_nil is assumed to be true instead of false.

Possible Options:

:accept :The value required for the object to be valid (default: ‘1’)
:message :The message to use (default: ‘is not accepted’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 123
123:         def validates_acceptance_of(*atts)
124:           opts = {
125:             :message => 'is not accepted',
126:             :allow_nil => true,
127:             :accept => '1',
128:             :tag => :acceptance,
129:           }.merge!(extract_options!(atts))
130:           reflect_validation(:acceptance, opts, atts)
131:           atts << opts
132:           validates_each(*atts) do |o, a, v|
133:             o.errors.add(a, opts[:message]) unless v == opts[:accept]
134:           end
135:         end

Validates confirmation of an attribute. Checks that the object has a _confirmation value matching the current value. For example:

  validates_confirmation_of :blah

Just makes sure that object.blah = object.blah_confirmation. Often used for passwords or email addresses on web forms.

Possible Options:

:message :The message to use (default: ‘is not confirmed’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 147
147:         def validates_confirmation_of(*atts)
148:           opts = {
149:             :message => 'is not confirmed',
150:             :tag => :confirmation,
151:           }.merge!(extract_options!(atts))
152:           reflect_validation(:confirmation, opts, atts)
153:           atts << opts
154:           validates_each(*atts) do |o, a, v|
155:             o.errors.add(a, opts[:message]) unless v == o.get_column_value("#{a}_confirmation""#{a}_confirmation")
156:           end
157:         end

Adds a validation for each of the given attributes using the supplied block. The block must accept three arguments: instance, attribute and value, e.g.:

  validates_each :name, :password do |object, attribute, value|
    object.errors.add(attribute, 'is not nice') unless value.nice?
  end

Possible 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.
:if :A symbol (indicating an instance_method) or proc (which is instance_evaled) skipping this validation if it returns nil or false.
:tag :The tag to use for this validation.

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 182
182:         def validates_each(*atts, &block)
183:           opts = extract_options!(atts)
184:           blk = if (i = opts[:if]) || (am = opts[:allow_missing]) || (an = opts[:allow_nil]) || (ab = opts[:allow_blank])
185:             proc do |o,a,v|
186:               next if i && !validation_if_proc(o, i)
187:               next if an && Array(v).all?(&:nil?)
188:               next if ab && Array(v).all?(&:blank?)
189:               next if am && Array(a).all?{|x| !o.values.has_key?(x)}
190:               block.call(o,a,v)
191:             end
192:           else
193:             block
194:           end
195:           tag = opts[:tag]
196:           atts.each do |a| 
197:             a_vals = Sequel.synchronize{validations[a] ||= []}
198:             if tag && (old = a_vals.find{|x| x[0] == tag})
199:               old[1] = blk
200:             else
201:               a_vals << [tag, blk]
202:             end
203:           end
204:         end

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).

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 212
212:         def validates_format_of(*atts)
213:           opts = {
214:             :message => 'is invalid',
215:             :tag => :format,
216:           }.merge!(extract_options!(atts))
217:           
218:           unless opts[:with].is_a?(Regexp)
219:             raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
220:           end
221:           
222:           reflect_validation(:format, opts, atts)
223:           atts << opts
224:           validates_each(*atts) do |o, a, v|
225:             o.errors.add(a, opts[:message]) unless v.to_s =~ opts[:with]
226:           end
227:         end

Validates that an attribute is within a specified range or set of values.

Possible Options:

:in :An array or range of values to check for validity (required)
:message :The message to use (default: ‘is not in range or set: <specified range>’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 316
316:         def validates_inclusion_of(*atts)
317:           opts = extract_options!(atts)
318:           n = opts[:in]
319:           unless n && (n.respond_to?(:cover?) || n.respond_to?(:include?))
320:             raise ArgumentError, "The :in parameter is required, and must respond to cover? or include?"
321:           end
322:           opts[:message] ||= "is not in range or set: #{n.inspect}"
323:           reflect_validation(:inclusion, opts, atts)
324:           atts << opts
325:           validates_each(*atts) do |o, a, v|
326:             o.errors.add(a, opts[:message]) unless n.send(n.respond_to?(:cover?) ? :cover? : :include?, v)
327:           end
328:         end

Validates the length of an attribute.

Possible Options:

:is :The exact size required for the value to be valid (no default)
:maximum :The maximum size allowed for the value (no default)
:message :The message to use (no default, overrides :nil_message, :too_long, :too_short, and :wrong_length options if present)
:minimum :The minimum size allowed for the value (no default)
:nil_message :The message to use use if :maximum option is used and the value is nil (default: ‘is not present’)
:too_long :The message to use use if it the value is too long (default: ‘is too long’)
:too_short :The message to use use if it the value is too short (default: ‘is too short’)
:within :The array/range that must include the size of the value for it to be valid (no default)
:wrong_length :The message to use use if it the value is not valid (default: ‘is the wrong length’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 242
242:         def validates_length_of(*atts)
243:           opts = {
244:             :nil_message  => 'is not present',
245:             :too_long     => 'is too long',
246:             :too_short    => 'is too short',
247:             :wrong_length => 'is the wrong length'
248:           }.merge!(extract_options!(atts))
249:           
250:           opts[:tag] ||= ([:length] + [:maximum, :minimum, :is, :within].reject{|x| !opts.include?(x)}).join('-').to_sym
251:           reflect_validation(:length, opts, atts)
252:           atts << opts
253:           validates_each(*atts) do |o, a, v|
254:             if m = opts[:maximum]
255:               o.errors.add(a, opts[:message] || (v ? opts[:too_long] : opts[:nil_message])) unless v && v.size <= m
256:             end
257:             if m = opts[:minimum]
258:               o.errors.add(a, opts[:message] || opts[:too_short]) unless v && v.size >= m
259:             end
260:             if i = opts[:is]
261:               o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && v.size == i
262:             end
263:             if w = opts[:within]
264:               o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && w.send(w.respond_to?(:cover?) ? :cover? : :include?, v.size)
265:             end
266:           end
267:         end

Validates whether an attribute is a number.

Possible Options:

:message :The message to use (default: ‘is not a number’)
:only_integer :Whether only integers are valid values (default: false)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 274
274:         def validates_numericality_of(*atts)
275:           opts = {
276:             :message => 'is not a number',
277:             :tag => :numericality,
278:           }.merge!(extract_options!(atts))
279:           reflect_validation(:numericality, opts, atts)
280:           atts << opts
281:           validates_each(*atts) do |o, a, v|
282:             begin
283:               if opts[:only_integer]
284:                 Kernel.Integer(v.to_s)
285:               else
286:                 Kernel.Float(v.to_s)
287:               end
288:             rescue
289:               o.errors.add(a, opts[:message])
290:             end
291:           end
292:         end

Validates the presence of an attribute. Requires the value not be blank, with false considered present instead of absent.

Possible Options:

:message :The message to use (default: ‘is not present’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 299
299:         def validates_presence_of(*atts)
300:           opts = {
301:             :message => 'is not present',
302:             :tag => :presence,
303:           }.merge!(extract_options!(atts))
304:           reflect_validation(:presence, opts, atts)
305:           atts << opts
306:           validates_each(*atts) do |o, a, v|
307:             o.errors.add(a, opts[:message]) if v.blank? && v != false
308:           end
309:         end

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.)’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 337
337:         def validates_schema_type(*atts)
338:           opts = {
339:             :tag => :schema_type,
340:           }.merge!(extract_options!(atts))
341:           reflect_validation(:schema_type, opts, atts)
342:           atts << opts
343:           validates_each(*atts) do |o, a, v|
344:             next if v.nil? || (klass = o.send(:schema_type_class, a)).nil?
345:             if klass.is_a?(Array) ? !klass.any?{|kls| v.is_a?(kls)} : !v.is_a?(klass)
346:               message = opts[:message] || "is not a valid #{Array(klass).join(" or ").downcase}"
347:               o.errors.add(a, message)
348:             end
349:           end
350:         end

Validates only if the fields in the model (specified by atts) are unique in the database. Pass an array of fields instead of multiple fields to specify that the combination of fields must be unique, instead of that each field should have a unique value.

This means that the code:

  validates_uniqueness_of([:column1, :column2])

validates the grouping of column1 and column2 while

  validates_uniqueness_of(:column1, :column2)

validates them separately.

You should also add a unique index in the database, as this suffers from a fairly obvious race condition.

Possible Options:

:message :The message to use (default: ‘is already taken’)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 368
368:         def validates_uniqueness_of(*atts)
369:           opts = {
370:             :message => 'is already taken',
371:             :tag => :uniqueness,
372:           }.merge!(extract_options!(atts))
373:     
374:           reflect_validation(:uniqueness, opts, atts)
375:           atts << opts
376:           validates_each(*atts) do |o, a, v|
377:             error_field = a
378:             a = Array(a)
379:             v = Array(v)
380:             next if v.empty? || !v.all?
381:             ds = o.class.filter(a.zip(v))
382:             num_dups = ds.count
383:             allow = if num_dups == 0
384:               # No unique value in the database
385:               true
386:             elsif num_dups > 1
387:               # Multiple "unique" values in the database!!
388:               # Someone didn't add a unique index
389:               false
390:             elsif o.new?
391:               # New record, but unique value already exists in the database
392:               false
393:             elsif ds.first === o
394:               # Unique value exists in database, but for the same record, so the update won't cause a duplicate record
395:               true
396:             else
397:               false
398:             end
399:             o.errors.add(error_field, opts[:message]) unless allow
400:           end
401:         end

[Validate]