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

Document Key Validation

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0
    • Affects Version/s: 5.0.2
    • Component/s: None
    • None

      Hash fields don't validate their keys allowing invalid keys into the database and causing an error to be thrown on updates.

      class Example
        include Mongoid::Document
      
        field :test, type: Hash
      end
      
      e = Example.create!(test: { "bad.1" => "bad key" })
      #=> #<Example _id: 56ce62a86572697d57000000, test: {"bad.1"=>"bad key"}>
      e.save!
      #=> true
      e.test.merge!("bad.2" => "bad key")
      e.save
      Mongo::Error::OperationFailure: The dotted field 'bad.1' in 'test.bad.1' is not valid for storage. (57)
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/operation/result.rb:226:in `validate!'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/operation/write/write_command_enabled.rb:48:in `execute_write_command'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/operation/write/write_command_enabled.rb:38:in `execute'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/collection/view/writable.rb:193:in `block in update'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/retryable.rb:84:in `call'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/retryable.rb:84:in `write_with_retry'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/collection/view/writable.rb:184:in `update'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongo-2.1.2/lib/mongo/collection/view/writable.rb:167:in `update_one'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongoid-5.0.2/lib/mongoid/query_cache.rb:168:in `update_one_with_clear_cache'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongoid-5.0.2/lib/mongoid/persistable/updatable.rb:144:in `block in update_document'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mongoid-5.0.2/lib/mongoid/persistable/updatable.rb:118:in `block (2 levels) in prepare_update'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
              from /.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
      ... 4 levels...
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            eric.r.pigeon@gmail.com eric pigeon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: