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

### Methods

validates_exact_length validates_format validates_includes validates_integer validates_length_range validates_max_length validates_min_length validates_not_string validates_numeric validates_presence validates_unique

### Public Instance methods

validates_exact_length(exact, atts, opts={})

Check that the attribute values are the given exact length.

[Source]

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

validates_format(with, atts, opts={})

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

[Source]

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

validates_includes(set, atts, opts={})

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

[Source]

        # File lib/sequel/plugins/validation_helpers.rb, line 73
    73:         def validates_includes(set, atts, opts={})
    74:           validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.include?(v)}
    75:         end

validates_integer(atts, opts={})

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

[Source]

        # File lib/sequel/plugins/validation_helpers.rb, line 78
    78:         def validates_integer(atts, opts={})
    79:           validatable_attributes_for_type(:integer, atts, opts) do |a,v,m|
    80:             begin
    81:               Kernel.Integer(v.to_s)
    82:               nil
    83:             rescue
    84:               validation_error_message(m)
    85:             end
    86:           end
    87:         end

validates_length_range(range, atts, opts={})

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

[Source]

        # File lib/sequel/plugins/validation_helpers.rb, line 90
    90:         def validates_length_range(range, atts, opts={})
    91:           validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.include?(v.length)}
    92:         end

validates_max_length(max, atts, opts={})

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

[Source]

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

validates_min_length(min, atts, opts={})

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

[Source]

         # File lib/sequel/plugins/validation_helpers.rb, line 100
    100:         def validates_min_length(min, atts, opts={})
    101:           validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) unless v && v.length >= min}
    102:         end

validates_not_string(atts, opts={})

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 110
    110:         def validates_not_string(atts, opts={})
    111:           validatable_attributes_for_type(:not_string, atts, opts){|a,v,m| validation_error_message(m, (db_schema[a]||{})[:type]) if v.is_a?(String)}
    112:         end

validates_numeric(atts, opts={})

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

[Source]

         # File lib/sequel/plugins/validation_helpers.rb, line 115
    115:         def validates_numeric(atts, opts={})
    116:           validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m|
    117:             begin
    118:               Kernel.Float(v.to_s)
    119:               nil
    120:             rescue
    121:               validation_error_message(m)
    122:             end
    123:           end
    124:         end

validates_presence(atts, opts={})

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

[Source]

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

validates_unique(*atts) {|ds| ...}

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’)

[Source]

         # File lib/sequel/plugins/validation_helpers.rb, line 156
    156:         def validates_unique(*atts)
    157:           opts = default_validation_helpers_options(:unique)
    158:           if atts.last.is_a?(Hash)
    159:             opts = opts.merge(atts.pop)
    160:           end
    161:           message = validation_error_message(opts[:message])
    162:           atts.each do |a|
    163:             ds = model.filter(Array(a).map{|x| [x, send(x)]})
    164:             ds = yield(ds) if block_given?
    165:             errors.add(a, message) unless (new? ? ds : ds.exclude(pk_hash)).count == 0
    166:           end
    167:         end

[Validate]