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

feat: allow specifying error message override for duplicate key errors unique: true #15059

Merged
merged 6 commits into from
Dec 3, 2024

Conversation

vkarpov15
Copy link
Collaborator

Summary

Allow overriding MongoDB's default "duplicate key error" message in your schemas:

new Schema({
  email: { type: String, unique: [true, 'Email must be unique'] }
});

This works for save() and query methods, but does not work for bulkWrite() and insertMany() because the duplicate key error in that case does not return keyPattern, which means we would either have to guess the duplicate key by the index name (brittle) or parse the index key from the error message (we would have to pull in Acorn or something like that, because duplicate key error message is not formatted as JSON).

Examples

@vkarpov15 vkarpov15 changed the base branch from master to 8.9 November 25, 2024 22:34
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor docs things

lib/schema.js Outdated Show resolved Hide resolved
lib/schemaType.js Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
@hasezoey hasezoey added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Nov 26, 2024
@hasezoey hasezoey added this to the 8.9 milestone Nov 26, 2024
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 merged commit beba447 into 8.9 Dec 3, 2024
76 of 77 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-12844 branch December 4, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants