SubDocuments Validation is not working on findOneAndUpdate
See original GitHub issueThis 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:
- Created 6 years ago
- Reactions:4
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you for your response:
Can we see mongoose validators running in findOneAndUpdate in futur évolutions???
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()