Module: Sequel::Plugins::ValidationHelpers::InstanceMethods
- Defined in:
- 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_null(atts, opts = {}) ⇒ Object
Check attribute value(s) are not NULL/nil.
-
#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_schema_types(atts = keys, opts = {}) ⇒ Object
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.
-
#validates_type(klass, atts, opts = {}) ⇒ Object
Check if value is an instance of a class.
-
#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.
96 97 98 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 96 def validates_exact_length(exact, atts, opts={}) validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| (m, exact) if v.nil? || v.length != exact} end |
#validates_format(with, atts, opts = {}) ⇒ Object
Check the string representation of the attribute value(s) against the regular expression with.
101 102 103 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 101 def validates_format(with, atts, opts={}) validatable_attributes_for_type(:format, atts, opts){|a,v,m| (m, with) unless v.to_s =~ with} end |
#validates_includes(set, atts, opts = {}) ⇒ Object
Check attribute value(s) is included in the given set.
106 107 108 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 106 def validates_includes(set, atts, opts={}) validatable_attributes_for_type(:includes, atts, opts){|a,v,m| (m, set) unless set.send(set.respond_to?(:cover?) ? :cover? : :include?, v)} end |
#validates_integer(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid integer.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 111 def validates_integer(atts, opts={}) validatable_attributes_for_type(:integer, atts, opts) do |a,v,m| begin Kernel.Integer(v.to_s) nil rescue (m) end end end |
#validates_length_range(range, atts, opts = {}) ⇒ Object
Check that the attribute values length is in the specified range.
123 124 125 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 123 def validates_length_range(range, atts, opts={}) validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| (m, range) if v.nil? || !range.send(range.respond_to?(:cover?) ? :cover? : :include?, v.length)} end |
#validates_max_length(max, atts, opts = {}) ⇒ Object
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.
131 132 133 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 131 def validates_max_length(max, atts, opts={}) validatable_attributes_for_type(:max_length, atts, opts){|a,v,m| v ? (m, max) : (opts[:nil_message] || DEFAULT_OPTIONS[:max_length][:nil_message]) if v.nil? || v.length > max} end |
#validates_min_length(min, atts, opts = {}) ⇒ Object
Check that the attribute values are not shorter than the given min length.
136 137 138 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 136 def validates_min_length(min, atts, opts={}) validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| (m, min) if v.nil? || v.length < min} end |
#validates_not_null(atts, opts = {}) ⇒ Object
Check attribute value(s) are not NULL/nil.
141 142 143 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 141 def validates_not_null(atts, opts={}) validatable_attributes_for_type(:not_null, atts, opts){|a,v,m| (m) if v.nil?} 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.
151 152 153 154 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 151 def validates_not_string(atts, opts={}) Sequel::Deprecation.deprecate('validates_not_string', "Please switch to validates_schema_types") validatable_attributes_for_type(:not_string, atts, opts){|a,v,m| (m, (db_schema[a]||{})[:type]) if v.is_a?(String)} end |
#validates_numeric(atts, opts = {}) ⇒ Object
Check attribute value(s) string representation is a valid float.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 157 def validates_numeric(atts, opts={}) validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m| begin Kernel.Float(v.to_s) nil rescue (m) end end end |
#validates_presence(atts, opts = {}) ⇒ Object
Check attribute value(s) is not considered blank by the database, but allow false values.
195 196 197 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 195 def validates_presence(atts, opts={}) validatable_attributes_for_type(:presence, atts, opts){|a,v,m| (m) if model.db.send(:blank_object?, v) && v != false} end |
#validates_schema_types(atts = keys, opts = {}) ⇒ Object
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.
171 172 173 174 175 176 177 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 171 def validates_schema_types(atts=keys, opts={}) Array(atts).each do |k| if type = schema_type_class(k) validates_type(type, k, {:allow_nil=>true}.merge(opts)) end end end |
#validates_type(klass, atts, opts = {}) ⇒ Object
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.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 181 def validates_type(klass, atts, opts={}) klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol) validatable_attributes_for_type(:type, atts, opts) do |a,v,m| if v.nil? Sequel::Deprecation.deprecate('validates_type will no longer allow nil values by default in Sequel 4. Use the :allow_nil=>true option to allow nil values.') next end if klass.is_a?(Array) ? !klass.any?{|kls| v.is_a?(kls)} : !v.is_a?(klass) (m, klass) end end 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’)
- :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:
: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)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/sequel/plugins/validation_helpers.rb', line 241 def validates_unique(*atts) opts = (:unique) if atts.last.is_a?(Hash) opts = opts.merge(atts.pop) end = (opts[:message]) where = opts[:where] atts.each do |a| arr = Array(a) next if arr.any?{|x| errors.on(x)} next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)} ds = if where where.call(model.dataset, self, arr) else vals = arr.map{|x| send(x)} next if vals.any?{|v| v.nil?} model.where(arr.zip(vals)) end ds = yield(ds) if block_given? ds = ds.exclude(pk_hash) unless new? errors.add(a, ) unless ds.count == 0 end end |