module Sequel::Plugins::ValidationHelpers::InstanceMethods

  1. lib/sequel/plugins/validation_helpers.rb

Public Instance methods

validates_exact_length(exact, atts, opts=OPTS)

Check that the attribute values are the given exact length.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
105 def validates_exact_length(exact, atts, opts=OPTS)
106   validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) if v.nil? || v.length != exact}
107 end
validates_format(with, atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
110 def validates_format(with, atts, opts=OPTS)
111   validatable_attributes_for_type(:format, atts, opts){|a,v,m| validation_error_message(m, with) unless v.to_s =~ with}
112 end
validates_includes(set, atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
115 def validates_includes(set, atts, opts=OPTS)
116   validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.public_send(set.respond_to?(:cover?) ? :cover? : :include?, v)}
117 end
validates_integer(atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
120 def validates_integer(atts, opts=OPTS)
121   validatable_attributes_for_type(:integer, atts, opts) do |a,v,m|
122     begin
123       Kernel.Integer(v.to_s)
124       nil
125     rescue
126       validation_error_message(m)
127     end
128   end
129 end
validates_length_range(range, atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
132 def validates_length_range(range, atts, opts=OPTS)
133   validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) if v.nil? || !range.cover?(v.length)}
134 end
validates_max_length(max, atts, opts=OPTS)

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.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
140 def validates_max_length(max, atts, opts=OPTS)
141   validatable_attributes_for_type(:max_length, atts, opts) do |a,v,m|
142     if v.nil?
143       validation_error_message(opts[:nil_message] || default_validation_helpers_options(:max_length)[:nil_message])
144     elsif v.length > max
145       validation_error_message(m, max)
146     end
147   end
148 end
validates_max_value(max, atts, opts=OPTS)

Check that the attribute values are not greater that the given maximum value. Does not perform validation if attribute value is nil. You should only call this if you have checked the attribute value has the expected type.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
153 def validates_max_value(max, atts, opts=OPTS)
154   validatable_attributes_for_type(:max_value, atts, opts) do |a,v,m|
155     validation_error_message(m, max) if !v.nil? && v > max
156   end
157 end
validates_min_length(min, atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
160 def validates_min_length(min, atts, opts=OPTS)
161   validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) if v.nil? || v.length < min}
162 end
validates_min_value(min, atts, opts=OPTS)

Check that the attribute values are not less that the given minimum value. Does not perform validation if attribute value is nil. You should only call this if you have checked the attribute value has the expected type.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
167 def validates_min_value(min, atts, opts=OPTS)
168   validatable_attributes_for_type(:min_value, atts, opts) do |a,v,m|
169     validation_error_message(m, min) if !v.nil? && v < min
170   end
171 end
validates_no_null_byte(atts, opts=OPTS)

Check attribute value(s) does not contain a null (“0”, ASCII NUL) byte.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
179 def validates_no_null_byte(atts, opts=OPTS)
180   validatable_attributes_for_type(:no_null_byte, atts, opts){|a,v,m| validation_error_message(m) if String === v && v.include?("\0")}
181 end
validates_not_null(atts, opts=OPTS)

Check attribute value(s) are not NULL/nil.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
174 def validates_not_null(atts, opts=OPTS)
175   validatable_attributes_for_type(:not_null, atts, opts){|a,v,m| validation_error_message(m) if v.nil?}
176 end
validates_numeric(atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
184 def validates_numeric(atts, opts=OPTS)
185   validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m|
186     begin
187       Kernel.Float(v.to_s)
188       nil
189     rescue
190       validation_error_message(m)
191     end
192   end
193 end
validates_operator(operator, rhs, atts, opts=OPTS)

Check attribute value(s) against a specified value and operation, e.g. validates_operator(:>, 3, :value) validates that value > 3.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
197 def validates_operator(operator, rhs, atts, opts=OPTS)
198   validatable_attributes_for_type(:operator, atts, opts){|a,v,m| validation_error_message(m, operator, rhs) if v.nil? || !v.public_send(operator, rhs)}
199 end
validates_presence(atts, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
224 def validates_presence(atts, opts=OPTS)
225   validatable_attributes_for_type(:presence, atts, opts){|a,v,m| validation_error_message(m) if model.db.send(:blank_object?, v) && v != false}
226 end
validates_schema_types(atts=keys, opts=OPTS)

Validates for all of the model columns (or just the given columns) that the column value is an instance of the expected class based on the column’s schema type.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
204 def validates_schema_types(atts=keys, opts=OPTS)
205   Array(atts).each do |k|
206     if type = schema_type_class(k)
207       validates_type(type, k, {:allow_nil=>true}.merge!(opts))
208     end
209   end
210 end
validates_type(klass, atts, opts=OPTS)

Check if value is an instance of a class. If klass is an array, the value must be an instance of one of the classes in the array.

[show source]
    # File lib/sequel/plugins/validation_helpers.rb
214 def validates_type(klass, atts, opts=OPTS)
215   klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol)
216   validatable_attributes_for_type(:type, atts, opts) do |a,v,m|
217     if klass.is_a?(Array) ? !klass.any?{|kls| v.is_a?(kls)} : !v.is_a?(klass)
218       validates_type_error_message(m, klass)
219     end
220   end
221 end
validates_unique(*atts)

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.where(: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:

:dataset

The base dataset to use for the unique query, defaults to the model’s dataset.

: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, true by default.

:where

A callable object where call takes three arguments, a dataset, the current object, and an array of columns, and should return a modified dataset that is filtered to include only rows with the same values as the current object for each column in the array.

If you want to do a case insensitive uniqueness validation on a database that is case sensitive by default, you can use:

validates_unique :column, where:(lambda do |ds, obj, cols|
  ds.where(cols.map do |c|
    v = obj.public_send(c)
    v = v.downcase if v
    [Sequel.function(:lower, c), v]
  end)
end)
[show source]
    # File lib/sequel/plugins/validation_helpers.rb
272 def validates_unique(*atts)
273   opts = default_validation_helpers_options(:unique)
274   if atts.last.is_a?(Hash)
275     opts = opts.merge(atts.pop)
276   end
277   message = validation_error_message(opts[:message])
278   from_values = opts[:from] == :values
279   where = opts[:where]
280   atts.each do |a|
281     arr = Array(a)
282     next if arr.any?{|x| errors.on(x)}
283     cc = changed_columns
284     next if opts.fetch(:only_if_modified, true) && !new? && !arr.any?{|x| cc.include?(x)}
285     ds = opts[:dataset] || model.dataset
286     ds = if where
287       where.call(ds, self, arr)
288     else
289       vals = arr.map{|x| from_values ? values[x] : get_column_value(x)}
290       next if vals.any?(&:nil?)
291       ds.where(arr.zip(vals))
292     end
293     ds = yield(ds) if defined?(yield)
294     unless new?
295       h = ds.joined_dataset? ? qualified_pk_hash : pk_hash
296       ds = ds.exclude(h)
297     end
298     errors.add(a, message) unless ds.empty?
299   end
300 end