-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow specifying error message override for duplicate key error…
…s `unique: true` Fix #12844
- Loading branch information
Showing
6 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14161,6 +14161,25 @@ describe('document', function() { | |
const fromDb = await ParentModel.findById(doc._id).orFail(); | ||
assert.strictEqual(fromDb.quests[0].campaign.milestones, null); | ||
}); | ||
|
||
it('handles custom error message for duplicate key errors (gh-12844)', async function() { | ||
const schema = new Schema({ | ||
name: String, | ||
email: { type: String, unique: [true, 'Email must be unique'] } | ||
}); | ||
const Model = db.model('Test', schema); | ||
await Model.init(); | ||
|
||
await Model.create({ email: '[email protected]' }); | ||
|
||
let duplicateKeyError = await Model.create({ email: '[email protected]' }).catch(err => err); | ||
assert.strictEqual(duplicateKeyError.message, 'Email must be unique'); | ||
assert.strictEqual(duplicateKeyError.cause.code, 11000); | ||
|
||
duplicateKeyError = await Model.updateOne({ name: 'test' }, { email: '[email protected]' }, { upsert: true }).catch(err => err); | ||
assert.strictEqual(duplicateKeyError.message, 'Email must be unique'); | ||
assert.strictEqual(duplicateKeyError.cause.code, 11000); | ||
}); | ||
}); | ||
|
||
describe('Check if instance function that is supplied in schema option is available', function() { | ||
|