Mongoose post hook handler to automatically convert validation and dup errors into human friendly strings
npm install mongoose-error-humanizerImportant: This package cannot detect when mongoose custom error messages are used in your schema (https://mongoosejs.com/docs/validation.html#custom-error-messages). Do not use this package if you need your custom error messages to be thrown.
name required |email required, name required |email must be unique |country cannot be other |city must be at least 1 character(s) long |city cannot be more than 10 character(s) long |age must be at least 1 |age cannot exceed 100 |birthday.year must be at least 1900 |Thrown errors are of type MongooseHumanError (an extension of the Error class).
``js
const mongoose = require('mongoose')
const humanizeErrors = require('mongoose-error-humanizer')
const schema = new mongoose.Schema({
name: { type: String, required: true, unique: true }
})
schema.post('save', humanizeErrors)
schema.post('update', humanizeErrors)
module.exports = mongoose.model('MyModel', schema)
``