matteodelabre / mongoose-beautiful-unique-validation

Plugin for Mongoose that turns duplicate errors into regular Mongoose validation errors
MIT License
117 stars 38 forks source link

Support for compound indexes #51

Open BusbyActual opened 6 years ago

BusbyActual commented 6 years ago

The current version appears to support unique keys however not complex unique keys.

Example

const mongoose = require('mongoose');
const beautifyUnique = require('../scripts/mongoose-beautiful-unique-validation');
const Schema = mongoose.Schema;

let modelSchema = new Schema({
  name: { type: String, required: true, max: 100 },
  serial: { type: String, required: true, max: 100 },
  foreignKey: { type: String, required: true, max: 100 },
  created: { type: Date, default: () => new Date() }
});

modelSchema .index({ foreignKey: 1, serial: 1 }, { unique: true });
modelSchema .plugin(beautifyUnique);

module.exports = mongoose.model('model', modelSchema );

Suggested

modelSchema .index({ foreignKey: 1, serial: 1 }, { unique: "'{VALUE}' is already in use" });
sanwalfarooque commented 3 years ago

@BusbyActual Any update on this ? Custom message for compound indexes ?