dm-validations 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +9 -1
- data/lib/dm-validations/auto_validate.rb +34 -28
- data/lib/dm-validations/validation_errors.rb +11 -0
- data/lib/dm-validations/version.rb +1 -1
- metadata +13 -4
data/History.rdoc
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
=== 0.10.
|
1
|
+
=== 0.10.1 / 2009-09-30
|
2
|
+
|
3
|
+
* 2 minor enhancements:
|
4
|
+
|
5
|
+
* Always pass the base options into the validation methods so that
|
6
|
+
:allow_nil => true is always in effect in auto-validations
|
7
|
+
* Improved active_model compatibility
|
8
|
+
|
9
|
+
=== 0.10.0 / 2009-09-15
|
2
10
|
|
3
11
|
* 3 major enhancements:
|
4
12
|
|
@@ -89,7 +89,7 @@ module DataMapper
|
|
89
89
|
# validation when the value is nil
|
90
90
|
opts = { :allow_nil => true }
|
91
91
|
|
92
|
-
if property.options.
|
92
|
+
if property.options.key?(:validates)
|
93
93
|
opts[:context] = property.options[:validates]
|
94
94
|
end
|
95
95
|
|
@@ -98,7 +98,7 @@ module DataMapper
|
|
98
98
|
infer_format_validation_for(property, opts.dup)
|
99
99
|
infer_uniqueness_validation_for(property, opts.dup)
|
100
100
|
infer_within_validation_for(property, opts.dup)
|
101
|
-
|
101
|
+
infer_type_validation_for(property, opts.dup)
|
102
102
|
end # auto_generate_validations
|
103
103
|
|
104
104
|
# Checks whether auto validations are currently
|
@@ -121,50 +121,56 @@ module DataMapper
|
|
121
121
|
# @return [TrueClass, FalseClass]
|
122
122
|
# true for properties with :auto_validation option that has positive value
|
123
123
|
def skip_auto_validation_for?(property)
|
124
|
-
property.options.
|
124
|
+
property.options.key?(:auto_validation) && !property.options[:auto_validation]
|
125
125
|
end
|
126
126
|
|
127
127
|
def infer_presence_validation_for(property, options)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
end
|
128
|
+
return if allow_nil?(property)
|
129
|
+
|
130
|
+
validates_present property.name, options_with_message(options, property, :presence)
|
132
131
|
end
|
133
132
|
|
134
133
|
def infer_length_validation_for(property, options)
|
135
|
-
|
136
|
-
case length = property.options.fetch(:length, DataMapper::Property::DEFAULT_LENGTH)
|
137
|
-
when Range then options[:within] = length
|
138
|
-
else options[:maximum] = length
|
139
|
-
end
|
134
|
+
return unless [ String, DataMapper::Types::Text ].include?(property.type)
|
140
135
|
|
141
|
-
|
136
|
+
case length = property.options.fetch(:length, DataMapper::Property::DEFAULT_LENGTH)
|
137
|
+
when Range then options[:within] = length
|
138
|
+
else options[:maximum] = length
|
142
139
|
end
|
140
|
+
|
141
|
+
validates_length property.name, options_with_message(options, property, :length)
|
143
142
|
end
|
144
143
|
|
145
144
|
def infer_format_validation_for(property, options)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
145
|
+
return unless property.options.key?(:format)
|
146
|
+
|
147
|
+
options[:with] = property.options[:format]
|
148
|
+
|
149
|
+
validates_format property.name, options_with_message(options, property, :format)
|
150
150
|
end
|
151
151
|
|
152
152
|
def infer_uniqueness_validation_for(property, options)
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
153
|
+
return unless property.options.key?(:unique)
|
154
|
+
|
155
|
+
case value = property.options[:unique]
|
156
|
+
when Array, Symbol
|
157
|
+
options[:scope] = Array(value)
|
158
|
+
|
159
|
+
validates_is_unique property.name, options_with_message(options, property, :is_unique)
|
160
|
+
when TrueClass
|
161
|
+
validates_is_unique property.name, options_with_message(options, property, :is_unique)
|
160
162
|
end
|
161
163
|
end
|
162
164
|
|
163
165
|
def infer_within_validation_for(property, options)
|
164
|
-
|
166
|
+
return unless property.options.key?(:set)
|
167
|
+
|
168
|
+
options[:set] = property.options[:set]
|
169
|
+
|
170
|
+
validates_within property.name, options_with_message(options, property, :within)
|
165
171
|
end
|
166
172
|
|
167
|
-
def
|
173
|
+
def infer_type_validation_for(property, options)
|
168
174
|
options[:gte] = property.min if property.min
|
169
175
|
options[:lte] = property.max if property.max
|
170
176
|
|
@@ -177,11 +183,11 @@ module DataMapper
|
|
177
183
|
options[:scale] = property.scale
|
178
184
|
|
179
185
|
validates_is_number property.name, options_with_message(options, property, :is_number)
|
180
|
-
|
186
|
+
elsif !property.custom?
|
181
187
|
# We only need this in the case we don't already
|
182
188
|
# have a numeric validator, because otherwise
|
183
189
|
# it will cause duplicate validation errors
|
184
|
-
validates_is_primitive property.name, options_with_message(options, property, :is_primitive)
|
190
|
+
validates_is_primitive property.name, options_with_message(options, property, :is_primitive)
|
185
191
|
end
|
186
192
|
end
|
187
193
|
|
@@ -84,6 +84,17 @@ module DataMapper
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
+
# Return validation errors for a particular field name or an empty array
|
88
|
+
#
|
89
|
+
# This method is a necessary requirement for active_model compatibility.
|
90
|
+
#
|
91
|
+
# @param [Symbol] field_name the name of the field you want an error for
|
92
|
+
# @return [Array<Array<String>>]
|
93
|
+
# array of validation errors or empty array, if there are no errors on given field
|
94
|
+
def [](field_name)
|
95
|
+
errors[field_name] ||= []
|
96
|
+
end
|
97
|
+
|
87
98
|
# Return validation errors for a particular field_name.
|
88
99
|
#
|
89
100
|
# @param [Symbol] field_name the name of the field you want an error for
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.
|
4
|
+
version: 0.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guy van den Berg, DataMapper development team
|
@@ -9,10 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.10.1
|
24
|
+
version:
|
16
25
|
description: Library for performing validations on DM models and pure Ruby object
|
17
26
|
email:
|
18
27
|
- vandenberg.guy [a] gmail [d] com
|