Implements the Validation meta-language.
- define_validation
- parse_config
- validate_confirmation
- validate_format
- validate_inclusion
- validate_length
- validate_numeric
- validate_numericality
- validate_value
| LENGTHS | = | [:min, :max, :range, :length].freeze |
Validates the length of String
attributes.
Examplevalidate_length :name, :max => 30, :msg => ‘Too long’ validate_length :name, :min => 2, :msg => ‘Too sort’ validate_length :name, :range => 2..30 validate_length :name, :length => 15, :msg => ‘Name should be %d chars long’ |
||
Validates the confirmation of String attributes.
Example
validate_confirmation :password, :msg => ‘No confirmation’
[ show source ]
# File lib/glue/validation.rb, line 215
215: def validate_confirmation(*params)
216: c = parse_config(params,
217: :msg => Glue::Validation::Errors.no_confirmation,
218: :postfix => Glue::Validation.confirmation_postfix,
219: :on => :save
220: )
221:
222: params.each do |field|
223: confirm_name = field.to_s + c[:postfix]
224: attr_accessor confirm_name.to_sym
225:
226: define_validation(:confirmation, field, c[:on]) do |obj|
227: value = obj.send(field)
228: obj.errors.add(field, c[:msg]) if value.nil? or obj.send(confirm_name) != value
229: end
230: end
231: end
Validates the format of String attributes. WARNING: regexp options are ignored.
Example
validate_format :name, :format => /$A*/, :msg => ‘My error’, :on => :create
[ show source ]
# File lib/glue/validation.rb, line 243
243: def validate_format(*params)
244: c = parse_config(params,
245: :format => nil,
246: :msg_no_value => Glue::Validation::Errors.no_value,
247: :msg => Glue::Validation::Errors.invalid_format,
248: :on => :save
249: )
250:
251: unless c[:format].is_a?(Regexp)
252: raise ArgumentError, "A regular expression must be supplied as the :format option for validate_format."
253: end
254:
255: params.each do |field|
256: define_validation(:format, field, c[:on]) do |obj|
257: value = obj.send(field)
258: obj.errors.add(field, c[:msg]) if value.nil? or not value.to_s.match(c[:format].source)
259: end
260: end
261: end
Validates that the attributes are included in an enumeration.
Example
validate_inclusion :sex, :in => %w{ Male Female }, :msg => ‘huh??’ validate_inclusion :age, :in => 5..99
[ show source ]
# File lib/glue/validation.rb, line 338
338: def validate_inclusion(*params)
339: c = parse_config(params,
340: :in => nil,
341: :msg => Glue::Validation::Errors.no_inclusion,
342: :allow_nil => false,
343: :on => :save
344: )
345:
346: unless c[:in].respond_to?('include?')
347: raise(ArgumentError, 'An object that responds to #include? must be supplied as the :in option')
348: end
349:
350: params.each do |field|
351: define_validation(:inclusion, field, c[:on]) do |obj|
352: value = obj.send(field)
353: unless (c[:allow_nil] && value.nil?) or c[:in].include?(value)
354: obj.errors.add(field, c[:msg])
355: end
356: end
357: end
358: end
[ show source ]
# File lib/glue/validation.rb, line 274
274: def validate_length(*params)
275: c = parse_config(params,
276: # :lengths => {:min => nil, :max => nil, :range => nil, :length => nil},
277: :min => nil, :max => nil, :range => nil, :length => nil,
278: :msg => nil,
279: :msg_no_value => Glue::Validation::Errors.no_value,
280: :msg_short => Glue::Validation::Errors.too_short,
281: :msg_long => Glue::Validation::Errors.too_long,
282: :msg_invalid => Glue::Validation::Errors.invalid_length,
283: :on => :save
284: )
285:
286: length_params = c.reject {|k,v| !LENGTHS.include?(k) || v.nil? }
287: valid_count = length_params.reject{|k,v| v.nil?}.length
288:
289: if valid_count == 0
290: raise ArgumentError, 'One of :min, :max, :range, or :length must be provided!'
291: elsif valid_count > 1
292: raise ArgumentError, 'You can only use one of :min, :max, :range, or :length options!'
293: end
294:
295: operation, required = length_params.keys[0], length_params.values[0]
296:
297: params.each do |field|
298: define_validation(:length, field, c[:on]) do |obj|
299: msg = c[:msg]
300: value = obj.send(field)
301: if value.nil?
302: obj.errors.add(field, c[:msg_no_value])
303: else
304: case operation
305: when :min
306: msg ||= c[:msg_short]
307: obj.errors.add(field, msg % required) if value.length < required
308: when :max
309: msg ||= c[:msg_long]
310: obj.errors.add(field, msg % required) if value.length > required
311: when :range
312: min, max = required.first, required.last
313: if value.length < min
314: msg ||= c[:msg_short]
315: obj.errors.add(field, msg % min)
316: end
317: if value.length > max
318: msg ||= c[:msg_long]
319: obj.errors.add(field, msg % min)
320: end
321: when :length
322: msg ||= c[:msg_invalid]
323: obj.errors.add(field, msg % required) if value.length != required
324: end
325: end
326: end
327: end
328: end
Validates that the attributes have numeric values.
- +:integer+
- Only accept integers
[+:msg] Custom message [+:on:] When to validate
Example
validate_numeric :age, :msg => ‘Must be an integer’
[ show source ]
# File lib/glue/validation.rb, line 375
375: def validate_numeric(*params)
376: c = parse_config(params,
377: :integer => false,
378: :msg => Glue::Validation::Errors.no_numeric,
379: :on => :save
380: )
381:
382: params.each do |field|
383: define_validation(:numeric, field, c[:on]) do |obj|
384: value = obj.send(field).to_s
385: if c[:integer]
386: unless value =~ /^[\+\-]*\d+$/
387: obj.errors.add(field, c[:msg])
388: end
389: else
390: begin
391: Float(value)
392: rescue ArgumentError, TypeError
393: obj.errors.add(field, c[:msg])
394: end
395: end
396: end
397: end
398: end
Alias for #validate_numeric
Validates that the attributes have a values, ie they are neither nil or empty.
Example
validate_value :param, :msg => ‘No confirmation’
[ show source ]
# File lib/glue/validation.rb, line 198
198: def validate_value(*params)
199: c = parse_config(params, :msg => Glue::Validation::Errors.no_value, :on => :save)
200:
201: params.each do |field|
202: define_validation(:value, field, c[:on]) do |obj|
203: value = obj.send(field)
204: obj.errors.add(field, c[:msg]) if value.nil? || (value.respond_to?(:empty?) && value.empty?)
205: end
206: end
207: end
Define a validation for this class on the specified event. Specify the on event for when this validation should be checked.
An extra check is performed to avoid multiple validations.
This example creates a validation for the ‘save’ event, and will add an error to the record if the ‘name’ property of the validating object is nil.
Example
field = :name
define_validation(:value, field, :save) do |object|
object.errors.add(field, "You must set a value for #{field}") if object.send(field).nil?
end
[ show source ]
# File lib/glue/validation.rb, line 430
430: def define_validation(val, field, on = :save, &block)
431: vk = Validation::Key.new(val, field)
432: unless validations.find { |v| v[2] == vk }
433: validations! << [on, block, vk]
434: end
435: end
Parse the configuration for a validation by comparing the default options with the user-specified options, and returning the results.
[ show source ]
# File lib/glue/validation.rb, line 407
407: def parse_config(params, defaults = {})
408: defaults.update(params.pop) if params.last.is_a?(Hash)
409: defaults
410: end