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

'strict: throw' does not work for inner _id properties #7480

Closed
sebastian-nowak opened this issue Feb 2, 2019 · 1 comment
Closed

'strict: throw' does not work for inner _id properties #7480

sebastian-nowak opened this issue Feb 2, 2019 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@sebastian-nowak
Copy link

Even though strict is set to throw for all schemas, it does not throw for inner _id properties. The following code fails on 5.4.9:

#!/usr/bin/env node
(async function () {
  const mongoose = require('mongoose');

  mongoose.plugin((schema) => {
    schema.options.versionKey = false;
    schema.options.minimize = false;
    schema.options.strict = 'throw';
    schema.options.strictQuery = 'throw';
  });

  mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});

  const schema = new mongoose.Schema({
    inner: {
      type: new mongoose.Schema({
        _id: false, // _id is disabled here
        something: {
          type: Boolean,
          required: true
        }
      }),
      require: true
    }
  });
  const Model = mongoose.model('test', schema);

  const instance = new Model({
    inner: {
      _id: mongoose.Types.ObjectId(), // property not allowed but it will validate
      something: true
    }
  });
  await instance.save(); // should fail but does not

  console.log('This message should not appear (but it will)');
})();
@vkarpov15 vkarpov15 added this to the 5.4.11 milestone Feb 5, 2019
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 5, 2019
@vkarpov15
Copy link
Collaborator

As a workaround, do this:

  const schema = new mongoose.Schema({
    inner: {
      type: new mongoose.Schema({
        something: {
          type: Boolean,
          required: true
        }
      }, { _id: false }),
      require: true
    }
  });

Setting _id: false in the schema path definition as opposed to as a schema option is an odd meme that's managed to find its way into a lot of codebases despite never being documented or fully supported. We'll make it work for this case 👍

vkarpov15 added a commit that referenced this issue Feb 8, 2019
vkarpov15 added a commit that referenced this issue Feb 13, 2019
chrischen pushed a commit to chrischen/mongoose that referenced this issue Feb 16, 2019
* https://github.com/Automattic/mongoose: (41 commits)
  fix(query): copy mongoose options when using `Query#merge()`
  test(query): repro Automattic#1790
  chore: remove unnecessary references to old mongoose versions
  fix(deps): update dependency ms to v2.1.1
  fix(timestamps): don't call createdAt getters when setting updatedAt on new doc
  chore: now working on 5.4.12
  test(timestamps): repro Automattic#7496
  chore: release 5.4.11
  docs: add views guru as sponsor
  refactor: use consolidated `isPOJO()` function instead of constructor checks
  fix(schema): handle `_id: false` in schema paths as a shortcut for setting the `_id` option to `false`
  test(schema): repro Automattic#7480
  Documentation: Document.prototype.validateSync returns ValidationError
  gitignore correct syntax
  style: fix lint
  fix(update): handle $addToSet and $push with ObjectIds and castNonArrays=false
  test(update): repro Automattic#7479
  docs(model): document `session` option to `save()`
  chore: bump lodash dev dep re: security
  chore: now working on 5.4.11
  ...
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