# mongoose-error-humanizer

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

Latest version **1.0.1** (published 2023-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-error-humanizer
pnpm add mongoose-error-humanizer
yarn add mongoose-error-humanizer
bun add mongoose-error-humanizer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-09-02 |
| First published | 2023-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Shiv Patel |
| Maintainers | shivpatel |

## Links

- npm: https://www.npmjs.com/package/mongoose-error-humanizer
- Repository: https://github.com/shivpatel/mongoose-error-humanizer
- Homepage: https://github.com/shivpatel/mongoose-error-humanizer#readme
- Issues: https://github.com/shivpatel/mongoose-error-humanizer/issues
- npm.io page: https://npm.io/package/mongoose-error-humanizer

## Recent versions

- 1.0.1 (latest) — 2023-09-02
- 1.0.0 — 2023-09-01

## README

# 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

```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)
```

---
_Source: https://npm.io/package/mongoose-error-humanizer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
