Module: CouchRest::Validation::AutoValidate

Included in:
ClassMethods
Defined in:
lib/couchrest/validation/auto_validate.rb

Instance Method Summary collapse

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.



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
150
151
152
# File 'lib/couchrest/validation/auto_validate.rb', line 90

def auto_generate_validations(property)
  return unless ((property.autovalidation_check != true) && self.auto_validation)
  return if (property.options && (property.options.has_key?(:auto_validation) && !property.options[:auto_validation]) || property.read_only)
  # value is set by the storage system
  opts = {}
  opts[:context] = property.options[:validates] if property.options.has_key?(:validates)

  # presence
  if opts[:allow_nil] == false
    validates_presence_of property.name, options_with_message(opts, property, :presence)
  end

  # length
  if property.type == String
    # XXX: maybe length should always return a Range, with the min defaulting to 1
    # 52 being the max set 
    len = property.options.fetch(:length, property.options.fetch(:size, 52))
    if len.is_a?(Range)
      opts[:within] = len
    else
      opts[:maximum] = len
    end
    validates_length_of property.name, options_with_message(opts, property, :length)
  end

  # format
  if property.options.has_key?(:format)
    opts[:with] = property.options[:format]
    # validates_format property.name, opts
    validates_format property.name, options_with_message(opts, property, :format)
  end

  # uniqueness validator
  if property.options.has_key?(:unique)
    value = property.options[:unique]
    if value.is_a?(Array) || value.is_a?(Symbol)
      # validates_is_unique property.name, :scope => Array(value)
      validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
    elsif value.is_a?(TrueClass)
      # validates_is_unique property.name
      validates_is_unique property.name, options_with_message({}, property, :is_unique)
    end
  end

  # within validator
  if property.options.has_key?(:set)
    validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within)
  end

  # numeric validator
  if "Integer" == property.type
    opts[:integer_only] = true
    validates_numericality_of property.name, options_with_message(opts, property, :is_number)
  elsif Float == property.type
    opts[:precision] = property.precision
    opts[:scale]     = property.scale
    validates_numericality_of property.name, options_with_message(opts, property, :is_number)
  end
  
  # marked the property has checked
  property.autovalidation_check = true
  
end

#options_with_message(base_options, property, validator_name) ⇒ Object

adds message for validator



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/couchrest/validation/auto_validate.rb', line 22

def options_with_message(base_options, property, validator_name)
  options = base_options.clone
  opts = property.options
  options[: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
  options
end