@@ -170,6 +170,8 @@ def write_attribute(name, value)
170170
171171 if attribute_writable? ( field_name )
172172 _assigning do
173+ # TODO: remove this
174+ # validate_attribute_value(field_name, value)
173175 localized = fields [ field_name ] . try ( :localized? )
174176 attributes_before_type_cast [ name . to_s ] = value
175177 typed_value = typed_value_for ( field_name , value )
@@ -358,6 +360,31 @@ def unalias_attribute(name)
358360
359361 private
360362
363+ # Validates an attribute value as being assignable to the specified field.
364+ #
365+ # For now, only Hash and Array fields are validated, and the value is
366+ # being checked to be of an appropriate type (i.e. either Hash or Array,
367+ # respectively, or nil).
368+ #
369+ # This method takes the name of the field as stored in the document
370+ # in the database, not (necessarily) the Ruby method name used to read/write
371+ # the said field.
372+ #
373+ # @param [ String, Symbol ] field_name The name of the field.
374+ # @param [ Object ] value The value to be validated.
375+ # TODO: remove this
376+ # def validate_attribute_value(field_name, value)
377+ # return if value.nil?
378+ # field = fields[field_name]
379+ # return unless field
380+ # validatable_types = [ Hash, Array ]
381+ # if validatable_types.include?(field.type)
382+ # unless value.is_a?(field.type)
383+ # raise Mongoid::Errors::InvalidValue.new(field.type, value.class)
384+ # end
385+ # end
386+ # end
387+
361388 def lookup_attribute_presence ( name , value )
362389 if localized_fields . has_key? ( name ) && value
363390 value = localized_fields [ name ] . send ( :lookup , value )
0 commit comments