Skip to content

enhancv/mongoose-duplicate-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose Duplicate Errors

Custom error messages for the mongodb duplicate error

Build Status Code Climate Test Coverage

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})' },
    },
}});

About

Custom error messages for the mongodb duplicate error

Resources

Stars

Watchers

Forks

Packages

No packages published