-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Configuration to make runValidators default to true? #6578
Comments
That's a good idea. Right now the way to do it would be with a global plugin: mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', setRunValidators);
schema.pre('updateMany', setRunValidators);
schema.pre('updateOne', setRunValidators);
schema.pre('update', setRunValidators);
});
function setRunValidators() {
this.setOptions({ runValidators: true });
} Would be neat to have this as a one-liner though. |
Some other nice global one-liners might be:
I have been using those policies in our projects (implemented in our common model builder function) because it helps to detect typos and other developer mistakes sooner. We override the defaults when we need to. So if you are thinking of introducing some global settings in future, let's try to find something that works neatly for all of those! Anyway the example above is good enough for me, for now, thanks. |
The |
The code below works, and throws an error as expected. const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', setRunValidators);
schema.pre('updateMany', setRunValidators);
schema.pre('updateOne', setRunValidators);
schema.pre('update', setRunValidators);
});
function setRunValidators () {
this.setOptions({ runValidators: true });
}
mongoose.connect('mongodb://localhost:27017/test', { family: 4, useNewUrlParser: true });
const userSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, min: 13, max: 90 }
});
const User = mongoose.model('User', userSchema);
const hafez = new User({ name: 'Hafez', age: 24 });
async function run () {
await User.remove();
await hafez.save();
await User.updateOne({}, { name: null });
}
run().catch(console.error); However, running this doesn't work throw an error. I can't seem to find a reference to const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.set('runValidators', true);
mongoose.connect('mongodb://localhost:27017/test', { family: 4, useNewUrlParser: true });
const userSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, min: 13, max: 90 }
});
const User = mongoose.model('User', userSchema);
const hafez = new User({ name: 'Hafez', age: 24 });
async function run () {
await User.remove();
await hafez.save();
await User.updateOne({}, { name: null });
}
run().catch(console.error); |
Also would be great to have a global option to |
Thanks for reporting, will investigate ASAP |
who can tell me where are the caveats of updating validators? I can't find it. |
@lineus thx |
@lqzhgood check https://mongoosejs.com/docs/api.html#query_Query-getOptions function setRunValidators() {
if ('runValidators' in this.getOptions()) {
return;
}
this.setOptions({ runValidators: true });
} |
@vkarpov15 i get "this.setOptions is not a function" error. |
@yeknava Do you happen to be using an arrow function instead of the code snippet above? Maybe share some code? |
sure @AbdelrahmanHafez , here is my code: function setRunValidators() {
this.setOptions({ runValidators: true, new: true });
}
mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', setRunValidators);
schema.pre('updateMany', setRunValidators);
schema.pre('updateOne', setRunValidators);
schema.pre('update', setRunValidators);
}); |
Until I discovered runValidators recently, I had always assumed mongoose did validation on updates by default. But it doesn't!
Would it be possible to add a global config or a schema config that would make
runValidators
default totrue
for all updates? (We could then override withfalse
on individual queries, if we need to.)The documentation says:
And the caveats are well documented below that.
In our apps, we tend to use only simple validators, so most of the caveats don't apply. If we do expand our validators, we could either turn off the option for that schema, or adapt our validators and update queries as per the docs.
The text was updated successfully, but these errors were encountered: