Skip to content

Commit dd90b68

Browse files
committed
Release 0.0.1
0 parents  commit dd90b68

File tree

9 files changed

+229
-0
lines changed

9 files changed

+229
-0
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [ "crocodile" ],
3+
"sourceMaps": "inline",
4+
"plugins": [ "add-module-exports" ]
5+
}

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "crocodile"
3+
}

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# osx
3+
.DS_Store
4+
5+
# project
6+
node_modules/
7+
logs
8+
*.log
9+
npm-debug.log*
10+
*.idea
11+
lib

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License
2+
3+
Copyright (c) 2017- Nick Baugh <niftylettuce@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "mongoose-validation-error-transform",
3+
"description": "Automatically transform Mongoose validation error message(s) to a humanized and readable format",
4+
"version": "0.0.1",
5+
"author": "Nick Baugh <niftylettuce@gmail.com>",
6+
"bugs": "https://github.com/niftylettuce/mongoose-validation-error-transform/issues",
7+
"devDependencies": {
8+
"babel-cli": "^6.11.4",
9+
"babel-plugin-add-module-exports": "^0.2.1",
10+
"babel-preset-crocodile": "^1.0.0",
11+
"eslint-config-crocodile": "^1.0.0"
12+
},
13+
"engines": {
14+
"node": ">= 6.x"
15+
},
16+
"homepage": "https://github.com/niftylettuce/mongoose-validation-error-transform",
17+
"keywords": [
18+
"mongoose",
19+
"validation",
20+
"error",
21+
"transform",
22+
"messages",
23+
"unique",
24+
"beautiful",
25+
"errors",
26+
"err",
27+
"handler",
28+
"handling",
29+
"pretty",
30+
"print",
31+
"alert",
32+
"mongodb",
33+
"mongo",
34+
"custom",
35+
"parse",
36+
"parser",
37+
"parsing"
38+
],
39+
"license": "MIT",
40+
"main": "lib/index.js",
41+
"repository": "niftylettuce/mongoose-validation-error-transform",
42+
"scripts": {
43+
"compile": "rm -rf lib/ && babel -d lib src",
44+
"lint": "eslint .",
45+
"prepublish": "npm run test",
46+
"test": "npm run lint"
47+
},
48+
"dependencies": {
49+
"lodash": "^4.17.4",
50+
"underscore.string": "^3.3.4"
51+
}
52+
}

src/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
const s = require('underscore.string');
3+
const _ = require('lodash');
4+
5+
export default function mongooseValidationErrorTransform(schema, options) {
6+
7+
options = _.defaults(options || {}, {
8+
capitalize: true,
9+
humanize: true,
10+
transform: messages => messages.join(', ')
11+
});
12+
13+
function humanizePath(err, doc, next) {
14+
15+
if (err.name !== 'ValidationError' || !_.isObject(err.errors))
16+
return next(err);
17+
18+
err.message = options.transform(_.map(
19+
_.values(err.errors),
20+
error => {
21+
if (!_.isString(error.path))
22+
return options.capitalize ? s.capitalize(error.message) : error.message;
23+
if (options.humanize)
24+
error.message = error.message.replace(
25+
new RegExp(error.path, 'g'),
26+
s.humanize(error.path)
27+
);
28+
if (options.capitalize)
29+
error.message = s.capitalize(error.message);
30+
return error.message;
31+
}
32+
));
33+
34+
next(err);
35+
36+
}
37+
38+
schema.post('save', humanizePath);
39+
schema.post('update', humanizePath);
40+
schema.post('findOneAndUpdate', humanizePath);
41+
schema.post('insertMany', humanizePath);
42+
43+
return schema;
44+
45+
}

0 commit comments

Comments
 (0)