Why Mongoose doesn't validate on update?

后端 未结 6 2539
故里飘歌
故里飘歌 2020-11-30 00:07

I have this code

var ClientSchema = new Schema({
  name: {type: String, required: true, trim: true}
});

var Client = mongoose.mode(\'Client\', ClientSchema)         


        
6条回答
  •  萌比男神i
    2020-11-30 01:08

    You're not doing anything wrong, validation is implemented as internal middleware within Mongoose and middleware doesn't get executed during an update as that's basically a pass-through to the native driver.

    If you want your client update validated you'll need to find the object to update, apply the new property values to it (see underscore's extend method), and then call save on it.

    Mongoose 4.0 Update

    As noted in the comments and victorkohl's answer, Mongoose now support the validation of the fields of $set and $unset operators when you include the runValidators: true option in the update call.

提交回复
热议问题