def auto_generate_validations(property)
property.options[:auto_validation] = true unless property.options.has_key?(:auto_validation)
return unless property.options[:auto_validation]
opts = { :allow_nil => property.nullable? || property.serial? }
opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
unless opts[:allow_nil]
validates_present property.name, options_with_message(opts, property, :presence)
end
if property.type == String
len = property.options.fetch(:length, property.options.fetch(:size, DataMapper::Property::DEFAULT_LENGTH))
if len.is_a?(Range)
opts[:within] = len
else
opts[:maximum] = len
end
validates_length property.name, options_with_message(opts, property, :length)
end
if property.options.has_key?(:format)
opts[:with] = property.options[:format]
validates_format property.name, options_with_message(opts, property, :format)
end
if property.options.has_key?(:unique)
value = property.options[:unique]
if value.is_a?(Array) || value.is_a?(Symbol)
validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
elsif value.is_a?(TrueClass)
validates_is_unique property.name, options_with_message({}, property, :is_unique)
end
end
if property.options.has_key?(:set)
validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within)
end
if Integer == property.type
opts[:integer_only] = true
validates_is_number property.name, options_with_message(opts, property, :is_number)
elsif BigDecimal == property.type || Float == property.type
opts[:precision] = property.precision
opts[:scale] = property.scale
validates_is_number property.name, options_with_message(opts, property, :is_number)
else
unless property.custom?
validates_is_primitive property.name, options_with_message(opts, property, :is_primitive)
end
end
end