co.validation 2.5.1 → 2.5.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.
|
@@ -382,15 +382,25 @@ 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
|
package/lib/validators.js
CHANGED
|
@@ -100,12 +100,20 @@ 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
|
+
return Number(value?.replace(',', '.'));
|
|
109
|
+
};
|
|
110
|
+
|
|
103
111
|
const number = ({
|
|
104
112
|
getMessage
|
|
105
|
-
}) => value => value && isNaN(
|
|
113
|
+
}) => value => value && isNaN(normalizeNumber(value)) ? {
|
|
106
114
|
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
107
115
|
} : {
|
|
108
|
-
data: value === null || value === undefined ? value :
|
|
116
|
+
data: value === null || value === undefined ? value : normalizeNumber(value)
|
|
109
117
|
};
|
|
110
118
|
|
|
111
119
|
exports.number = number;
|