| Module | Sequel::Plugins::ValidationHelpers::InstanceMethods |
| In: |
lib/sequel/plugins/validation_helpers.rb
|
ValidationHelpers contains instance method equivalents for most of the previous default validations. The names and APIs have changed, though.
The validates_unique validation has a unique API, but the other validations have the API explained here:
Arguments:
Options:
Check that the attribute values are the given exact length.
# File lib/sequel/plugins/validation_helpers.rb, line 30
30: def validates_exact_length(exact, atts, opts={})
31: validatable_attributes(atts, opts){|a,v,m| (m || "is not #{exact} characters") unless v && v.length == exact}
32: end
Check the string representation of the attribute value(s) against the regular expression with.
# File lib/sequel/plugins/validation_helpers.rb, line 35
35: def validates_format(with, atts, opts={})
36: validatable_attributes(atts, opts){|a,v,m| (m || 'is invalid') unless v.to_s =~ with}
37: end
Check attribute value(s) is included in the given set.
# File lib/sequel/plugins/validation_helpers.rb, line 40
40: def validates_includes(set, atts, opts={})
41: validatable_attributes(atts, opts){|a,v,m| (m || "is not in range or set: #{set.inspect}") unless set.include?(v)}
42: end
Check attribute value(s) string representation is a valid integer.
# File lib/sequel/plugins/validation_helpers.rb, line 45
45: def validates_integer(atts, opts={})
46: validatable_attributes(atts, opts) do |a,v,m|
47: begin
48: Kernel.Integer(v.to_s)
49: nil
50: rescue
51: m || 'is not a number'
52: end
53: end
54: end
Check that the attribute values length is in the specified range.
# File lib/sequel/plugins/validation_helpers.rb, line 57
57: def validates_length_range(range, atts, opts={})
58: validatable_attributes(atts, opts){|a,v,m| (m || "is outside the allowed range") unless v && range.include?(v.length)}
59: end
Check that the attribute values are not longer than the given max length.
# File lib/sequel/plugins/validation_helpers.rb, line 62
62: def validates_max_length(max, atts, opts={})
63: validatable_attributes(atts, opts){|a,v,m| (m || "is longer than #{max} characters") unless v && v.length <= max}
64: end
Check that the attribute values are not shorter than the given min length.
# File lib/sequel/plugins/validation_helpers.rb, line 67
67: def validates_min_length(min, atts, opts={})
68: validatable_attributes(atts, opts){|a,v,m| (m || "is shorter than #{min} characters") unless v && v.length >= min}
69: end
Check that the attribute value(s) 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.
# File lib/sequel/plugins/validation_helpers.rb, line 77
77: def validates_not_string(atts, opts={})
78: validatable_attributes(atts, opts) do |a,v,m|
79: next unless v.is_a?(String)
80: next m if m
81: (sch = db_schema[a] and typ = sch[:type]) ? "is not a valid #{typ}" : "is a string"
82: end
83: end
Check attribute value(s) string representation is a valid float.
# File lib/sequel/plugins/validation_helpers.rb, line 86
86: def validates_numeric(atts, opts={})
87: validatable_attributes(atts, opts) do |a,v,m|
88: begin
89: Kernel.Float(v.to_s)
90: nil
91: rescue
92: m || 'is not a number'
93: end
94: end
95: end
Check attribute value(s) is not considered blank by the database, but allow false values.
# File lib/sequel/plugins/validation_helpers.rb, line 98
98: def validates_presence(atts, opts={})
99: validatable_attributes(atts, opts){|a,v,m| (m || "is not present") if model.db.send(:blank_object?, v) && v != false}
100: end
Checks that there are no duplicate values in the database for the given attributes. 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_unique([:column1, :column2])
validates the grouping of column1 and column2 while
validates_unique(:column1, :column2)
validates them separately.
You should also add a unique index in the database, as this suffers from a fairly obvious race condition.
This validation does not respect the :allow_* options that the other validations accept, since it can deals with multiple attributes at once.
Possible Options:
# File lib/sequel/plugins/validation_helpers.rb, line 121
121: def validates_unique(*atts)
122: message = (atts.pop[:message] if atts.last.is_a?(Hash)) || 'is already taken'
123: atts.each do |a|
124: ds = model.filter(Array(a).map{|x| [x, send(x)]})
125: errors.add(a, message) unless (new? ? ds : ds.exclude(pk_hash)).count == 0
126: end
127: end