Module Sequel::Plugins::ValidationHelpers::InstanceMethods
In: lib/sequel/plugins/validation_helpers.rb

Methods

Public Instance methods

Check that the attribute values are the given exact length.

[Source]

    # File lib/sequel/plugins/validation_helpers.rb, line 95
95:         def validates_exact_length(exact, atts, opts={})
96:           validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) unless v && v.length == exact}
97:         end

Check the string representation of the attribute value(s) against the regular expression with.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 100
100:         def validates_format(with, atts, opts={})
101:           validatable_attributes_for_type(:format, atts, opts){|a,v,m| validation_error_message(m, with) unless v.to_s =~ with}
102:         end

Check attribute value(s) is included in the given set.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 105
105:         def validates_includes(set, atts, opts={})
106:           validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.send(set.respond_to?(:cover?) ? :cover? : :include?, v)}
107:         end

Check attribute value(s) string representation is a valid integer.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 110
110:         def validates_integer(atts, opts={})
111:           validatable_attributes_for_type(:integer, atts, opts) do |a,v,m|
112:             begin
113:               Kernel.Integer(v.to_s)
114:               nil
115:             rescue
116:               validation_error_message(m)
117:             end
118:           end
119:         end

Check that the attribute values length is in the specified range.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 122
122:         def validates_length_range(range, atts, opts={})
123:           validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.send(range.respond_to?(:cover?) ? :cover? : :include?, v.length)}
124:         end

Check that the attribute values are not longer than the given max length.

Accepts a :nil_message option that is the error message to use when the value is nil instead of being too long.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 130
130:         def validates_max_length(max, atts, opts={})
131:           validatable_attributes_for_type(:max_length, atts, opts){|a,v,m| v ? validation_error_message(m, max) : validation_error_message(opts[:nil_message] || DEFAULT_OPTIONS[:max_length][:nil_message]) unless v && v.length <= max}
132:         end

Check that the attribute values are not shorter than the given min length.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 135
135:         def validates_min_length(min, atts, opts={})
136:           validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) unless v && v.length >= min}
137:         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.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 145
145:         def validates_not_string(atts, opts={})
146:           validatable_attributes_for_type(:not_string, atts, opts){|a,v,m| validation_error_message(m, (db_schema[a]||{})[:type]) if v.is_a?(String)}
147:         end

Check attribute value(s) string representation is a valid float.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 150
150:         def validates_numeric(atts, opts={})
151:           validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m|
152:             begin
153:               Kernel.Float(v.to_s)
154:               nil
155:             rescue
156:               validation_error_message(m)
157:             end
158:           end
159:         end

Check attribute value(s) is not considered blank by the database, but allow false values.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 168
168:         def validates_presence(atts, opts={})
169:           validatable_attributes_for_type(:presence, atts, opts){|a,v,m| validation_error_message(m) if model.db.send(:blank_object?, v) && v != false}
170:         end

Check if value is an instance of a class

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 162
162:         def validates_type(klass, atts, opts={})
163:           klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol)
164:           validatable_attributes_for_type(:type, atts, opts){|a,v,m| validation_error_message(m, klass) if v && !v.is_a?(klass)}
165:         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:

  • :message - The message to use (default: ‘is already taken’)
  • :only_if_modified - Only check the uniqueness if the object is new or one of the columns has been modified.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 199
199:         def validates_unique(*atts)
200:           opts = default_validation_helpers_options(:unique)
201:           if atts.last.is_a?(Hash)
202:             opts = opts.merge(atts.pop)
203:           end
204:           message = validation_error_message(opts[:message])
205:           atts.each do |a|
206:             arr = Array(a)
207:             next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)}
208:             ds = model.filter(arr.map{|x| [x, send(x)]})
209:             ds = yield(ds) if block_given?
210:             ds = ds.exclude(pk_hash) unless new?
211:             errors.add(a, message) unless ds.count == 0
212:           end
213:         end

[Validate]