1.0.1 • Published 8 months ago

mongoose-error-humanizer v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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

CaseExample
required field missingname required
multiple, required fields missingemail required, name required
index (unique) violationemail must be unique
enum violationcountry cannot be other
string min length violationcity must be at least 1 character(s) long
string max length violationcity cannot be more than 10 character(s) long
number min violationage must be at least 1
number max violationage cannot exceed 100
nested field violationsbirthday.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)
1.0.1

8 months ago

1.0.0

8 months ago