# mongoose-validation-error-transform

> Automatically transform Mongoose validation error message(s) to a humanized and readable format

Latest version **0.0.6** (published 2020-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-validation-error-transform
pnpm add mongoose-validation-error-transform
yarn add mongoose-validation-error-transform
bun add mongoose-validation-error-transform
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2020-07-10 |
| First published | 2017-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.x |
| Dependencies | 3 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Nick Baugh |
| Maintainers | niftylettuce |
| Keywords | alert, beautiful, custom, err, error, errors, handler, handling, messages, mongo, mongodb, mongoose, parse, parser, parsing, pretty, print, transform, unique, validation |

## Links

- npm: https://www.npmjs.com/package/mongoose-validation-error-transform
- Repository: https://github.com/niftylettuce/mongoose-validation-error-transform
- Issues: https://github.com/niftylettuce/mongoose-validation-error-transform/issues
- npm.io page: https://npm.io/package/mongoose-validation-error-transform

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [capitalize](https://npm.io/package/capitalize.md) ^2.0.0
- [humanize-string](https://npm.io/package/humanize-string.md) ^2.1.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2020-07-10
- 0.0.5 — 2019-09-09
- 0.0.4 — 2019-09-05
- 0.0.2 — 2017-08-08

## README

# mongoose-validation-error-transform

[![Slack Status][slack-image]][slack-url]
[![NPM version][npm-image]][npm-url]
[![Standard JS Style][standard-image]][standard-url]
[![MIT License][license-image]][license-url]

> Automatically transform [Mongoose][mongoose] validation error message(s) to a humanized and readable format, built for [CrocodileJS][crocodile-url].


## Index

* [Install](#install)
* [Usage](#usage)
* [License](#license)


## Install

```bash
npm install --save mongoose-validation-error-transform
```

> You may also want to use [mongoose-beautiful-unique-validation][mongoose-beautiful-unique-validation] too (see [this comment][comment])!


## Usage

```js
const mongooseValidationErrorTransform = require('mongoose-validation-error-transform');

mongoose.plugin(mongooseValidationErrorTransform, {

  //
  // these are the default options you can override
  // (you don't need to specify this object otherwise)
  //

  // should we capitalize the first letter of the message?
  capitalize: true,

  // should we convert `full_name` => `Full name`?
  humanize: true,

  // how should we join together multiple validation errors?
  transform: function(messages) {
    return messages.join(', ');
  }

});
```

If you have a Mongoose schema defined with a required String field `full_name`,
and if there is an error with a missing `full_name` on a document - then it will
automatically rewrite the message of `full_name is required` to
`Full name is required`.

If there are multiple validation error messages, such as:

* `full_name is required`
* `age is not at least (18)`

Then it will rewrite the error message to `Full name is required, Age is not at least (18)`.

Of course - by modifying the options mentioned above, you can transform the messages however you'd like.

For example, if you'd like to output a `<ul>` HTML tag with `<li>` for each error (but only of course if there's more than one error):

```js
mongoose.plugin(mongooseValidationErrorTransform, {
  transform: function(messages) {
    if (messages.length === 1) return messages[0];
    return `<ul><li>${messages.join('</li><li>')}</li></ul>`;
  }
});
```

This would output the following for the previous example:

```html
<ul><li>Full name is required</li><li>Age is not at least (18)</li></ul>
```


## License

[MIT](LICENSE) © Nick Baugh


## 

[license-image]: http://img.shields.io/badge/license-MIT-blue.svg

[license-url]: LICENSE

[npm-image]: https://img.shields.io/npm/v/mongoose-validation-error-transform.svg

[npm-url]: https://npmjs.org/package/mongoose-validation-error-transform

[crocodile-url]: https://crocodilejs.com

[standard-image]: https://img.shields.io/badge/code%20style-standard%2Bes7-brightgreen.svg

[standard-url]: https://github.com/crocodilejs/eslint-config-crocodile

[slack-image]: https://img.shields.io/badge/chat-join%20slack-brightgreen

[slack-url]: https://join.slack.com/t/ladjs/shared_invite/zt-fqei6z11-Bq2trhwHQxVc5x~ifiZG0g

[mongoose]: https://github.com/Automattic/mongoose

[comment]: https://github.com/Automattic/mongoose/issues/2284#issuecomment-320810641

[mongoose-beautiful-unique-validation]: https://github.com/matteodelabre/mongoose-beautiful-unique-validation

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