Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): make bulkWrite() and insertMany() throw if throwOnValidationError set and all ops invalid #14587

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3233,6 +3233,14 @@ Model.$__insertMany = function(arr, options, callback) {

// Quickly escape while there aren't any valid docAttributes
if (docAttributes.length === 0) {
if (throwOnValidationError) {
return callback(new MongooseBulkWriteError(
validationErrors,
results,
null,
'insertMany'
));
}
if (rawResult) {
const res = {
acknowledged: true,
Expand Down Expand Up @@ -3588,6 +3596,14 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
validOps = validOps.sort().map(index => ops[index]);

if (validOps.length === 0) {
if (options.throwOnValidationError && validationErrors.length) {
throw new MongooseBulkWriteError(
validationErrors,
results,
res,
'bulkWrite'
);
}
return getDefaultBulkwriteResult();
}

Expand Down
44 changes: 41 additions & 3 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4062,7 +4062,32 @@ describe('Model', function() {
assert.ok(doc.createdAt.valueOf() >= now.valueOf());
assert.ok(doc.updatedAt);
assert.ok(doc.updatedAt.valueOf() >= now.valueOf());
});

it('throwOnValidationError (gh-14572)', async function() {
const schema = new Schema({
num: Number
});

const M = db.model('Test', schema);

const ops = [
{
insertOne: {
document: {
num: 'not a number'
}
}
}
];

const err = await M.bulkWrite(
ops,
{ ordered: false, throwOnValidationError: true }
).then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'MongooseBulkWriteError');
assert.equal(err.validationErrors[0].errors['num'].name, 'CastError');
});

it('with child timestamps and array filters (gh-7032)', async function() {
Expand Down Expand Up @@ -6602,14 +6627,14 @@ describe('Model', function() {
});

it('insertMany should throw an error if there were operations that failed validation, ' +
'but all operations that passed validation succeeded (gh-13256)', async function() {
'but all operations that passed validation succeeded (gh-14572) (gh-13256)', async function() {
const userSchema = new Schema({
age: { type: Number }
});

const User = db.model('User', userSchema);

const err = await User.insertMany([
let err = await User.insertMany([
new User({ age: 12 }),
new User({ age: 12 }),
new User({ age: 'NaN' })
Expand All @@ -6623,7 +6648,20 @@ describe('Model', function() {
assert.ok(err.results[2] instanceof Error);
assert.equal(err.results[2].errors['age'].name, 'CastError');

const docs = await User.find();
let docs = await User.find();
assert.deepStrictEqual(docs.map(doc => doc.age), [12, 12]);

err = await User.insertMany([
new User({ age: 'NaN' })
], { ordered: false, throwOnValidationError: true })
.then(() => null)
.catch(err => err);

assert.ok(err);
assert.equal(err.name, 'MongooseBulkWriteError');
assert.equal(err.validationErrors[0].errors['age'].name, 'CastError');

docs = await User.find();
assert.deepStrictEqual(docs.map(doc => doc.age), [12, 12]);
});

Expand Down