Module: Sequel::Plugins::ValidationHelpers::InstanceMethods
- Defined in:
- lib/sequel/lib/sequel/plugins/validation_helpers.rb
Instance Method Summary collapse
-
#validates_exact_length(exact, atts, opts = {}) ⇒ Object
Check that the attribute values are the given exact length.
-
#validates_format(with, atts, opts = {}) ⇒ Object
Check the string representation of the attribute value(s) against the regular expression with.
-
#validates_includes(set, atts, opts = {}) ⇒ Object
Check attribute value(s) is included in the given set.
-
#validates_integer(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid integer.
-
#validates_length_range(range, atts, opts = {}) ⇒ Object
Check that the attribute values length is in the specified range.
-
#validates_max_length(max, atts, opts = {}) ⇒ Object
Check that the attribute values are not longer than the given max length.
-
#validates_min_length(min, atts, opts = {}) ⇒ Object
Check that the attribute values are not shorter than the given min length.
-
#validates_not_string(atts, opts = {}) ⇒ Object
Check that the attribute value(s) is not a string.
-
#validates_numeric(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid float.
-
#validates_presence(atts, opts = {}) ⇒ Object
Check attribute value(s) is not considered blank by the database, but allow false values.
-
#validates_unique(*atts) ⇒ Object
Checks that there are no duplicate values in the database for the given attributes.
Instance Method Details
#validates_exact_length(exact, atts, opts = {}) ⇒ Object
Check that the attribute values are the given exact length.
37 38 39 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 37 def validates_exact_length(exact, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is not #{exact} characters") unless v && v.length == exact} end |
#validates_format(with, atts, opts = {}) ⇒ Object
Check the string representation of the attribute value(s) against the regular expression with.
42 43 44 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 42 def validates_format(with, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || 'is invalid') unless v.to_s =~ with} end |
#validates_includes(set, atts, opts = {}) ⇒ Object
Check attribute value(s) is included in the given set.
47 48 49 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 47 def validates_includes(set, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is not in range or set: #{set.inspect}") unless set.include?(v)} end |
#validates_integer(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid integer.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 52 def validates_integer(atts, opts={}) validatable_attributes(atts, opts) do |a,v,m| begin Kernel.Integer(v.to_s) nil rescue m || 'is not a number' end end end |
#validates_length_range(range, atts, opts = {}) ⇒ Object
Check that the attribute values length is in the specified range.
64 65 66 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 64 def validates_length_range(range, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is outside the allowed range") unless v && range.include?(v.length)} end |
#validates_max_length(max, atts, opts = {}) ⇒ Object
Check that the attribute values are not longer than the given max length.
69 70 71 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 69 def validates_max_length(max, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is longer than #{max} characters") unless v && v.length <= max} end |
#validates_min_length(min, atts, opts = {}) ⇒ Object
Check that the attribute values are not shorter than the given min length.
74 75 76 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 74 def validates_min_length(min, atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is shorter than #{min} characters") unless v && v.length >= min} end |
#validates_not_string(atts, opts = {}) ⇒ Object
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.
84 85 86 87 88 89 90 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 84 def validates_not_string(atts, opts={}) validatable_attributes(atts, opts) do |a,v,m| next unless v.is_a?(String) next m if m (sch = db_schema[a] and typ = sch[:type]) ? "is not a valid #{typ}" : "is a string" end end |
#validates_numeric(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid float.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 93 def validates_numeric(atts, opts={}) validatable_attributes(atts, opts) do |a,v,m| begin Kernel.Float(v.to_s) nil rescue m || 'is not a number' end end end |
#validates_presence(atts, opts = {}) ⇒ Object
Check attribute value(s) is not considered blank by the database, but allow false values.
105 106 107 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 105 def validates_presence(atts, opts={}) validatable_attributes(atts, opts){|a,v,m| (m || "is not present") if model.db.send(:blank_object?, v) && v != false} end |
#validates_unique(*atts) ⇒ Object
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')
134 135 136 137 138 139 140 141 |
# File 'lib/sequel/lib/sequel/plugins/validation_helpers.rb', line 134 def validates_unique(*atts) = (atts.pop[:message] if atts.last.is_a?(Hash)) || 'is already taken' atts.each do |a| ds = model.filter(Array(a).map{|x| [x, send(x)]}) ds = yield(ds) if block_given? errors.add(a, ) unless (new? ? ds : ds.exclude(pk_hash)).count == 0 end end |