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

ValidationHelpers contains instance method equivalents for most of the previous default validations. The names and APIs have changed, though.

The validates_unique validation has a unique API, but the other validations have the API explained here:

Arguments:

- atts - Single attribute symbol or an array of attribute symbols specifying the attribute(s) to validate.

Options:

- :allow_blank - Whether to skip the validation if the value is blank. You should make sure all objects respond to blank if you use this option, which you can do by requiring ‘sequel/extensions/blank‘
- :allow_missing - Whether to skip the validation if the attribute isn‘t a key in the values hash. This is different from allow_nil, because Sequel only sends the attributes in the values when doing an insert or update. If the attribute is not present, Sequel doesn‘t specify it, so the database will use the table‘s default value. This is different from having an attribute in values with a value of nil, which Sequel will send as NULL. If your database table has a non NULL default, this may be a good option to use. You don‘t want to use allow_nil, because if the attribute is in values but has a value nil, Sequel will attempt to insert a NULL value into the database, instead of using the database‘s default.
- :allow_nil - Whether to skip the validation if the value is nil.
- :message - The message to use

### 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 30
    30:         def validates_exact_length(exact, atts, opts={})
    31:           validatable_attributes(atts, opts){|a,v,m| (m || "is not #{exact} characters") unless v && v.length == exact}
    32:         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 35
    35:         def validates_format(with, atts, opts={})
    36:           validatable_attributes(atts, opts){|a,v,m| (m || 'is invalid') unless v.to_s =~ with}
    37:         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 40
    40:         def validates_includes(set, atts, opts={})
    41:           validatable_attributes(atts, opts){|a,v,m| (m || "is not in range or set: #{set.inspect}") unless set.include?(v)}
    42:         end

validates_integer(atts, opts={})

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

[Source]

        # File lib/sequel/plugins/validation_helpers.rb, line 45
    45:         def validates_integer(atts, opts={})
    46:           validatable_attributes(atts, opts) do |a,v,m|
    47:             begin
    48:               Kernel.Integer(v.to_s)
    49:               nil
    50:             rescue
    51:               m || 'is not a number'
    52:             end
    53:           end
    54:         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 57
    57:         def validates_length_range(range, atts, opts={})
    58:           validatable_attributes(atts, opts){|a,v,m| (m || "is outside the allowed range") unless v && range.include?(v.length)}
    59:         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 62
    62:         def validates_max_length(max, atts, opts={})
    63:           validatable_attributes(atts, opts){|a,v,m| (m || "is longer than #{max} characters") unless v && v.length <= max}
    64:         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 67
    67:         def validates_min_length(min, atts, opts={})
    68:           validatable_attributes(atts, opts){|a,v,m| (m || "is shorter than #{min} characters") unless v && v.length >= min}
    69:         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 77
    77:         def validates_not_string(atts, opts={})
    78:           validatable_attributes(atts, opts) do |a,v,m|
    79:             next unless v.is_a?(String)
    80:             next m if m
    81:             (sch = db_schema[a] and typ = sch[:type]) ?  "is not a valid #{typ}" : "is a string"
    82:           end
    83:         end

validates_numeric(atts, opts={})

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

[Source]

        # File lib/sequel/plugins/validation_helpers.rb, line 86
    86:         def validates_numeric(atts, opts={})
    87:           validatable_attributes(atts, opts) do |a,v,m|
    88:             begin
    89:               Kernel.Float(v.to_s)
    90:               nil
    91:             rescue
    92:               m || 'is not a number'
    93:             end
    94:           end
    95:         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 98
     98:         def validates_presence(atts, opts={})
     99:           validatable_attributes(atts, opts){|a,v,m| (m || "is not present") if model.db.send(:blank_object?, v) && v != false}
    100:         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 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 deals with multiple attributes at once.

Possible Options:

- :message - The message to use (default: ‘is already taken’)

[Source]

         # File lib/sequel/plugins/validation_helpers.rb, line 121
    121:         def validates_unique(*atts)
    122:           message = (atts.pop[:message] if atts.last.is_a?(Hash)) || 'is already taken'
    123:           atts.each do |a|
    124:             ds = model.filter(Array(a).map{|x| [x, send(x)]})
    125:             errors.add(a, message) unless (new? ? ds : ds.exclude(pk_hash)).count == 0
    126:           end
    127:         end

[Validate]