co.validation 1.0.0-RC.7 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -39,6 +39,45 @@ describe('createValidation', () => {
|
|
|
39
39
|
errors: undefined
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
+
it('supports custom', () => {
|
|
43
|
+
const biggerThanX = (value, allValues) => {
|
|
44
|
+
if (value < allValues.x) {
|
|
45
|
+
throw 'bigger';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
expect((0, _.createValidation)({
|
|
52
|
+
type: 'custom',
|
|
53
|
+
validator: biggerThanX
|
|
54
|
+
})(5, {
|
|
55
|
+
x: 10
|
|
56
|
+
})).toEqual({
|
|
57
|
+
data: undefined,
|
|
58
|
+
errors: ['Error']
|
|
59
|
+
});
|
|
60
|
+
expect((0, _.createValidation)({
|
|
61
|
+
type: 'custom',
|
|
62
|
+
validator: biggerThanX,
|
|
63
|
+
getMessage: bigger => bigger
|
|
64
|
+
})(5, {
|
|
65
|
+
x: 10
|
|
66
|
+
})).toEqual({
|
|
67
|
+
data: undefined,
|
|
68
|
+
errors: ['bigger']
|
|
69
|
+
});
|
|
70
|
+
expect((0, _.createValidation)({
|
|
71
|
+
type: 'custom',
|
|
72
|
+
validator: biggerThanX,
|
|
73
|
+
getMessage: () => 'Required'
|
|
74
|
+
})(50, {
|
|
75
|
+
x: 10
|
|
76
|
+
})).toEqual({
|
|
77
|
+
data: 50,
|
|
78
|
+
errors: undefined
|
|
79
|
+
});
|
|
80
|
+
});
|
|
42
81
|
it('supports equals', () => {
|
|
43
82
|
expect((0, _.createValidation)({
|
|
44
83
|
type: 'equals',
|
|
@@ -370,21 +409,21 @@ describe('createValidation', () => {
|
|
|
370
409
|
type: 'boolean',
|
|
371
410
|
getMessage: () => 'Boolean'
|
|
372
411
|
})('true')).toEqual({
|
|
373
|
-
data:
|
|
412
|
+
data: true,
|
|
374
413
|
errors: undefined
|
|
375
414
|
});
|
|
376
415
|
expect((0, _.createValidation)({
|
|
377
416
|
type: 'boolean',
|
|
378
417
|
getMessage: () => 'Boolean'
|
|
379
418
|
})('false')).toEqual({
|
|
380
|
-
data:
|
|
419
|
+
data: false,
|
|
381
420
|
errors: undefined
|
|
382
421
|
});
|
|
383
422
|
expect((0, _.createValidation)({
|
|
384
423
|
type: 'boolean',
|
|
385
424
|
getMessage: () => 'Boolean'
|
|
386
425
|
})('FAlse')).toEqual({
|
|
387
|
-
data:
|
|
426
|
+
data: false,
|
|
388
427
|
errors: undefined
|
|
389
428
|
});
|
|
390
429
|
expect((0, _.createValidation)({
|
|
@@ -83,17 +83,43 @@ const registrationFormValidation = (0, _.createValidation)({
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
86
|
+
const ukErrors = {
|
|
87
|
+
'##object': 'Має бути об\'єкт',
|
|
88
|
+
'##array': 'Має бути масив',
|
|
89
|
+
'##required': 'Обов\'язково',
|
|
90
|
+
'##equals': `Має дорівнювати {to}`,
|
|
91
|
+
'##maxLength': `Має бути {max} символів або менше`,
|
|
92
|
+
'##minLength': `Має бути принаймні {min} символів`,
|
|
93
|
+
'##number': 'Має бути число',
|
|
94
|
+
'##minValue': `Має бути принаймні {min}`,
|
|
95
|
+
'##maxValue': `Має бути не більше ніж {max}`,
|
|
96
|
+
'##email': 'Недійсний формат електронної пошти',
|
|
97
|
+
'##alphanumeric': 'Тільки буквено-цифрові символи',
|
|
98
|
+
'##string': 'Має бути рядок',
|
|
99
|
+
'##boolean': 'Має бути логічним значенням',
|
|
100
|
+
'##equalsToField': 'Має дорівнювати {field}',
|
|
101
|
+
'##regex': 'Недійсний формат',
|
|
102
|
+
'##custom': 'Помилка'
|
|
103
|
+
};
|
|
104
|
+
describe('registrationFormValidation in language=' + encodeURI, () => {
|
|
105
|
+
['en', 'uk', 'en'].map(lang => {
|
|
106
|
+
it('single invalid item creates error without array in lang' + lang, () => {
|
|
107
|
+
if (lang === 'en') {
|
|
108
|
+
(0, _.resetDefaultMessages)();
|
|
109
|
+
} else {
|
|
110
|
+
(0, _.setDefaultMessages)(ukErrors);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const validatedObject = {};
|
|
114
|
+
const validationResult = registrationFormValidation(validatedObject);
|
|
115
|
+
expect(validationResult.errors).toEqual({
|
|
116
|
+
firstName: [lang === 'en' ? 'Required' : 'Обов\'язково'],
|
|
117
|
+
lastName: [lang === 'en' ? 'Required' : 'Обов\'язково'],
|
|
118
|
+
email: [lang === 'en' ? 'Required' : 'Обов\'язково'],
|
|
119
|
+
age: [lang === 'en' ? 'Required' : 'Обов\'язково']
|
|
120
|
+
});
|
|
121
|
+
expect(validationResult.data).toEqual(undefined);
|
|
95
122
|
});
|
|
96
|
-
expect(validationResult.data).toEqual(undefined);
|
|
97
123
|
});
|
|
98
124
|
it('single invalid item creates error with empty array', () => {
|
|
99
125
|
const validatedObject = {
|
|
@@ -147,32 +173,7 @@ describe('registrationFormValidation', () => {
|
|
|
147
173
|
toys: [1, true, 'test']
|
|
148
174
|
}]
|
|
149
175
|
};
|
|
150
|
-
const validationResult = registrationFormValidation(validatedObject);
|
|
151
|
-
// "data": {
|
|
152
|
-
// "firstName":"firstName",
|
|
153
|
-
// "lastName":"lastName",
|
|
154
|
-
// "email":"test@mail.com",
|
|
155
|
-
// "hobbies":[{
|
|
156
|
-
// "title":"title"
|
|
157
|
-
// }, {
|
|
158
|
-
// "title":"title",
|
|
159
|
-
// "description":"description",
|
|
160
|
-
// "toys":[1,true,"test"]
|
|
161
|
-
// }]
|
|
162
|
-
// },
|
|
163
|
-
// "errors": {
|
|
164
|
-
// "hobbies": [
|
|
165
|
-
// ["Must be an object"],
|
|
166
|
-
// null,
|
|
167
|
-
// [{
|
|
168
|
-
// "toys":[["Required"],null]
|
|
169
|
-
// }],
|
|
170
|
-
// [{"title":["Required"]}],
|
|
171
|
-
// null
|
|
172
|
-
// ]
|
|
173
|
-
// }
|
|
174
|
-
// }
|
|
175
|
-
|
|
176
|
+
const validationResult = registrationFormValidation(validatedObject);
|
|
176
177
|
expect(validationResult.data).toEqual({
|
|
177
178
|
firstName: 'firstName',
|
|
178
179
|
lastName: 'lastName',
|
package/lib/createValidation.js
CHANGED
|
@@ -20,6 +20,9 @@ const createValidation = rule => {
|
|
|
20
20
|
case 'required':
|
|
21
21
|
return (0, _validators.required)(rule);
|
|
22
22
|
|
|
23
|
+
case 'custom':
|
|
24
|
+
return (0, _validators.custom)(rule);
|
|
25
|
+
|
|
23
26
|
case 'equals':
|
|
24
27
|
return (0, _validators.equals)(rule);
|
|
25
28
|
|
|
@@ -97,7 +100,7 @@ const createArrayValidation = rule => {
|
|
|
97
100
|
|
|
98
101
|
if (!Array.isArray(array)) {
|
|
99
102
|
return {
|
|
100
|
-
errors: (0, _messageProcessor.default)(rule.getMessage,
|
|
103
|
+
errors: (0, _messageProcessor.default)(rule.getMessage, `##array`, null, array)
|
|
101
104
|
};
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -144,7 +147,7 @@ const createObjectValidation = rule => {
|
|
|
144
147
|
|
|
145
148
|
if (!(0, _helpers.isPlainObject)(object)) {
|
|
146
149
|
return {
|
|
147
|
-
errors: (0, _messageProcessor.default)(rule.getMessage,
|
|
150
|
+
errors: (0, _messageProcessor.default)(rule.getMessage, `##object`, null, object)
|
|
148
151
|
};
|
|
149
152
|
}
|
|
150
153
|
|
package/lib/index.js
CHANGED
|
@@ -9,10 +9,24 @@ Object.defineProperty(exports, "createValidation", {
|
|
|
9
9
|
return _createValidation.default;
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
|
+
Object.defineProperty(exports, "resetDefaultMessages", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _messageProcessor.resetDefaultMessages;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "setDefaultMessages", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _messageProcessor.setDefaultMessages;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
12
24
|
exports.createReduxFormValidation = void 0;
|
|
13
25
|
|
|
14
26
|
var _createValidation = _interopRequireDefault(require("./createValidation"));
|
|
15
27
|
|
|
28
|
+
var _messageProcessor = require("./messageProcessor");
|
|
29
|
+
|
|
16
30
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
31
|
|
|
18
32
|
const createReduxFormValidation = rules => {
|
package/lib/messageProcessor.js
CHANGED
|
@@ -3,13 +3,45 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = void 0;
|
|
6
|
+
exports.default = exports.resetDefaultMessages = exports.setDefaultMessages = void 0;
|
|
7
|
+
const defaultDefaultMessages = {
|
|
8
|
+
'##object': 'Must be an object',
|
|
9
|
+
'##array': 'Must be an array',
|
|
10
|
+
'##required': 'Required',
|
|
11
|
+
'##equals': `Must be equal to {to}`,
|
|
12
|
+
'##maxLength': `Must be {max} characters or less`,
|
|
13
|
+
'##minLength': `Must be at least {min} characters`,
|
|
14
|
+
'##number': 'Must be a number',
|
|
15
|
+
'##minValue': `Must be at least {min}`,
|
|
16
|
+
'##maxValue': `Must be not more than {max}`,
|
|
17
|
+
'##email': 'Invalid email format',
|
|
18
|
+
'##alphanumeric': 'Only alphanumeric characters',
|
|
19
|
+
'##string': 'Must be a string',
|
|
20
|
+
'##boolean': 'Must be a boolean',
|
|
21
|
+
'##equalsToField': 'Must be equal to {field}',
|
|
22
|
+
'##regex': 'Invalid format',
|
|
23
|
+
'##custom': 'Error'
|
|
24
|
+
};
|
|
25
|
+
const currentDefaultMessages = { ...defaultDefaultMessages
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const setDefaultMessages = defaultMessages => {
|
|
29
|
+
Object.assign(currentDefaultMessages, defaultMessages);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
exports.setDefaultMessages = setDefaultMessages;
|
|
33
|
+
|
|
34
|
+
const resetDefaultMessages = () => {
|
|
35
|
+
Object.assign(currentDefaultMessages, defaultDefaultMessages);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.resetDefaultMessages = resetDefaultMessages;
|
|
7
39
|
|
|
8
|
-
var _default = (getMessage,
|
|
40
|
+
var _default = (getMessage, defaultValueCode, stringFormatter, ...args) => {
|
|
9
41
|
let result = null;
|
|
10
42
|
|
|
11
43
|
if (!getMessage) {
|
|
12
|
-
result =
|
|
44
|
+
result = currentDefaultMessages[defaultValueCode];
|
|
13
45
|
} else if (typeof getMessage === 'function') {
|
|
14
46
|
result = getMessage(...args);
|
|
15
47
|
|
package/lib/validators.js
CHANGED
|
@@ -3,18 +3,35 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.regex = exports.equalsToField = exports.boolean = exports.string = exports.alphanumeric = exports.email = exports.maxValue = exports.minValue = exports.number = exports.minLength = exports.maxLength = exports.equals = exports.required = void 0;
|
|
6
|
+
exports.regex = exports.equalsToField = exports.boolean = exports.string = exports.alphanumeric = exports.email = exports.maxValue = exports.minValue = exports.number = exports.minLength = exports.maxLength = exports.equals = exports.required = exports.custom = void 0;
|
|
7
7
|
|
|
8
8
|
var _messageProcessor = _interopRequireDefault(require("./messageProcessor"));
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
+
const custom = ({
|
|
13
|
+
validator,
|
|
14
|
+
getMessage
|
|
15
|
+
}) => (value, allValues) => {
|
|
16
|
+
try {
|
|
17
|
+
return {
|
|
18
|
+
data: validator(value, allValues)
|
|
19
|
+
};
|
|
20
|
+
} catch (e) {
|
|
21
|
+
return {
|
|
22
|
+
errors: (0, _messageProcessor.default)(getMessage, '##custom', null, e)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.custom = custom;
|
|
28
|
+
|
|
12
29
|
const required = ({
|
|
13
30
|
getMessage
|
|
14
31
|
}) => value => value || typeof value === 'number' ? {
|
|
15
32
|
data: value
|
|
16
33
|
} : {
|
|
17
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
34
|
+
errors: (0, _messageProcessor.default)(getMessage, '##required')
|
|
18
35
|
};
|
|
19
36
|
|
|
20
37
|
exports.required = required;
|
|
@@ -25,7 +42,7 @@ const equals = ({
|
|
|
25
42
|
}) => value => value === to ? {
|
|
26
43
|
data: value
|
|
27
44
|
} : {
|
|
28
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
45
|
+
errors: (0, _messageProcessor.default)(getMessage, `##equals`, string => string.replace(/{to}/g, to), to)
|
|
29
46
|
};
|
|
30
47
|
|
|
31
48
|
exports.equals = equals;
|
|
@@ -35,7 +52,7 @@ const maxLength = ({
|
|
|
35
52
|
getMessage
|
|
36
53
|
}) => value => {
|
|
37
54
|
return value && value.length > max ? {
|
|
38
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
55
|
+
errors: (0, _messageProcessor.default)(getMessage, `##maxLength`, string => string.replace(/{max}/g, max), max)
|
|
39
56
|
} : {
|
|
40
57
|
data: value
|
|
41
58
|
};
|
|
@@ -47,7 +64,7 @@ const minLength = ({
|
|
|
47
64
|
min,
|
|
48
65
|
getMessage
|
|
49
66
|
}) => value => value && value.length < min ? {
|
|
50
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
67
|
+
errors: (0, _messageProcessor.default)(getMessage, `##minLength`, string => string.replace(/{min}/g, min), min)
|
|
51
68
|
} : {
|
|
52
69
|
data: value
|
|
53
70
|
};
|
|
@@ -57,7 +74,7 @@ exports.minLength = minLength;
|
|
|
57
74
|
const number = ({
|
|
58
75
|
getMessage
|
|
59
76
|
}) => value => value && isNaN(Number(value)) ? {
|
|
60
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
77
|
+
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
61
78
|
} : {
|
|
62
79
|
data: value === null || value === undefined ? value : Number(value)
|
|
63
80
|
};
|
|
@@ -68,7 +85,7 @@ const minValue = ({
|
|
|
68
85
|
min,
|
|
69
86
|
getMessage
|
|
70
87
|
}) => value => value && value < min ? {
|
|
71
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
88
|
+
errors: (0, _messageProcessor.default)(getMessage, `##minValue`, string => string.replace(/{min}/g, min), min)
|
|
72
89
|
} : {
|
|
73
90
|
data: value
|
|
74
91
|
};
|
|
@@ -79,7 +96,7 @@ const maxValue = ({
|
|
|
79
96
|
max,
|
|
80
97
|
getMessage
|
|
81
98
|
}) => value => value && value > max ? {
|
|
82
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
99
|
+
errors: (0, _messageProcessor.default)(getMessage, `##maxValue`, string => string.replace(/{max}/g, max), max)
|
|
83
100
|
} : {
|
|
84
101
|
data: value
|
|
85
102
|
};
|
|
@@ -89,7 +106,7 @@ exports.maxValue = maxValue;
|
|
|
89
106
|
const email = ({
|
|
90
107
|
getMessage
|
|
91
108
|
}) => value => value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ? {
|
|
92
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
109
|
+
errors: (0, _messageProcessor.default)(getMessage, '##email')
|
|
93
110
|
} : {
|
|
94
111
|
data: value
|
|
95
112
|
};
|
|
@@ -99,7 +116,7 @@ exports.email = email;
|
|
|
99
116
|
const alphanumeric = ({
|
|
100
117
|
getMessage
|
|
101
118
|
}) => value => value && /[^a-zA-Z0-9]/i.test(value) ? {
|
|
102
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
119
|
+
errors: (0, _messageProcessor.default)(getMessage, '##alphanumeric')
|
|
103
120
|
} : {
|
|
104
121
|
data: value
|
|
105
122
|
};
|
|
@@ -109,7 +126,7 @@ exports.alphanumeric = alphanumeric;
|
|
|
109
126
|
const string = ({
|
|
110
127
|
getMessage
|
|
111
128
|
}) => value => value && typeof value !== 'string' ? {
|
|
112
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
129
|
+
errors: (0, _messageProcessor.default)(getMessage, '##string')
|
|
113
130
|
} : {
|
|
114
131
|
data: value
|
|
115
132
|
};
|
|
@@ -118,13 +135,20 @@ exports.string = string;
|
|
|
118
135
|
|
|
119
136
|
const boolean = ({
|
|
120
137
|
getMessage
|
|
121
|
-
}) => value => value === undefined || value === null
|
|
138
|
+
}) => value => value === undefined || value === null ? {
|
|
139
|
+
data: value
|
|
140
|
+
} : typeof value === 'boolean' ? {
|
|
141
|
+
data: value
|
|
142
|
+
} : typeof value === 'string' && {
|
|
122
143
|
'true': true,
|
|
123
144
|
'false': true
|
|
124
145
|
}[value.toLowerCase()] ? {
|
|
125
|
-
data:
|
|
146
|
+
data: {
|
|
147
|
+
'true': true,
|
|
148
|
+
'false': false
|
|
149
|
+
}[value.toLowerCase()]
|
|
126
150
|
} : {
|
|
127
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
151
|
+
errors: (0, _messageProcessor.default)(getMessage, '##boolean')
|
|
128
152
|
};
|
|
129
153
|
|
|
130
154
|
exports.boolean = boolean;
|
|
@@ -133,7 +157,7 @@ const equalsToField = ({
|
|
|
133
157
|
field,
|
|
134
158
|
getMessage
|
|
135
159
|
}) => (value, allValues) => value !== allValues[field] ? {
|
|
136
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
160
|
+
errors: (0, _messageProcessor.default)(getMessage, `##equalsToField`, string => string.replace(/{field}/g, field), field)
|
|
137
161
|
} : {
|
|
138
162
|
data: value
|
|
139
163
|
};
|
|
@@ -144,7 +168,7 @@ const regex = ({
|
|
|
144
168
|
regex,
|
|
145
169
|
getMessage
|
|
146
170
|
}) => value => value && !regex.test(value) ? {
|
|
147
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
171
|
+
errors: (0, _messageProcessor.default)(getMessage, '##regex')
|
|
148
172
|
} : {
|
|
149
173
|
data: value
|
|
150
174
|
};
|