mongoose-duplicate-error
v1.0.0
Published
Custom error messages for the mongodb duplicate error
Downloads
13
Readme
Mongoose Duplicate Errors
Custom error messages for the mongodb duplicate error
Installation
yarn add mongoose-duplicate-error
Usage
Just add it to a model and it will prettify the unique errors by default, by transforming the
E11000 duplicate key error collection...
error into a other must be unique
validation error.
const Schema = require('mongoose').Schema;
const mongooseDuplicateError = require('mongoose-duplicate-error');
const CustomerSchema = new Schema({
username: {
type: String,
unique: true,
},
email: {
type: String,
unique: true,
},
});
CustomerSchema.plugin(mongooseDuplicateError);
It will also work for compound indexes, by generating an error for the first index in the group
const Schema = require('mongoose').Schema;
const mongooseDuplicateError = require('mongoose-duplicate-error');
const CustomerSchema = new Schema({
email: {
required: true,
type: String,
},
deleted: Boolean,
});
CustomerSchema.index({ email: 1, deleted: 1 }, { unique: true });
CustomerSchema.plugin(mongooseDuplicateError);
You can further custumize the error messages for any unique index. By providing a "path" and message template you can specify exactly which field will recieve an error, and with what text.
const Schema = require('mongoose').Schema;
const mongooseDuplicateError = require('mongoose-duplicate-error');
const CustomerSchema = new Schema({
email: {
required: true,
type: String,
},
deleted: Boolean,
});
CustomerSchema.index({ email: 1, deleted: 1 }, { unique: true });
CustomerSchema.plugin(mongooseDuplicateError, {
indexes: {
email_1_deleted_1: { path: 'email', message: 'The {PATH} must be unique ({VALUE})' },
},
}});