Automattic / Automattic/mongoose

SubDocuments Validation is not working on findOneAndUpdate

Open
#5,234 7 comments 4 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

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??!!!

Contributor guide

Open the contributing guide

Research direction

The issue provides BalanceSchema, InvoiceSchema, and the findOneAndUpdate call but names no repository files or tests. Reproduce the update with missing or invalid taxes subdocument fields, then inspect the existing validation tests and update-validator behavior. Done means the expected validators run consistently for taxes subdocuments during findOneAndUpdate, with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.