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

146
Views
How to use the doValidate method of mongoose?

I have the following schema

const mySchema = new mongoose.Schema({
   kind: {
      type: String,
      enum: { 
         values: ["dollar", "euro", "pound", "lira"],
         message: "only dollar, euro, pound or lira are allowed"
      }
      required: [true, "kind field is required"]
   }
   // ... other fields
})

I have the following code:

app.post("/route", async (req, res, next) => {
    // ๐Ÿ write code here
})

I want to validate only one path from that schema, I don't to validate everything, I just want to validate one field, so what I did according to this git discussion is the following:

app.post("/route", async (req, res, next) => {
    // ๐Ÿ 
    await mySchema.path("kind").doValidate(req.body.kind)
})

but this did not work and I got the following error:

.../node_modules/mongoose/lib/schematype.js:1332
        fn(err);
        ^

TypeError: fn is not a function
    at .../node_modules/mongoose/lib/schematype.js:1332:9
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

How can I use doValidate and get the benefit of it? and why there's no documentation on how to use it?

  • and is there a way I can promisify it?
about 4 years ago ยท Santiago Gelvez
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!