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

Methods

Classes and Modules

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

Attributes

validations  [R]  A hash of associations 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 48
48:         def has_validations?
49:           !validations.empty?
50:         end

Setup the validations hash in the subclass

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 53
53:         def inherited(subclass)
54:           super
55:           subclass.class_eval do
56:             @validation_mutex = Mutex.new
57:             @validations = {}
58:           end
59:         end

Instructs the model to skip validations defined in superclasses

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 62
62:         def skip_superclass_validations
63:           @skip_superclass_validations = true
64:         end

Instructs the model to skip validations defined in superclasses

[Source]

    # File lib/sequel/plugins/validation_class_methods.rb, line 67
67:         def skip_superclass_validations?
68:           defined?(@skip_superclass_validations) && @skip_superclass_validations
69:         end

Validates the given instance.

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 91
 91:         def validate(o)
 92:           superclass.validate(o) if superclass.respond_to?(:validate) && !skip_superclass_validations?
 93:           validations.each do |att, procs|
 94:             v = case att
 95:             when Array
 96:               att.collect{|a| o.send(a)}
 97:             else
 98:               o.send(att)
 99:             end
100:             procs.each {|tag, p| p.call(o, att, v)}
101:           end
102:         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 86
86:         def validates(&block)
87:           Generator.new(self, &block)
88:         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 111
111:         def validates_acceptance_of(*atts)
112:           opts = {
113:             :message => 'is not accepted',
114:             :allow_nil => true,
115:             :accept => '1',
116:             :tag => :acceptance,
117:           }.merge!(extract_options!(atts))
118:           atts << opts
119:           validates_each(*atts) do |o, a, v|
120:             o.errors.add(a, opts[:message]) unless v == opts[:accept]
121:           end
122:         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 134
134:         def validates_confirmation_of(*atts)
135:           opts = {
136:             :message => 'is not confirmed',
137:             :tag => :confirmation,
138:           }.merge!(extract_options!(atts))
139:           atts << opts
140:           validates_each(*atts) do |o, a, v|
141:             o.errors.add(a, opts[:message]) unless v == o.send("#{a}_confirmation""#{a}_confirmation")
142:           end
143:         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 168
168:         def validates_each(*atts, &block)
169:           opts = extract_options!(atts)
170:           blk = if (i = opts[:if]) || (am = opts[:allow_missing]) || (an = opts[:allow_nil]) || (ab = opts[:allow_blank])
171:             proc do |o,a,v|
172:               next if i && !validation_if_proc(o, i)
173:               next if an && Array(v).all?{|x| x.nil?}
174:               next if ab && Array(v).all?{|x| x.blank?}
175:               next if am && Array(a).all?{|x| !o.values.has_key?(x)}
176:               block.call(o,a,v)
177:             end
178:           else
179:             block
180:           end
181:           tag = opts[:tag]
182:           atts.each do |a| 
183:             a_vals = @validation_mutex.synchronize{validations[a] ||= []}
184:             if tag && (old = a_vals.find{|x| x[0] == tag})
185:               old[1] = blk
186:             else
187:               a_vals << [tag, blk]
188:             end
189:           end
190:         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 198
198:         def validates_format_of(*atts)
199:           opts = {
200:             :message => 'is invalid',
201:             :tag => :format,
202:           }.merge!(extract_options!(atts))
203:           
204:           unless opts[:with].is_a?(Regexp)
205:             raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
206:           end
207:           
208:           atts << opts
209:           validates_each(*atts) do |o, a, v|
210:             o.errors.add(a, opts[:message]) unless v.to_s =~ opts[:with]
211:           end
212:         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 324
324:         def validates_inclusion_of(*atts)
325:           opts = extract_options!(atts)
326:           unless opts[:in] && opts[:in].respond_to?(:include?) 
327:             raise ArgumentError, "The :in parameter is required, and respond to include?"
328:           end
329:           opts[:message] ||= "is not in range or set: #{opts[:in].inspect}"
330:           atts << opts
331:           validates_each(*atts) do |o, a, v|
332:             o.errors.add(a, opts[:message]) unless opts[:in].include?(v)
333:           end
334:         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 :too_long, :too_short, and :wrong_length options if present)
  • :minimum - The minimum size allowed for the value (no default)
  • :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 226
226:         def validates_length_of(*atts)
227:           opts = {
228:             :too_long     => 'is too long',
229:             :too_short    => 'is too short',
230:             :wrong_length => 'is the wrong length'
231:           }.merge!(extract_options!(atts))
232:           
233:           opts[:tag] ||= ([:length] + [:maximum, :minimum, :is, :within].reject{|x| !opts.include?(x)}).join('-').to_sym
234:           atts << opts
235:           validates_each(*atts) do |o, a, v|
236:             if m = opts[:maximum]
237:               o.errors.add(a, opts[:message] || opts[:too_long]) unless v && v.size <= m
238:             end
239:             if m = opts[:minimum]
240:               o.errors.add(a, opts[:message] || opts[:too_short]) unless v && v.size >= m
241:             end
242:             if i = opts[:is]
243:               o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && v.size == i
244:             end
245:             if w = opts[:within]
246:               o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size)
247:             end
248:           end
249:         end

Validates whether an attribute is not a string. This is generally useful in conjunction with raise_on_typecast_failure = false, where you are passing in string values for non-string attributes (such as numbers and dates). If typecasting fails (invalid number or date), the value of the attribute will be a string in an invalid format, and if typecasting succeeds, the value will not be a string.

Possible Options:

  • :message - The message to use (default: ‘is a string’ or ‘is not a valid (integer|datetime|etc.)’ if the type is known)

[Source]

     # File lib/sequel/plugins/validation_class_methods.rb, line 260
260:         def validates_not_string(*atts)
261:           opts = {
262:             :tag => :not_string,
263:           }.merge!(extract_options!(atts))
264:           atts << opts
265:           validates_each(*atts) do |o, a, v|
266:             if v.is_a?(String)
267:               unless message = opts[:message]
268:                 message = if sch = o.db_schema[a] and typ = sch[:type]
269:                   "is not a valid #{typ}"
270:                 else
271:                   "is a string"
272:                 end
273:               end
274:               o.errors.add(a, message)
275:             end
276:           end
277:         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 284
284:         def validates_numericality_of(*atts)
285:           opts = {
286:             :message => 'is not a number',
287:             :tag => :numericality,
288:           }.merge!(extract_options!(atts))
289:           atts << opts
290:           validates_each(*atts) do |o, a, v|
291:             begin
292:               if opts[:only_integer]
293:                 Kernel.Integer(v.to_s)
294:               else
295:                 Kernel.Float(v.to_s)
296:               end
297:             rescue
298:               o.errors.add(a, opts[:message])
299:             end
300:           end
301:         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 308
308:         def validates_presence_of(*atts)
309:           opts = {
310:             :message => 'is not present',
311:             :tag => :presence,
312:           }.merge!(extract_options!(atts))
313:           atts << opts
314:           validates_each(*atts) do |o, a, v|
315:             o.errors.add(a, opts[:message]) if v.blank? && v != false
316:           end
317:         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 352
352:         def validates_uniqueness_of(*atts)
353:           opts = {
354:             :message => 'is already taken',
355:             :tag => :uniqueness,
356:           }.merge!(extract_options!(atts))
357:     
358:           atts << opts
359:           validates_each(*atts) do |o, a, v|
360:             error_field = a
361:             a = Array(a)
362:             v = Array(v)
363:             ds = o.class.filter(a.zip(v))
364:             num_dups = ds.count
365:             allow = if num_dups == 0
366:               # No unique value in the database
367:               true
368:             elsif num_dups > 1
369:               # Multiple "unique" values in the database!!
370:               # Someone didn't add a unique index
371:               false
372:             elsif o.new?
373:               # New record, but unique value already exists in the database
374:               false
375:             elsif ds.first === o
376:               # Unique value exists in database, but for the same record, so the update won't cause a duplicate record
377:               true
378:             else
379:               false
380:             end
381:             o.errors.add(error_field, opts[:message]) unless allow
382:           end
383:         end

[Validate]