-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·113 lines (88 loc) · 2.88 KB
/
Copy pathindex.js
File metadata and controls
executable file
·113 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Helpers
// stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript
var isArray = Array.isArray ? Array.isArray
: function(array) { return array.constructor === Array; };
// stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string
var startsWith = function(str, pattern) {
return str.indexOf(pattern) === 0;
};
// stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
// Package
function joiErrorsForForms(format, options) {
var convert = useNothing;
if (typeof options === 'string') { convert = useFixedMsg; }
if (typeof options === 'object') {
convert = isArray(options) ? useJoiMsg : useJoiType;
}
return function joiErrorsForFormsInner(joiErrs) {
if (!joiErrs || !joiErrs.details) {
return null;
}
var newErrs = {};
for (var i = 0, leni = joiErrs.details.length; i < leni; i++) {
var detail = joiErrs.details[i];
var path = detail.path;
switch (format) {
case 'mongoose':
newErrs[path] = {
message: convert(detail, options),
name: 'ValidatorError',
path: path,
type: detail.type
};
break;
default:
newErrs[path] = convert(detail, options);
}
}
return newErrs;
};
}
function useNothing(detail) {
return detail.message;
}
function useFixedMsg(detail, options) {
return substituteContext(detail.context, options);
}
function useJoiMsg(detail, options) {
var detailMessage = detail.message;
for (var i = 0, leni = options.length; i < leni; i++) {
var option = options[i];
var regex = option.regex;
if (typeof regex === 'string'
? detailMessage.indexOf(regex) !== -1
: detailMessage.search(regex) !== -1) {
return substituteContext(detail.context, option.message);
}
}
return detailMessage;
}
function useJoiType(detail, options) {
var context = detail.context;
var message;
if (options[detail.type]) {
message = options[detail.type](context)
}
if (!message) {
message = detail.message;
}
return substituteContext(context, message);
}
function substituteContext(context, message) {
for (var prop in context) {
if (context.hasOwnProperty(prop)) {
message = replaceAll(message, '${' + prop + '}', '' + context[prop]);
}
}
return message;
}
module.exports = {
form: function form(options) { return joiErrorsForForms('form', options); },
mongoose: function mongoose(options) { return joiErrorsForForms('mongoose', options); }
};