co.validation 2.5.2 → 2.6.2
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.
|
@@ -52,6 +52,13 @@ describe('createValidation', () => {
|
|
|
52
52
|
data: 0,
|
|
53
53
|
errors: undefined
|
|
54
54
|
});
|
|
55
|
+
expect((0, _.createValidation)({
|
|
56
|
+
type: 'required',
|
|
57
|
+
getMessage: () => 'Required'
|
|
58
|
+
})(false)).toEqual({
|
|
59
|
+
data: false,
|
|
60
|
+
errors: undefined
|
|
61
|
+
});
|
|
55
62
|
});
|
|
56
63
|
it('supports custom', () => {
|
|
57
64
|
const biggerThanX = (value, allValues) => {
|
|
@@ -405,6 +412,24 @@ describe('createValidation', () => {
|
|
|
405
412
|
data: 123,
|
|
406
413
|
errors: undefined
|
|
407
414
|
});
|
|
415
|
+
expect((0, _.createValidation)({
|
|
416
|
+
type: 'number'
|
|
417
|
+
})(null)).toEqual({
|
|
418
|
+
data: null,
|
|
419
|
+
errors: undefined
|
|
420
|
+
});
|
|
421
|
+
expect((0, _.createValidation)({
|
|
422
|
+
type: 'number'
|
|
423
|
+
})(undefined)).toEqual({
|
|
424
|
+
data: undefined,
|
|
425
|
+
errors: undefined
|
|
426
|
+
});
|
|
427
|
+
expect((0, _.createValidation)({
|
|
428
|
+
type: 'number'
|
|
429
|
+
})('')).toEqual({
|
|
430
|
+
data: undefined,
|
|
431
|
+
errors: ['Must be a number']
|
|
432
|
+
});
|
|
408
433
|
expect((0, _.createValidation)({
|
|
409
434
|
type: 'number',
|
|
410
435
|
getMessage: () => 'Number'
|
package/lib/validators.js
CHANGED
|
@@ -28,7 +28,7 @@ exports.custom = custom;
|
|
|
28
28
|
|
|
29
29
|
const required = ({
|
|
30
30
|
getMessage
|
|
31
|
-
}) => value => value || typeof value === 'number' ? {
|
|
31
|
+
}) => value => value || typeof value === 'number' || typeof value === 'boolean' ? {
|
|
32
32
|
data: value
|
|
33
33
|
} : {
|
|
34
34
|
errors: (0, _messageProcessor.default)(getMessage, '##required')
|
|
@@ -105,12 +105,16 @@ const normalizeNumber = value => {
|
|
|
105
105
|
return value;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
if (value === '' || value === null || value === undefined) {
|
|
109
|
+
return NaN;
|
|
110
|
+
}
|
|
111
|
+
|
|
108
112
|
return Number(value?.replace(',', '.'));
|
|
109
113
|
};
|
|
110
114
|
|
|
111
115
|
const number = ({
|
|
112
116
|
getMessage
|
|
113
|
-
}) => value => value && isNaN(normalizeNumber(value)) ? {
|
|
117
|
+
}) => value => value !== null && value !== undefined && isNaN(normalizeNumber(value)) ? {
|
|
114
118
|
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
115
119
|
} : {
|
|
116
120
|
data: value === null || value === undefined ? value : normalizeNumber(value)
|