-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
base: 8.9
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
/*! | ||
* | ||
*/ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ?
Summary
Allow overriding MongoDB's default "duplicate key error" message in your schemas:
This works for
save()
and query methods, but does not work forbulkWrite()
andinsertMany()
because the duplicate key error in that case does not returnkeyPattern
, 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