Module: DataMapper::Validate::AutoValidate
- Included in:
- ClassMethods
- Defined in:
- lib/dm-validations/auto_validate.rb
Instance Method Summary collapse
-
#auto_generate_validations(property) ⇒ Object
Auto-generate validations for a given property.
-
#options_with_message(base_options, property, validator_name) ⇒ Object
adds message for validator.
Instance Method Details
#auto_generate_validations(property) ⇒ Object
Auto-generate validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dm-validations/auto_validate.rb', line 78 def auto_generate_validations(property) property.[:auto_validation] = true unless property..has_key?(:auto_validation) return unless property.[:auto_validation] # a serial property is allowed to be nil too, because the # value is set by the storage system opts = { :allow_nil => property.nullable? || property.serial? } opts[:context] = property.[:validates] if property..has_key?(:validates) # presence unless opts[:allow_nil] # validates_present property.name, opts validates_present property.name, (opts, property, :presence) end # length if property.type == String #len = property.length # XXX: maybe length should always return a Range, with the min defaulting to 1 len = property..fetch(:length, property..fetch(:size, DataMapper::Property::DEFAULT_LENGTH)) if len.is_a?(Range) opts[:within] = len else opts[:maximum] = len end # validates_length property.name, opts validates_length property.name, (opts, property, :length) end # format if property..has_key?(:format) opts[:with] = property.[:format] # validates_format property.name, opts validates_format property.name, (opts, property, :format) end # uniqueness validator if property..has_key?(:unique) value = property.[:unique] if value.is_a?(Array) || value.is_a?(Symbol) # validates_is_unique property.name, :scope => Array(value) validates_is_unique property.name, ({:scope => Array(value)}, property, :is_unique) elsif value.is_a?(TrueClass) # validates_is_unique property.name validates_is_unique property.name, ({}, property, :is_unique) end end # within validator if property..has_key?(:set) validates_within property.name, ({:set => property.[:set]}, property, :within) end # numeric validator if Integer == property.type opts[:integer_only] = true # validates_is_number property.name, opts validates_is_number property.name, (opts, property, :is_number) elsif BigDecimal == property.type || Float == property.type opts[:precision] = property.precision opts[:scale] = property.scale # validates_is_number property.name, opts validates_is_number property.name, (opts, property, :is_number) else # We only need this in the case we don't already # have a numeric validator, because otherwise # it will cause duplicate validation errors unless property.custom? # validates_is_primitive property.name, opts validates_is_primitive property.name, (opts, property, :is_primitive) end end end |
#options_with_message(base_options, property, validator_name) ⇒ Object
adds message for validator
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dm-validations/auto_validate.rb', line 10 def (, property, validator_name) = .clone opts = property. [:message] = if opts[:messages] if opts[:messages].is_a?(Hash) and msg = opts[:messages][validator_name] msg else nil end elsif opts[:message] opts[:message] else nil end end |