co.validation 2.1.0 → 2.2.1
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.
|
@@ -354,6 +354,45 @@ describe('createValidation', () => {
|
|
|
354
354
|
errors: ['Must be a number']
|
|
355
355
|
});
|
|
356
356
|
});
|
|
357
|
+
it('supports minValue', () => {
|
|
358
|
+
expect((0, _.createValidation)({
|
|
359
|
+
type: 'minValue',
|
|
360
|
+
min: 1
|
|
361
|
+
})(0)).toEqual({
|
|
362
|
+
data: undefined,
|
|
363
|
+
errors: ['Must be at least 1']
|
|
364
|
+
});
|
|
365
|
+
expect((0, _.createValidation)({
|
|
366
|
+
type: 'minValue',
|
|
367
|
+
min: 1
|
|
368
|
+
})(-1)).toEqual({
|
|
369
|
+
data: undefined,
|
|
370
|
+
errors: ['Must be at least 1']
|
|
371
|
+
});
|
|
372
|
+
expect((0, _.createValidation)({
|
|
373
|
+
type: 'minValue',
|
|
374
|
+
min: 3,
|
|
375
|
+
getMessage: () => 'MinValue'
|
|
376
|
+
})(123)).toEqual({
|
|
377
|
+
data: 123,
|
|
378
|
+
errors: undefined
|
|
379
|
+
});
|
|
380
|
+
expect((0, _.createValidation)({
|
|
381
|
+
type: 'minValue',
|
|
382
|
+
min: 233,
|
|
383
|
+
getMessage: () => 'MinValue'
|
|
384
|
+
})(123)).toEqual({
|
|
385
|
+
data: undefined,
|
|
386
|
+
errors: ['MinValue']
|
|
387
|
+
});
|
|
388
|
+
expect((0, _.createValidation)({
|
|
389
|
+
type: 'minValue',
|
|
390
|
+
min: 333
|
|
391
|
+
})(22)).toEqual({
|
|
392
|
+
data: undefined,
|
|
393
|
+
errors: ['Must be at least 333']
|
|
394
|
+
});
|
|
395
|
+
});
|
|
357
396
|
it('supports string', () => {
|
|
358
397
|
expect((0, _.createValidation)({
|
|
359
398
|
type: 'string',
|
|
@@ -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
|
@@ -100,7 +100,7 @@ const createArrayValidation = rule => {
|
|
|
100
100
|
|
|
101
101
|
if (!Array.isArray(array)) {
|
|
102
102
|
return {
|
|
103
|
-
errors: (0, _messageProcessor.default)(rule.getMessage,
|
|
103
|
+
errors: (0, _messageProcessor.default)(rule.getMessage, `##array`, null, array)
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -147,7 +147,7 @@ const createObjectValidation = rule => {
|
|
|
147
147
|
|
|
148
148
|
if (!(0, _helpers.isPlainObject)(object)) {
|
|
149
149
|
return {
|
|
150
|
-
errors: (0, _messageProcessor.default)(rule.getMessage,
|
|
150
|
+
errors: (0, _messageProcessor.default)(rule.getMessage, `##object`, null, object)
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
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
|
@@ -19,7 +19,7 @@ const custom = ({
|
|
|
19
19
|
};
|
|
20
20
|
} catch (e) {
|
|
21
21
|
return {
|
|
22
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
22
|
+
errors: (0, _messageProcessor.default)(getMessage, '##custom', null, e)
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
};
|
|
@@ -31,7 +31,7 @@ const required = ({
|
|
|
31
31
|
}) => value => value || typeof value === 'number' ? {
|
|
32
32
|
data: value
|
|
33
33
|
} : {
|
|
34
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
34
|
+
errors: (0, _messageProcessor.default)(getMessage, '##required')
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
exports.required = required;
|
|
@@ -42,7 +42,7 @@ const equals = ({
|
|
|
42
42
|
}) => value => value === to ? {
|
|
43
43
|
data: value
|
|
44
44
|
} : {
|
|
45
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
45
|
+
errors: (0, _messageProcessor.default)(getMessage, `##equals`, string => string.replace(/{to}/g, to), to)
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
exports.equals = equals;
|
|
@@ -52,7 +52,7 @@ const maxLength = ({
|
|
|
52
52
|
getMessage
|
|
53
53
|
}) => value => {
|
|
54
54
|
return value && value.length > max ? {
|
|
55
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
55
|
+
errors: (0, _messageProcessor.default)(getMessage, `##maxLength`, string => string.replace(/{max}/g, max), max)
|
|
56
56
|
} : {
|
|
57
57
|
data: value
|
|
58
58
|
};
|
|
@@ -64,7 +64,7 @@ const minLength = ({
|
|
|
64
64
|
min,
|
|
65
65
|
getMessage
|
|
66
66
|
}) => value => value && value.length < min ? {
|
|
67
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
67
|
+
errors: (0, _messageProcessor.default)(getMessage, `##minLength`, string => string.replace(/{min}/g, min), min)
|
|
68
68
|
} : {
|
|
69
69
|
data: value
|
|
70
70
|
};
|
|
@@ -74,7 +74,7 @@ exports.minLength = minLength;
|
|
|
74
74
|
const number = ({
|
|
75
75
|
getMessage
|
|
76
76
|
}) => value => value && isNaN(Number(value)) ? {
|
|
77
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
77
|
+
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
78
78
|
} : {
|
|
79
79
|
data: value === null || value === undefined ? value : Number(value)
|
|
80
80
|
};
|
|
@@ -84,10 +84,10 @@ exports.number = number;
|
|
|
84
84
|
const minValue = ({
|
|
85
85
|
min,
|
|
86
86
|
getMessage
|
|
87
|
-
}) => value => value && value < min ? {
|
|
88
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
87
|
+
}) => value => !isNaN(value) && Number(value) < min ? {
|
|
88
|
+
errors: (0, _messageProcessor.default)(getMessage, `##minValue`, string => string.replace(/{min}/g, min), min)
|
|
89
89
|
} : {
|
|
90
|
-
data: value
|
|
90
|
+
data: Number(value)
|
|
91
91
|
};
|
|
92
92
|
|
|
93
93
|
exports.minValue = minValue;
|
|
@@ -95,10 +95,10 @@ exports.minValue = minValue;
|
|
|
95
95
|
const maxValue = ({
|
|
96
96
|
max,
|
|
97
97
|
getMessage
|
|
98
|
-
}) => value => value && value > max ? {
|
|
99
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
98
|
+
}) => value => !isNaN(value) && Number(value) > max ? {
|
|
99
|
+
errors: (0, _messageProcessor.default)(getMessage, `##maxValue`, string => string.replace(/{max}/g, max), max)
|
|
100
100
|
} : {
|
|
101
|
-
data: value
|
|
101
|
+
data: Number(value)
|
|
102
102
|
};
|
|
103
103
|
|
|
104
104
|
exports.maxValue = maxValue;
|
|
@@ -106,7 +106,7 @@ exports.maxValue = maxValue;
|
|
|
106
106
|
const email = ({
|
|
107
107
|
getMessage
|
|
108
108
|
}) => value => value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ? {
|
|
109
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
109
|
+
errors: (0, _messageProcessor.default)(getMessage, '##email')
|
|
110
110
|
} : {
|
|
111
111
|
data: value
|
|
112
112
|
};
|
|
@@ -116,7 +116,7 @@ exports.email = email;
|
|
|
116
116
|
const alphanumeric = ({
|
|
117
117
|
getMessage
|
|
118
118
|
}) => value => value && /[^a-zA-Z0-9]/i.test(value) ? {
|
|
119
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
119
|
+
errors: (0, _messageProcessor.default)(getMessage, '##alphanumeric')
|
|
120
120
|
} : {
|
|
121
121
|
data: value
|
|
122
122
|
};
|
|
@@ -126,7 +126,7 @@ exports.alphanumeric = alphanumeric;
|
|
|
126
126
|
const string = ({
|
|
127
127
|
getMessage
|
|
128
128
|
}) => value => value && typeof value !== 'string' ? {
|
|
129
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
129
|
+
errors: (0, _messageProcessor.default)(getMessage, '##string')
|
|
130
130
|
} : {
|
|
131
131
|
data: value
|
|
132
132
|
};
|
|
@@ -148,7 +148,7 @@ const boolean = ({
|
|
|
148
148
|
'false': false
|
|
149
149
|
}[value.toLowerCase()]
|
|
150
150
|
} : {
|
|
151
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
151
|
+
errors: (0, _messageProcessor.default)(getMessage, '##boolean')
|
|
152
152
|
};
|
|
153
153
|
|
|
154
154
|
exports.boolean = boolean;
|
|
@@ -157,7 +157,7 @@ const equalsToField = ({
|
|
|
157
157
|
field,
|
|
158
158
|
getMessage
|
|
159
159
|
}) => (value, allValues) => value !== allValues[field] ? {
|
|
160
|
-
errors: (0, _messageProcessor.default)(getMessage,
|
|
160
|
+
errors: (0, _messageProcessor.default)(getMessage, `##equalsToField`, string => string.replace(/{field}/g, field), field)
|
|
161
161
|
} : {
|
|
162
162
|
data: value
|
|
163
163
|
};
|
|
@@ -168,7 +168,7 @@ const regex = ({
|
|
|
168
168
|
regex,
|
|
169
169
|
getMessage
|
|
170
170
|
}) => value => value && !regex.test(value) ? {
|
|
171
|
-
errors: (0, _messageProcessor.default)(getMessage, '
|
|
171
|
+
errors: (0, _messageProcessor.default)(getMessage, '##regex')
|
|
172
172
|
} : {
|
|
173
173
|
data: value
|
|
174
174
|
};
|