Skip to content

Commit 69fbb1c

Browse files
committed
feat: upgraded deps, fixed package, removed underscore.string
1 parent 44154c3 commit 69fbb1c

File tree

8 files changed

+7074
-52
lines changed

8 files changed

+7074
-52
lines changed

.babelrc

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"presets": [ "crocodile" ],
3-
"sourceMaps": "inline",
4-
"plugins": [ "add-module-exports" ]
2+
"presets": [
3+
["@babel/env", {
4+
"targets": {
5+
"node": "6.4.0"
6+
}
7+
}]
8+
]
59
}

.eslintignore

-1
This file was deleted.

.eslintrc

-3
This file was deleted.

.lib.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["eslint:recommended", "plugin:node/recommended"]
3+
}

Readme.md renamed to README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# mongoose-validation-error-transform
32

43
[![Slack Status][slack-image]][slack-url]
@@ -85,18 +84,31 @@ This would output the following for the previous example:
8584

8685
## License
8786

88-
[MIT][license-url]
87+
[MIT](LICENSE) © Nick Baugh
88+
8989

90+
##
9091

9192
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg
93+
9294
[license-url]: LICENSE
95+
9396
[npm-image]: https://img.shields.io/npm/v/mongoose-validation-error-transform.svg
97+
9498
[npm-url]: https://npmjs.org/package/mongoose-validation-error-transform
99+
95100
[crocodile-url]: https://crocodilejs.com
101+
96102
[standard-image]: https://img.shields.io/badge/code%20style-standard%2Bes7-brightgreen.svg
103+
97104
[standard-url]: https://github.com/crocodilejs/eslint-config-crocodile
105+
98106
[slack-image]: http://slack.crocodilejs.com/badge.svg
107+
99108
[slack-url]: http://slack.crocodilejs.com
109+
100110
[mongoose]: https://github.com/Automattic/mongoose
111+
101112
[comment]: https://github.com/Automattic/mongoose/issues/2284#issuecomment-320810641
113+
102114
[mongoose-beautiful-unique-validation]: https://github.com/matteodelabre/mongoose-beautiful-unique-validation

package.json

+83-24
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,108 @@
44
"version": "0.0.2",
55
"author": "Nick Baugh <niftylettuce@gmail.com>",
66
"bugs": "https://github.com/niftylettuce/mongoose-validation-error-transform/issues",
7+
"commitlint": {
8+
"extends": [
9+
"@commitlint/config-conventional"
10+
]
11+
},
12+
"contributors": [
13+
"Nick Baugh <niftylettuce@gmail.com>"
14+
],
15+
"dependencies": {
16+
"capitalize": "^2.0.0",
17+
"humanize": "^0.0.9",
18+
"lodash": "^4.17.15"
19+
},
720
"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"
21+
"@babel/cli": "^7.5.5",
22+
"@babel/core": "^7.5.5",
23+
"@babel/preset-env": "^7.5.5",
24+
"@commitlint/cli": "^8.1.0",
25+
"@commitlint/config-conventional": "^8.1.0",
26+
"eslint": "^6.3.0",
27+
"eslint-config-xo-lass": "^1.0.3",
28+
"eslint-plugin-node": "^10.0.0",
29+
"fixpack": "^2.3.1",
30+
"husky": "^3.0.5",
31+
"lint-staged": "^9.2.5",
32+
"remark-cli": "^7.0.0",
33+
"remark-preset-github": "^0.0.16",
34+
"rimraf": "^3.0.0",
35+
"xo": "^0.24.0"
1236
},
1337
"engines": {
1438
"node": ">= 6.x"
1539
},
40+
"files": [
41+
"lib "
42+
],
1643
"homepage": "https://github.com/niftylettuce/mongoose-validation-error-transform",
44+
"husky": {
45+
"hooks": {
46+
"pre-commit": "lint-staged && npm test",
47+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
48+
}
49+
},
1750
"keywords": [
18-
"mongoose",
19-
"validation",
20-
"error",
21-
"transform",
22-
"messages",
23-
"unique",
51+
"alert",
2452
"beautiful",
25-
"errors",
53+
"custom",
2654
"err",
55+
"error",
56+
"errors",
2757
"handler",
2858
"handling",
29-
"pretty",
30-
"print",
31-
"alert",
32-
"mongodb",
59+
"messages",
3360
"mongo",
34-
"custom",
61+
"mongodb",
62+
"mongoose",
3563
"parse",
3664
"parser",
37-
"parsing"
65+
"parsing",
66+
"pretty",
67+
"print",
68+
"transform",
69+
"unique",
70+
"validation"
3871
],
3972
"license": "MIT",
73+
"lint-staged": {
74+
"*.js": [
75+
"xo --fix",
76+
"git add"
77+
],
78+
"*.md": [
79+
"remark . -qfo",
80+
"git add"
81+
],
82+
"package.json": [
83+
"fixpack",
84+
"git add"
85+
]
86+
},
4087
"main": "lib/index.js",
88+
"prettier": {
89+
"singleQuote": true,
90+
"bracketSpacing": true,
91+
"trailingComma": "none"
92+
},
93+
"remarkConfig": {
94+
"plugins": [
95+
"preset-github"
96+
]
97+
},
4198
"repository": "niftylettuce/mongoose-validation-error-transform",
4299
"scripts": {
43-
"compile": "rm -rf lib/ && babel -d lib src",
44-
"lint": "eslint .",
45-
"prepublish": "npm run test",
46-
"test": "npm run lint"
100+
"compile": "rimraf lib && babel src --out-dir lib && eslint --no-inline-config -c .lib.eslintrc lib",
101+
"lint": "xo && remark . -qfo",
102+
"test": "npm run lint && npm run compile"
47103
},
48-
"dependencies": {
49-
"lodash": "^4.17.4",
50-
"underscore.string": "^3.3.4"
104+
"xo": {
105+
"prettier": true,
106+
"space": true,
107+
"extends": [
108+
"xo-lass"
109+
]
51110
}
52111
}

src/index.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
2-
const s = require('underscore.string');
31
const _ = require('lodash');
2+
const capitalize = require('capitalize');
3+
const humanize = require('humanize');
44

5-
export default function mongooseValidationErrorTransform(schema, options) {
6-
5+
function mongooseValidationErrorTransform(schema, options) {
76
options = _.defaults(options || {}, {
87
capitalize: true,
98
humanize: true,
109
transform: messages => messages.join(', ')
1110
});
1211

1312
function humanizePath(err, doc, next) {
14-
15-
if (err.name !== 'ValidationError' || !_.isObject(err.errors))
13+
if (err.name !== 'ValidationError' || !_.isObject(err.errors)) {
1614
return next(err);
15+
}
1716

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)
17+
err.message = options.transform(
18+
_.map(_.values(err.errors), error => {
19+
if (!_.isString(error.path)) {
20+
return options.capitalize ? capitalize(error.message) : error.message;
21+
}
22+
23+
if (options.humanize) {
2424
error.message = error.message.replace(
2525
new RegExp(error.path, 'g'),
26-
s.humanize(error.path)
26+
humanize(error.path)
2727
);
28-
if (options.capitalize)
29-
error.message = s.capitalize(error.message);
28+
}
29+
30+
if (options.capitalize) {
31+
error.message = capitalize(error.message);
32+
}
33+
3034
return error.message;
31-
}
32-
));
35+
})
36+
);
3337

3438
next(err);
35-
3639
}
3740

3841
schema.post('save', humanizePath);
@@ -41,5 +44,6 @@ export default function mongooseValidationErrorTransform(schema, options) {
4144
schema.post('insertMany', humanizePath);
4245

4346
return schema;
44-
4547
}
48+
49+
module.exports = mongooseValidationErrorTransform;

0 commit comments

Comments
 (0)