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

Open
wants to merge 2 commits into
base: 8.9
Choose a base branch
from

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

Comment on lines +2802 to +2804
/*!
*
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should likely have docs or a ignore comment like other places

* will still throw MongoDB's default `E11000 duplicate key error` message.
*
* @param {Boolean} value
* @param {String} message
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* @param {String} message
* @param {String} [message]

if i understand correctly, message can be optional (theoretically value could be too)

@@ -309,7 +309,7 @@ declare module 'mongoose' {
eachPath(fn: (path: string, type: SchemaType) => void): this;

/** Defines an index (most likely compound) for this schema. */
index(fields: IndexDefinition, options?: IndexOptions): this;
index(fields: IndexDefinition, options?: Omit<IndexOptions, 'unique'> & { unique?: boolean | undefined | [true, string] }): this;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
index(fields: IndexDefinition, options?: Omit<IndexOptions, 'unique'> & { unique?: boolean | undefined | [true, string] }): this;
index(fields: IndexDefinition, options?: Omit<IndexOptions, 'unique'> & { unique?: boolean | [true, string] }): this;

i think this undefined can be omitted because property is already marked ?

@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
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.

2 participants