Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-431

Wrong merge used when validating nested objects

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      In Mongoid::Validations::AssociatedValidator on line 28 there should be merge instead of merge!. Since the options hash is frozen merge! won't work. Action Record (which is very similar) is using merge.

      Below is a modified copy with the correct code. Don't have git so I can't create a pull request.

      1. encoding: utf-8
        module Mongoid #:nodoc:
        module Validations #:nodoc:
      2. Validates whether or not an association is valid or not. Will correctly
      3. handle has one and has many associations.
        #
      4. Example:
        #
      5. class Person
      6. include Mongoid::Document
      7. embeds_one :name
      8. embeds_many :addresses
        #
      9. validates_associated :name, :addresses
      10. end
        class AssociatedValidator < ActiveModel::EachValidator
      1. Validates that the associations provided are either all nil or all
      2. valid. If neither is true then the appropriate errors will be added to
      3. the parent document.
        #
      4. Example:
        #
      5. <tt>validator.validate_each(document, :name, name)</tt>
        def validate_each(document, attribute, value)
        values = value.is_a?(Array) ? value : [ value ]
        return if values.collect { |doc| doc.nil? || doc.valid? }

        .all?
        document.errors.add(attribute, :invalid, options.merge(:value => value))
        end
        end
        end
        end

            Assignee:
            Unassigned Unassigned
            Reporter:
            sandstrom sandstrom
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: