Skip to content

Commit af5cf12

Browse files
committed
init
0 parents  commit af5cf12

File tree

11 files changed

+6371
-0
lines changed

11 files changed

+6371
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = false
12+
insert_final_newline = false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
globalConfig.json
2+
node_modules

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Mongoose Validation Failed Handler
2+
3+
## Usage
4+
5+
```javascripts
6+
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');
7+
8+
const error = mongooseErrorHandler(mongoose.Error.ValidationError, {
9+
messages: {
10+
[kind]: String
11+
},
12+
paths: {
13+
[path]: Object
14+
}
15+
});
16+
```

jest-mongodb-config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
mongodbMemoryServerOptions: {
3+
instance: {
4+
dbName: 'jest',
5+
port: 27001,
6+
},
7+
binary: {
8+
version: '4.0.3',
9+
skipMD5: true
10+
},
11+
autoStart: false
12+
}
13+
};

lib/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const { defaultsDeep, lowerFirst, isObject, omit, isEmpty } = require('lodash');
2+
const format = require('string-template');
3+
const messages = require('./messages');
4+
const { transformsError, errorx } = require('./utils');
5+
6+
function mongooseErrorHandler(error, options) {
7+
options = defaultsDeep(options, {
8+
capitalize: false,
9+
humanize: false,
10+
messages: messages,
11+
paths: {},
12+
});
13+
14+
if (error.name === 'ValidationError') {
15+
const attr = Object.keys(error.errors).shift();
16+
const err = error.errors[attr];
17+
const { path, kind, value, properties, stringValue } = transformsError(err);
18+
const ex = errorx({ path, kind, value });
19+
const agrs = {
20+
path,
21+
value: stringValue,
22+
...properties,
23+
};
24+
25+
if (!isEmpty(options.paths) && options.paths[path]) {
26+
const po = options.paths[path];
27+
if (
28+
po.original === true &&
29+
(isEmpty(po.kind) || (po.kind && po.kind === kind))
30+
) {
31+
return ex(err.message);
32+
}
33+
34+
if (po.message && (isEmpty(po.kind) || (po.kind && po.kind === kind))) {
35+
return ex(format(po.message, agrs));
36+
}
37+
}
38+
39+
if (options.messages[kind]) {
40+
return ex(format(options.messages[kind], agrs));
41+
}
42+
43+
return ex(format(options.messages.base, agrs));
44+
}
45+
46+
return error;
47+
}
48+
49+
module.exports = mongooseErrorHandler;

lib/messages.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
base: '{path} is invalid',
3+
required: '{path} is required',
4+
enum: '{path} is invalid',
5+
6+
string: '{path} must be a string',
7+
maxlength: '{path} length must be less than or equal to {maxlength} characters long',
8+
minlength: '{path} length must be at least {minlength} characters long',
9+
regexp: '{path} format is invalid',
10+
11+
number: '{path} must be a number',
12+
'number.max': '{path} must be greater than or equal to {max}',
13+
'number.min': '{path} must be less than or equal to {{#limit}}',
14+
15+
date: '{path} must be a date',
16+
'date.max': '{path} must be less than or equal to {max}',
17+
'date.min': '{path} must be greater than or equal to {min}',
18+
19+
buffer: '{path} must be a buffer',
20+
boolean: '{path} must be a boolean',
21+
objectId: '{path} must be a objectId',
22+
map: '{path} must be a map',
23+
};

lib/utils.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const { lowerFirst, omit, pickBy, isUndefined } = require('lodash');
2+
3+
/**
4+
* Get mongoose validator error data.
5+
* @param {Error} error
6+
* @return {Object}
7+
*/
8+
exports.transformsError = function (error) {
9+
const o = {};
10+
o.path = error.path;
11+
o.value = error.value;
12+
o.stringValue = error.stringValue;
13+
o.message = error.message;
14+
15+
if (error.kind) {
16+
o.kind = lowerFirst(error.kind);
17+
}
18+
19+
if (typeof error.properties !== 'undefined') {
20+
o.properties = omit(error.properties, [
21+
'validator',
22+
'message',
23+
'type',
24+
'path',
25+
'value',
26+
]);
27+
28+
switch (o.kind) {
29+
case 'max': {
30+
if (typeof o.properties['max'] === 'number') {
31+
o.kind = 'number.max';
32+
} else {
33+
o.kind = 'date.max';
34+
}
35+
break;
36+
}
37+
case 'min': {
38+
if (typeof o.properties['min'] === 'number') {
39+
o.kind = 'number.min';
40+
} else {
41+
o.kind = 'date.min';
42+
}
43+
break;
44+
}
45+
default: {
46+
break;
47+
}
48+
}
49+
}
50+
51+
return pickBy(o, (o) => typeof o !== 'undefined');
52+
};
53+
54+
/**
55+
* Mongoose validator error
56+
*
57+
* @param {Object} attributes Error attributes
58+
* @return {Error}
59+
*
60+
* @example
61+
* error({ message, path, kind, value });
62+
*/
63+
exports.errorx = function ({ path, kind, value }) {
64+
return function (message) {
65+
let error = new Error(message);
66+
error.name = 'MongooseValidatorError';
67+
error.path = path;
68+
error.kind = kind;
69+
error.value = value;
70+
return error;
71+
};
72+
};

0 commit comments

Comments
 (0)