co.validation 2.5.2 → 2.6.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.
|
@@ -405,6 +405,24 @@ describe('createValidation', () => {
|
|
|
405
405
|
data: 123,
|
|
406
406
|
errors: undefined
|
|
407
407
|
});
|
|
408
|
+
expect((0, _.createValidation)({
|
|
409
|
+
type: 'number'
|
|
410
|
+
})(null)).toEqual({
|
|
411
|
+
data: null,
|
|
412
|
+
errors: undefined
|
|
413
|
+
});
|
|
414
|
+
expect((0, _.createValidation)({
|
|
415
|
+
type: 'number'
|
|
416
|
+
})(undefined)).toEqual({
|
|
417
|
+
data: undefined,
|
|
418
|
+
errors: undefined
|
|
419
|
+
});
|
|
420
|
+
expect((0, _.createValidation)({
|
|
421
|
+
type: 'number'
|
|
422
|
+
})('')).toEqual({
|
|
423
|
+
data: undefined,
|
|
424
|
+
errors: ['Must be a number']
|
|
425
|
+
});
|
|
408
426
|
expect((0, _.createValidation)({
|
|
409
427
|
type: 'number',
|
|
410
428
|
getMessage: () => 'Number'
|
package/lib/validators.js
CHANGED
|
@@ -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)
|