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

validation ignored if the path set as 'select: false' #13062

Closed
2 tasks done
ryanmz1 opened this issue Feb 21, 2023 · 3 comments
Closed
2 tasks done

validation ignored if the path set as 'select: false' #13062

ryanmz1 opened this issue Feb 21, 2023 · 3 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@ryanmz1
Copy link

ryanmz1 commented Feb 21, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Related Issue:
#730
[https://github.com//issues/730](issue 730)

...
const User = mongoose.model('User', Schema({
  name: { type: String, required: true },
  age: { type: Number, required: true, **select:false** }
});
// set user's name and age
// user={name:'Ben',age:16}
...
// then update and save in this way
const user=await User.findOne({name}); // user={name:'Ben'}
// do some work
...
await user.save(); // does not run validation on 'age'
...

At most time, before doc.save(), validation will run. But if select option set to false on some path, it does not run validation on that path even there's a required validator until select set to true. This idea may come from #730. That's reasonable enough.

But it's so sad that this rationale cannot be searched on the documentation. I only found it in source code, around line 2610:
image

I just thought full validation will run rather than select option determines. It wasted some time.
So, for making more lives much easier, I think this rule should be added to the documentation directly and explicitly, at least a warning item of rules on validation ;)

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Feb 21, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.9.4, 6.9.5 Feb 21, 2023
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Feb 21, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Feb 21, 2023

The following script throws an error as expected. Please modify the below script to demonstrate the issue you're seeing.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: { type: String, required: true },
  age: { type: Number, required: true, select: false }
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({
    name: 'Test Testerson',
    age: 0
  });

  const doc = await Test.findOne();
  doc.age = null;
  doc.name = 'Test';
  await doc.save();
  console.log('done');
}

run();

@vkarpov15 vkarpov15 removed this from the 6.9.5 milestone Feb 21, 2023
@ryanmz1
Copy link
Author

ryanmz1 commented Feb 27, 2023

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: { type: String, required: true },
  age: { type: Number, required: true, select: false },
  links: { type: String, required: true, select: false }
});

testSchema.pre('save', function() {
  **this.links = undefined;**
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({
    name: 'Test Testerson',
    age: 0,
    **links: 'some init links'**
  });

  const doc = await Test.findOne(); // error occur: .select('+links');
  
  doc.name = 'Test';
  **await doc.save();**
  console.log('done');
}
run();

"links" set to undefined manually in pre save middleware, but finally there is no error. When you turn on select option, then error occur as expected.

Maybe it's a very weird usage situation above but actually the most interesting thing I see are just:

  1. validation happens at a very first time among pre save middlewares.
  2. the select option does determine validation paths list, which is difficult for beginners to realize though.

The following script throws an error as expected. Please modify the below script to demonstrate the issue you're seeing.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: { type: String, required: true },
  age: { type: Number, required: true, select: false }
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({
    name: 'Test Testerson',
    age: 0
  });

  const doc = await Test.findOne();
  doc.age = null;
  doc.name = 'Test';
  await doc.save();
  console.log('done');
}

run();

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Feb 27, 2023
@vkarpov15 vkarpov15 added this to the 6.10.3 milestone Feb 27, 2023
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. 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 can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Mar 6, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: { type: String, required: true },
  age: { type: Number, required: true, select: false },
  links: { type: String, required: true, select: false }
});

testSchema.pre('save', function() {
  this.links = undefined;
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({
    name: 'Test Testerson',
    age: 0,
    links: 'some init links'
  });

  const doc = await Test.findOne(); // error occur: .select('+links');
  
  doc.name = 'Test';
  await doc.save();
  console.log('what is doc', doc);
  console.log('done');
}
run();

@vkarpov15 vkarpov15 modified the milestones: 6.10.3, 6.10.2 Mar 7, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.10.2, 6.10.3 Mar 7, 2023
vkarpov15 added a commit that referenced this issue Mar 8, 2023
Avoid saving changes to non-modified paths
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants