|
| 1 | + |
| 2 | +# mongoose-validation-error-transform |
| 3 | + |
| 4 | +[![Slack Status][slack-image]][slack-url] |
| 5 | +[![NPM version][npm-image]][npm-url] |
| 6 | +[![Standard JS Style][standard-image]][standard-url] |
| 7 | +[![MIT License][license-image]][license-url] |
| 8 | + |
| 9 | +> Automatically transform [Mongoose][mongoose] validation error message(s) to a humanized and readable format, built for [CrocodileJS][crocodile-url]. |
| 10 | +
|
| 11 | + |
| 12 | +## Index |
| 13 | + |
| 14 | +* [Install](#install) |
| 15 | +* [Usage](#usage) |
| 16 | +* [License](#license) |
| 17 | + |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install --save mongoose-validation-error-transform |
| 23 | +``` |
| 24 | + |
| 25 | +> You may also want to use [mongoose-beautiful-unique-validation][mongoose-beautiful-unique-validation] too (see [this comment][comment])! |
| 26 | +
|
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +```js |
| 31 | +const mongooseValidationErrorTransform = require('mongoose-validation-error-transform'); |
| 32 | + |
| 33 | +mongoose.plugin(mongooseValidationErrorTransform, { |
| 34 | + |
| 35 | + // |
| 36 | + // these are the default options you can override |
| 37 | + // (you don't need to specify this object otherwise) |
| 38 | + // |
| 39 | + |
| 40 | + // should we capitalize the first letter of the message? |
| 41 | + capitalize: true, |
| 42 | + |
| 43 | + // should we convert `full_name` => `Full name`? |
| 44 | + humanize: true, |
| 45 | + |
| 46 | + // how should we join together multiple validation errors? |
| 47 | + transform: function(messages) { |
| 48 | + return messages.join(', '); |
| 49 | + } |
| 50 | + |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +If you have a Mongoose schema defined with a required String field `full_name`, |
| 55 | +and if there is an error with a missing `full_name` on a document - then it will |
| 56 | +automatically rewrite the message of `"full_name" is required` to |
| 57 | +`"Full name is required"`. |
| 58 | + |
| 59 | +If there are multiple validation error messages, such as: |
| 60 | + |
| 61 | +* `"full_name" is required` |
| 62 | +* `"age" is not at least (18)` |
| 63 | + |
| 64 | +Then it will rewrite the error message to `Full name is required, Age is not at least (18)`. |
| 65 | + |
| 66 | +Of course - by modifying the options mentioned above, you can transform the messages however you'd like. |
| 67 | + |
| 68 | +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): |
| 69 | + |
| 70 | +```js |
| 71 | +mongoose.plugin(mongooseValidationErrorTransform, { |
| 72 | + transform: function(messages) { |
| 73 | + if (messages.length === 1) return messages[0]; |
| 74 | + return `<ul><li>${messages.join('</li><li>')}</li></ul>`; |
| 75 | + } |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +This would output the following for the previous example: |
| 80 | + |
| 81 | +```html |
| 82 | +<ul><li>Full name is required</li><li>Age is not at least (18)</li></ul> |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +[MIT][license-url] |
| 89 | + |
| 90 | + |
| 91 | +[license-image]: http://img.shields.io/badge/license-MIT-blue.svg |
| 92 | +[license-url]: LICENSE |
| 93 | +[npm-image]: https://img.shields.io/npm/v/mongoose-validation-error-transform.svg |
| 94 | +[npm-url]: https://npmjs.org/package/mongoose-validation-error-transform |
| 95 | +[crocodile-url]: https://crocodilejs.com |
| 96 | +[standard-image]: https://img.shields.io/badge/code%20style-standard%2Bes7-brightgreen.svg |
| 97 | +[standard-url]: https://github.com/crocodilejs/eslint-config-crocodile |
| 98 | +[slack-image]: http://slack.crocodilejs.com/badge.svg |
| 99 | +[slack-url]: http://slack.crocodilejs.com |
| 100 | +[mongoose]: https://github.com/Automattic/mongoose |
| 101 | +[comment]: https://github.com/Automattic/mongoose/issues/2284#issuecomment-320810641 |
| 102 | +[mongoose-beautiful-unique-validation]: https://github.com/matteodelabre/mongoose-beautiful-unique-validation |
0 commit comments