mongoose does not validate the updated part

Viewed 37

I have created a userSchema mongoose model and here is the related part of my model :

  password: {
    type: String,
    required: [true, "Please enter a password"],
    minlength: [6, "Minimum password length is 6 characters"],
  },

but when i updated a record that is saved to database:

userSchema.statics.updated = async function (_id, field, value) {
  const user = await this.updateOne(
    { _id },

    {
      $set: { [field]: value, updatedAt: Date.now() },
    }
  );
};

the validation does not work i tried adding {runValidators:true} or creating a middleware like

userSchema.pre("updatedOne", function (next) {
  this.options.runValidators = true;
  next();
});

but none of them solved my problem. How can i handle that?

0 Answers
Related