Module | Sequel::Plugins::ValidationHelpers::InstanceMethods |
In: |
lib/sequel/plugins/validation_helpers.rb
|
Check that the attribute values are the given exact length.
# File lib/sequel/plugins/validation_helpers.rb, line 37 37: def validates_exact_length(exact, atts, opts={}) 38: validatable_attributes(atts, opts){|a,v,m| (m || "is not #{exact} characters") unless v && v.length == exact} 39: end
Check the string representation of the attribute value(s) against the regular expression with.
# File lib/sequel/plugins/validation_helpers.rb, line 42 42: def validates_format(with, atts, opts={}) 43: validatable_attributes(atts, opts){|a,v,m| (m || 'is invalid') unless v.to_s =~ with} 44: end
Check attribute value(s) is included in the given set.
# File lib/sequel/plugins/validation_helpers.rb, line 47 47: def validates_includes(set, atts, opts={}) 48: validatable_attributes(atts, opts){|a,v,m| (m || "is not in range or set: #{set.inspect}") unless set.include?(v)} 49: end
Check attribute value(s) string representation is a valid integer.
# File lib/sequel/plugins/validation_helpers.rb, line 52 52: def validates_integer(atts, opts={}) 53: validatable_attributes(atts, opts) do |a,v,m| 54: begin 55: Kernel.Integer(v.to_s) 56: nil 57: rescue 58: m || 'is not a number' 59: end 60: end 61: end
Check that the attribute values length is in the specified range.
# File lib/sequel/plugins/validation_helpers.rb, line 64 64: def validates_length_range(range, atts, opts={}) 65: validatable_attributes(atts, opts){|a,v,m| (m || "is outside the allowed range") unless v && range.include?(v.length)} 66: end
Check that the attribute values are not longer than the given max length.
# File lib/sequel/plugins/validation_helpers.rb, line 69 69: def validates_max_length(max, atts, opts={}) 70: validatable_attributes(atts, opts){|a,v,m| (m || "is longer than #{max} characters") unless v && v.length <= max} 71: end
Check that the attribute values are not shorter than the given min length.
# File lib/sequel/plugins/validation_helpers.rb, line 74 74: def validates_min_length(min, atts, opts={}) 75: validatable_attributes(atts, opts){|a,v,m| (m || "is shorter than #{min} characters") unless v && v.length >= min} 76: 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 84 84: def validates_not_string(atts, opts={}) 85: validatable_attributes(atts, opts) do |a,v,m| 86: next unless v.is_a?(String) 87: next m if m 88: (sch = db_schema[a] and typ = sch[:type]) ? "is not a valid #{typ}" : "is a string" 89: end 90: end
Check attribute value(s) string representation is a valid float.
# File lib/sequel/plugins/validation_helpers.rb, line 93 93: def validates_numeric(atts, opts={}) 94: validatable_attributes(atts, opts) do |a,v,m| 95: begin 96: Kernel.Float(v.to_s) 97: nil 98: rescue 99: m || 'is not a number' 100: end 101: end 102: end
Check attribute value(s) is not considered blank by the database, but allow false values.
# File lib/sequel/plugins/validation_helpers.rb, line 105 105: def validates_presence(atts, opts={}) 106: validatable_attributes(atts, opts){|a,v,m| (m || "is not present") if model.db.send(:blank_object?, v) && v != false} 107: 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 can pass a block, which is yielded the dataset in which the columns must be unique. So if you are doing a soft delete of records, in which the name must be unique, but only for active records:
validates_unique(:name){|ds| ds.filter(:active)}
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 deal with a grouping of multiple attributes.
Possible Options:
# File lib/sequel/plugins/validation_helpers.rb, line 134 134: def validates_unique(*atts) 135: message = (atts.pop[:message] if atts.last.is_a?(Hash)) || 'is already taken' 136: atts.each do |a| 137: ds = model.filter(Array(a).map{|x| [x, send(x)]}) 138: ds = yield(ds) if block_given? 139: errors.add(a, message) unless (new? ? ds : ds.exclude(pk_hash)).count == 0 140: end 141: end