Module | Sequel::Plugins::DeprecatedValidationClassMethods::ClassMethods |
In: |
lib/sequel/model/deprecated_validations.rb
|
Returns true if validations are defined.
# File lib/sequel/model/deprecated_validations.rb, line 21 21: def has_validations? 22: !validations.empty? 23: end
Instructs the model to skip validations defined in superclasses
# File lib/sequel/model/deprecated_validations.rb, line 26 26: def skip_superclass_validations 27: @skip_superclass_validations = true 28: end
Validates the given instance.
# File lib/sequel/model/deprecated_validations.rb, line 51 51: def validate(o) 52: if superclass.respond_to?(:validate) && !@skip_superclass_validations 53: superclass.validate(o) 54: end 55: validations.each do |att, procs| 56: v = case att 57: when Array 58: att.collect{|a| o.send(a)} 59: else 60: o.send(att) 61: end 62: procs.each {|tag, p| p.call(o, att, v)} 63: end 64: end
Defines validations by converting a longhand block into a series of shorthand definitions. For example:
class MyClass < Sequel::Model validates do length_of :name, :minimum => 6 length_of :password, :minimum => 8 end end
is equivalent to:
class MyClass < Sequel::Model validates_length_of :name, :minimum => 6 validates_length_of :password, :minimum => 8 end
# File lib/sequel/model/deprecated_validations.rb, line 45 45: def validates(&block) 46: Deprecation.deprecate('Sequel::Model.validates', 'Use Model.plugin(:validation_class_methods) first') 47: Generator.new(self, &block) 48: end
Validates acceptance of an attribute. Just checks that the value is equal to the :accept option. This method is unique in that :allow_nil is assumed to be true instead of false.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 73 73: def validates_acceptance_of(*atts) 74: Deprecation.deprecate('Sequel::Model.validates_acceptance_of', 'Use Model.plugin(:validation_class_methods) first') 75: opts = { 76: :message => 'is not accepted', 77: :allow_nil => true, 78: :accept => '1', 79: :tag => :acceptance, 80: }.merge!(extract_options!(atts)) 81: atts << opts 82: validates_each(*atts) do |o, a, v| 83: o.errors[a] << opts[:message] unless v == opts[:accept] 84: end 85: end
Validates confirmation of an attribute. Checks that the object has a _confirmation value matching the current value. For example:
validates_confirmation_of :blah
Just makes sure that object.blah = object.blah_confirmation. Often used for passwords or email addresses on web forms.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 97 97: def validates_confirmation_of(*atts) 98: Deprecation.deprecate('Sequel::Model.validates_confirmation_of', 'Use Model.plugin(:validation_class_methods) first') 99: opts = { 100: :message => 'is not confirmed', 101: :tag => :confirmation, 102: }.merge!(extract_options!(atts)) 103: atts << opts 104: validates_each(*atts) do |o, a, v| 105: o.errors[a] << opts[:message] unless v == o.send("#{a}_confirmation""#{a}_confirmation") 106: end 107: end
Adds a validation for each of the given attributes using the supplied block. The block must accept three arguments: instance, attribute and value, e.g.:
validates_each :name, :password do |object, attribute, value| object.errors[attribute] << 'is not nice' unless value.nice? end
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 132 132: def validates_each(*atts, &block) 133: Deprecation.deprecate('Sequel::Model.validates_each', 'Use Model.plugin(:validation_class_methods) first') 134: opts = extract_options!(atts) 135: blk = if (i = opts[:if]) || (am = opts[:allow_missing]) || (an = opts[:allow_nil]) || (ab = opts[:allow_blank]) 136: proc do |o,a,v| 137: next if i && !validation_if_proc(o, i) 138: next if an && Array(v).all?{|x| x.nil?} 139: next if ab && Array(v).all?{|x| x.blank?} 140: next if am && Array(a).all?{|x| !o.values.has_key?(x)} 141: block.call(o,a,v) 142: end 143: else 144: block 145: end 146: tag = opts[:tag] 147: atts.each do |a| 148: a_vals = validations[a] 149: if tag && (old = a_vals.find{|x| x[0] == tag}) 150: old[1] = blk 151: else 152: a_vals << [tag, blk] 153: end 154: end 155: end
Validates the format of an attribute, checking the string representation of the value against the regular expression provided by the :with option.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 163 163: def validates_format_of(*atts) 164: Deprecation.deprecate('Sequel::Model.validates_format_of', 'Use Model.plugin(:validation_class_methods) first') 165: opts = { 166: :message => 'is invalid', 167: :tag => :format, 168: }.merge!(extract_options!(atts)) 169: 170: unless opts[:with].is_a?(Regexp) 171: raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash" 172: end 173: 174: atts << opts 175: validates_each(*atts) do |o, a, v| 176: o.errors[a] << opts[:message] unless v.to_s =~ opts[:with] 177: end 178: end
Validates that an attribute is within a specified range or set of values.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 294 294: def validates_inclusion_of(*atts) 295: Deprecation.deprecate('Sequel::Model.validates_inclusion_of', 'Use Model.plugin(:validation_class_methods) first') 296: opts = extract_options!(atts) 297: unless opts[:in] && opts[:in].respond_to?(:include?) 298: raise ArgumentError, "The :in parameter is required, and respond to include?" 299: end 300: opts[:message] ||= "is not in range or set: #{opts[:in].inspect}" 301: atts << opts 302: validates_each(*atts) do |o, a, v| 303: o.errors[a] << opts[:message] unless opts[:in].include?(v) 304: end 305: end
Validates the length of an attribute.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 192 192: def validates_length_of(*atts) 193: Deprecation.deprecate('Sequel::Model.validates_length_of', 'Use Model.plugin(:validation_class_methods) first') 194: opts = { 195: :too_long => 'is too long', 196: :too_short => 'is too short', 197: :wrong_length => 'is the wrong length' 198: }.merge!(extract_options!(atts)) 199: 200: opts[:tag] ||= ([:length] + [:maximum, :minimum, :is, :within].reject{|x| !opts.include?(x)}).join('-').to_sym 201: atts << opts 202: validates_each(*atts) do |o, a, v| 203: if m = opts[:maximum] 204: o.errors[a] << (opts[:message] || opts[:too_long]) unless v && v.size <= m 205: end 206: if m = opts[:minimum] 207: o.errors[a] << (opts[:message] || opts[:too_short]) unless v && v.size >= m 208: end 209: if i = opts[:is] 210: o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && v.size == i 211: end 212: if w = opts[:within] 213: o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size) 214: end 215: end 216: end
Validates whether an attribute 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.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 227 227: def validates_not_string(*atts) 228: Deprecation.deprecate('Sequel::Model.validates_not_string', 'Use Model.plugin(:validation_class_methods) first') 229: opts = { 230: :tag => :not_string, 231: }.merge!(extract_options!(atts)) 232: atts << opts 233: validates_each(*atts) do |o, a, v| 234: if v.is_a?(String) 235: unless message = opts[:message] 236: message = if sch = o.db_schema[a] and typ = sch[:type] 237: "is not a valid #{typ}" 238: else 239: "is a string" 240: end 241: end 242: o.errors[a] << message 243: end 244: end 245: end
Validates whether an attribute is a number.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 252 252: def validates_numericality_of(*atts) 253: Deprecation.deprecate('Sequel::Model.validates_numericality_of', 'Use Model.plugin(:validation_class_methods) first') 254: opts = { 255: :message => 'is not a number', 256: :tag => :numericality, 257: }.merge!(extract_options!(atts)) 258: atts << opts 259: validates_each(*atts) do |o, a, v| 260: begin 261: if opts[:only_integer] 262: Kernel.Integer(v.to_s) 263: else 264: Kernel.Float(v.to_s) 265: end 266: rescue 267: o.errors[a] << opts[:message] 268: end 269: end 270: end
Validates the presence of an attribute. Requires the value not be blank, with false considered present instead of absent.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 277 277: def validates_presence_of(*atts) 278: Deprecation.deprecate('Sequel::Model.validates_presence_of', 'Use Model.plugin(:validation_class_methods) first') 279: opts = { 280: :message => 'is not present', 281: :tag => :presence, 282: }.merge!(extract_options!(atts)) 283: atts << opts 284: validates_each(*atts) do |o, a, v| 285: o.errors[a] << opts[:message] if v.blank? && v != false 286: end 287: end
Validates only if the fields in the model (specified by atts) are unique in the database. 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_uniqueness_of([:column1, :column2])
validates the grouping of column1 and column2 while
validates_uniqueness_of(:column1, :column2)
validates them separately.
You should also add a unique index in the database, as this suffers from a fairly obvious race condition.
Possible Options:
# File lib/sequel/model/deprecated_validations.rb, line 323 323: def validates_uniqueness_of(*atts) 324: Deprecation.deprecate('Sequel::Model.validates_uniqueness_of', 'Use Model.plugin(:validation_class_methods) first') 325: opts = { 326: :message => 'is already taken', 327: :tag => :uniqueness, 328: }.merge!(extract_options!(atts)) 329: 330: atts << opts 331: validates_each(*atts) do |o, a, v| 332: error_field = a 333: a = Array(a) 334: v = Array(v) 335: ds = o.class.filter(a.zip(v)) 336: num_dups = ds.count 337: allow = if num_dups == 0 338: # No unique value in the database 339: true 340: elsif num_dups > 1 341: # Multiple "unique" values in the database!! 342: # Someone didn't add a unique index 343: false 344: elsif o.new? 345: # New record, but unique value already exists in the database 346: false 347: elsif ds.first === o 348: # Unique value exists in database, but for the same record, so the update won't cause a duplicate record 349: true 350: else 351: false 352: end 353: o.errors[error_field] << opts[:message] unless allow 354: end 355: end
Returns the validations hash for the class.
# File lib/sequel/model/deprecated_validations.rb, line 358 358: def validations 359: @validations ||= Hash.new {|h, k| h[k] = []} 360: end