mongoose-error-humanizer

1.0.1 • Public • Published

mongoose-error-humanizer

Mongoose post hook handler to automatically convert validation and dup errors into human friendly strings

Important: 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.

Example Error Messages

Case Example
required field missing name required
multiple, required fields missing email required, name required
index (unique) violation email must be unique
enum violation country cannot be other
string min length violation city must be at least 1 character(s) long
string max length violation city cannot be more than 10 character(s) long
number min violation age must be at least 1
number max violation age cannot exceed 100
nested field violations birthday.year must be at least 1900

Thrown errors are of type MongooseHumanError (an extension of the Error class).

Usage

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)

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-error-humanizer

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

9.19 kB

Total Files

6

Last publish

Collaborators

  • shivpatel