Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
mongoose does not validate the updated part

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?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!