co.validation 2.5.1 → 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.
|
@@ -382,19 +382,47 @@ describe('createValidation', () => {
|
|
|
382
382
|
});
|
|
383
383
|
it('supports number', () => {
|
|
384
384
|
expect((0, _.createValidation)({
|
|
385
|
-
type: 'number'
|
|
386
|
-
getMessage: () => 'Number'
|
|
385
|
+
type: 'number'
|
|
387
386
|
})('1234')).toEqual({
|
|
388
387
|
data: 1234,
|
|
389
388
|
errors: undefined
|
|
390
389
|
});
|
|
391
390
|
expect((0, _.createValidation)({
|
|
392
|
-
type: 'number'
|
|
393
|
-
|
|
391
|
+
type: 'number'
|
|
392
|
+
})('1234,5')).toEqual({
|
|
393
|
+
data: 1234.5,
|
|
394
|
+
errors: undefined
|
|
395
|
+
});
|
|
396
|
+
expect((0, _.createValidation)({
|
|
397
|
+
type: 'number'
|
|
398
|
+
})('1234.5')).toEqual({
|
|
399
|
+
data: 1234.5,
|
|
400
|
+
errors: undefined
|
|
401
|
+
});
|
|
402
|
+
expect((0, _.createValidation)({
|
|
403
|
+
type: 'number'
|
|
394
404
|
})(123)).toEqual({
|
|
395
405
|
data: 123,
|
|
396
406
|
errors: undefined
|
|
397
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
|
+
});
|
|
398
426
|
expect((0, _.createValidation)({
|
|
399
427
|
type: 'number',
|
|
400
428
|
getMessage: () => 'Number'
|
package/lib/validators.js
CHANGED
|
@@ -100,12 +100,24 @@ const minLength = ({
|
|
|
100
100
|
|
|
101
101
|
exports.minLength = minLength;
|
|
102
102
|
|
|
103
|
+
const normalizeNumber = value => {
|
|
104
|
+
if (typeof value === 'number') {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (value === '' || value === null || value === undefined) {
|
|
109
|
+
return NaN;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Number(value?.replace(',', '.'));
|
|
113
|
+
};
|
|
114
|
+
|
|
103
115
|
const number = ({
|
|
104
116
|
getMessage
|
|
105
|
-
}) => value => value && isNaN(
|
|
117
|
+
}) => value => value !== null && value !== undefined && isNaN(normalizeNumber(value)) ? {
|
|
106
118
|
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
107
119
|
} : {
|
|
108
|
-
data: value === null || value === undefined ? value :
|
|
120
|
+
data: value === null || value === undefined ? value : normalizeNumber(value)
|
|
109
121
|
};
|
|
110
122
|
|
|
111
123
|
exports.number = number;
|