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 64 64: def validates_exact_length(exact, atts, opts={}) 65: validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) unless v && v.length == exact} 66: end
Check the string representation of the attribute value(s) against the regular expression with.
# File lib/sequel/plugins/validation_helpers.rb, line 69 69: def validates_format(with, atts, opts={}) 70: validatable_attributes_for_type(:format, atts, opts){|a,v,m| validation_error_message(m, with) unless v.to_s =~ with} 71: end
Check attribute value(s) is included in the given set.
# File lib/sequel/plugins/validation_helpers.rb, line 74 74: def validates_includes(set, atts, opts={}) 75: validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.include?(v)} 76: end
Check attribute value(s) string representation is a valid integer.
# File lib/sequel/plugins/validation_helpers.rb, line 79 79: def validates_integer(atts, opts={}) 80: validatable_attributes_for_type(:integer, atts, opts) do |a,v,m| 81: begin 82: Kernel.Integer(v.to_s) 83: nil 84: rescue 85: validation_error_message(m) 86: end 87: end 88: end
Check that the attribute values length is in the specified range.
# File lib/sequel/plugins/validation_helpers.rb, line 91 91: def validates_length_range(range, atts, opts={}) 92: validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.include?(v.length)} 93: end
Check that the attribute values are not longer than the given max length.
# File lib/sequel/plugins/validation_helpers.rb, line 96 96: def validates_max_length(max, atts, opts={}) 97: validatable_attributes_for_type(:max_length, atts, opts){|a,v,m| validation_error_message(m, max) unless v && v.length <= max} 98: end
Check that the attribute values are not shorter than the given min length.
# File lib/sequel/plugins/validation_helpers.rb, line 101 101: def validates_min_length(min, atts, opts={}) 102: validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) unless v && v.length >= min} 103: 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 111 111: def validates_not_string(atts, opts={}) 112: validatable_attributes_for_type(:not_string, atts, opts){|a,v,m| validation_error_message(m, (db_schema[a]||{})[:type]) if v.is_a?(String)} 113: end
Check attribute value(s) string representation is a valid float.
# File lib/sequel/plugins/validation_helpers.rb, line 116 116: def validates_numeric(atts, opts={}) 117: validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m| 118: begin 119: Kernel.Float(v.to_s) 120: nil 121: rescue 122: validation_error_message(m) 123: end 124: end 125: end
Check attribute value(s) is not considered blank by the database, but allow false values.
# File lib/sequel/plugins/validation_helpers.rb, line 134 134: def validates_presence(atts, opts={}) 135: validatable_attributes_for_type(:presence, atts, opts){|a,v,m| validation_error_message(m) if model.db.send(:blank_object?, v) && v != false} 136: end
Check if value is an instance of a class
# File lib/sequel/plugins/validation_helpers.rb, line 128 128: def validates_type(klass, atts, opts={}) 129: klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol) 130: validatable_attributes_for_type(:type, atts, opts){|a,v,m| validation_error_message(m, klass) if v && !v.is_a?(klass)} 131: 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 165 165: def validates_unique(*atts) 166: opts = default_validation_helpers_options(:unique) 167: if atts.last.is_a?(Hash) 168: opts = opts.merge(atts.pop) 169: end 170: message = validation_error_message(opts[:message]) 171: atts.each do |a| 172: arr = Array(a) 173: next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)} 174: ds = model.filter(arr.map{|x| [x, send(x)]}) 175: ds = yield(ds) if block_given? 176: ds = ds.exclude(pk_hash) unless new? 177: errors.add(a, message) unless ds.count == 0 178: end 179: end