Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable not working when deleting nested property by assigning new value to parent #9281

Closed
DenBo opened this issue Jul 24, 2020 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@DenBo
Copy link

DenBo commented Jul 24, 2020

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When setting schemas to strict:'throw' and deleting a nested property on refetched document by assigning new value to parent, when document is revalidated, no error is thrown

If the current behavior is a bug, please provide the steps to reproduce.

describe('Immutable validator tests', () => {
  let Test = null

  before(() => {
    const SubSchema = new Schema({
      nestedProp: {
        type: String,
        immutable: true
      }
    }, {
      strict: 'throw'
    })

    const TestSchema = new Schema({
      object: {
        type: SubSchema
      }
    }, {
      strict: 'throw'
    })

    mongoose.model('Test', TestSchema)

    Test = mongoose.model('Test')
  })

  it('should produce an error, field in subschema is deleted when updating document', async () => {
    const testDoc = new Test({
      object: {
        nestedProp: 'A'
      }
    })

    await testDoc.save()

    const testDocRefetched = await Test.findById(testDoc._id)

    testDocRefetched.object = {}
    // works:  testDocRefetched.object.nestedProp = undefined

    await testDocRefetched.save().should.be.rejectedWith(
      'Test validation failed: object.nestedProp: Path `nestedProp` is immutable and strict mode is set to throw., object: Validation failed: nestedProp: Path `nestedProp` is immutable and strict mode is set to throw.'
    )
  })
})

What is the expected behavior?

Error should be thrown

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

mongoose: 5.9.20
node: 12.17.0
mongodb: 3.6.17

@vkarpov15 vkarpov15 added this to the 5.9.26 milestone Jul 24, 2020
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jul 24, 2020
vkarpov15 added a commit that referenced this issue Jul 25, 2020
vkarpov15 added a commit that referenced this issue Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants