question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SubDocuments Validation is not working on findOneAndUpdate

See original GitHub issue

This is My Model Schema:

const BalanceSchema = new Schema({
   name: { type: String, required: true },
   rate: { type: Number, required: true },
   amount: { type: Number, required: true },
});

const InvoiceSchema = new Schema({
   key: { type: String, required: true, match: /(EST|INV|BIL)-\w+/ },
   name: { type: String, required: true },
   taxes: [BalanceSchema]
});
And This How I Update my documents:

Invoice.findOneAndUpdate({
   _id: request.params.invoiceId,
  }, { status: request.body }, { new: true, runValidators: true }).exec(function (error, invoice) {
    if (error) response.apiError(error);
    if (!invoice) return response.apiNotFound();
    return response.apiResponse(invoice);
});

The problem is when I execute this function (findOnAndUpdate), the option runValidators: true doesn’t work on taxes subDocuments but only on invoice fields??!!!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
medkhelificommented, May 29, 2017

Thank you for your response:

mongoose validators will only run in a findByIdAndUpdate operation with runValidators set to true on the fields you are trying to update

Can we see mongoose validators running in findOneAndUpdate in futur évolutions???

0reactions
sobafuchscommented, Jun 5, 2017

I will add it as a new feature request. I remember talking to @vkarpov15 about this and I think the plan is to make validators run by default in 5.x. However, it wont be possible to run validators for the entire object because mongoose can only see what you’re updating in a findByIdAndUpdate call. IF you want to validate an update operation based on the entire document, you’ll need to do a .save()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose findOneAndUpdate on subdocument throw an ...
It only throws for fields where type: Schema.Types.ObjectId (In this example lastMessage.message prop pass validation);; It doesn't throw for ...
Read more >
Mongoose findOneAndUpdate and runValidators not working
I am having issues trying to get the 'runValidators' option to work. My post schema has an comments.replies.creator field that has required ...
Read more >
Mongoose v6.8.2: Validation
Validators are not run on undefined values. The only exception is the required validator. Validation is asynchronously recursive; when you call Model#save, sub- ......
Read more >
Cannot Update SubDocument using findOneAndUpdate
this is my code to update the doc UserAddress.findOneAndUpdate({ "_id": _userAddress._id, "address._id .
Read more >
Mongoose query findOneAndUpdate() doesn't update ...
Because Mongoose by default creates a new MongoDB ObjectId ( this hidden _id field) every time you pass it a Javascript Object to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found