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 92
92:         def validates_exact_length(exact, atts, opts=OPTS)
93:           validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) if v.nil? || v.length != exact}
94:         end

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

[Source]

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

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

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 102
102:         def validates_includes(set, atts, opts=OPTS)
103:           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)}
104:         end

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

[Source]

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

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

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 119
119:         def validates_length_range(range, atts, opts=OPTS)
120:           validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) if v.nil? || !range.send(range.respond_to?(:cover?) ? :cover? : :include?, v.length)}
121:         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 127
127:         def validates_max_length(max, atts, opts=OPTS)
128:           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]) if v.nil? || v.length > max}
129:         end

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

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 132
132:         def validates_min_length(min, atts, opts=OPTS)
133:           validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) if v.nil? || v.length < min}
134:         end

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

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 137
137:         def validates_not_null(atts, opts=OPTS)
138:           validatable_attributes_for_type(:not_null, atts, opts){|a,v,m| validation_error_message(m) if v.nil?}
139:         end

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

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 142
142:         def validates_numeric(atts, opts=OPTS)
143:           validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m|
144:             begin
145:               Kernel.Float(v.to_s)
146:               nil
147:             rescue
148:               validation_error_message(m)
149:             end
150:           end
151:         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 176
176:         def validates_presence(atts, opts=OPTS)
177:           validatable_attributes_for_type(:presence, atts, opts){|a,v,m| validation_error_message(m) if model.db.send(:blank_object?, v) && v != false}
178:         end

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.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 156
156:         def validates_schema_types(atts=keys, opts=OPTS)
157:           Array(atts).each do |k|
158:             if type = schema_type_class(k)
159:               validates_type(type, k, {:allow_nil=>true}.merge(opts))
160:             end
161:           end
162:         end

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.

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 166
166:         def validates_type(klass, atts, opts=OPTS)
167:           klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol)
168:           validatable_attributes_for_type(:type, atts, opts) do |a,v,m|
169:             if klass.is_a?(Array) ? !klass.any?{|kls| v.is_a?(kls)} : !v.is_a?(klass)
170:               validation_error_message(m, klass)
171:             end
172:           end
173:         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:

: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.
: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 to a case insensitive uniqueness validation on a database that is case sensitive by default, you can use:

  validates_unique :column, :where=>(proc do |ds, obj, cols|
    ds.where(cols.map do |c|
      v = obj.send(c)
      v = v.downcase if v
      [Sequel.function(:lower, c), v]
    end)
  end)

[Source]

     # File lib/sequel/plugins/validation_helpers.rb, line 224
224:         def validates_unique(*atts)
225:           opts = default_validation_helpers_options(:unique)
226:           if atts.last.is_a?(Hash)
227:             opts = opts.merge(atts.pop)
228:           end
229:           message = validation_error_message(opts[:message])
230:           where = opts[:where]
231:           atts.each do |a|
232:             arr = Array(a)
233:             next if arr.any?{|x| errors.on(x)}
234:             next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)}
235:             ds = opts[:dataset] || model.dataset
236:             ds = if where
237:               where.call(ds, self, arr)
238:             else
239:               vals = arr.map{|x| get_column_value(x)}
240:               next if vals.any?{|v| v.nil?}
241:               ds.where(arr.zip(vals))
242:             end
243:             ds = yield(ds) if block_given?
244:             unless new?
245:               h = ds.joined_dataset? ? qualified_pk_hash : pk_hash
246:               ds = ds.exclude(h)
247:             end
248:             errors.add(a, message) unless ds.count == 0
249:           end
250:         end

[Validate]