co.validation 2.6.2 → 2.7.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.
|
@@ -427,8 +427,7 @@ describe('createValidation', () => {
|
|
|
427
427
|
expect((0, _.createValidation)({
|
|
428
428
|
type: 'number'
|
|
429
429
|
})('')).toEqual({
|
|
430
|
-
data: undefined
|
|
431
|
-
errors: ['Must be a number']
|
|
430
|
+
data: undefined
|
|
432
431
|
});
|
|
433
432
|
expect((0, _.createValidation)({
|
|
434
433
|
type: 'number',
|
|
@@ -445,6 +444,12 @@ describe('createValidation', () => {
|
|
|
445
444
|
});
|
|
446
445
|
});
|
|
447
446
|
it('supports minValue', () => {
|
|
447
|
+
expect((0, _.createValidation)({
|
|
448
|
+
type: 'minValue',
|
|
449
|
+
min: 1
|
|
450
|
+
})('')).toEqual({
|
|
451
|
+
data: undefined
|
|
452
|
+
});
|
|
448
453
|
expect((0, _.createValidation)({
|
|
449
454
|
type: 'minValue',
|
|
450
455
|
min: 1
|
|
@@ -483,6 +488,51 @@ describe('createValidation', () => {
|
|
|
483
488
|
errors: ['Must be at least 333']
|
|
484
489
|
});
|
|
485
490
|
});
|
|
491
|
+
it('supports maxValue', () => {
|
|
492
|
+
expect((0, _.createValidation)({
|
|
493
|
+
type: 'maxValue',
|
|
494
|
+
max: 10
|
|
495
|
+
})('')).toEqual({
|
|
496
|
+
data: undefined
|
|
497
|
+
});
|
|
498
|
+
expect((0, _.createValidation)({
|
|
499
|
+
type: 'maxValue',
|
|
500
|
+
max: 10
|
|
501
|
+
})(11)).toEqual({
|
|
502
|
+
data: undefined,
|
|
503
|
+
errors: ['Must be not more than 10']
|
|
504
|
+
});
|
|
505
|
+
expect((0, _.createValidation)({
|
|
506
|
+
type: 'maxValue',
|
|
507
|
+
max: 10
|
|
508
|
+
})(15)).toEqual({
|
|
509
|
+
data: undefined,
|
|
510
|
+
errors: ['Must be not more than 10']
|
|
511
|
+
});
|
|
512
|
+
expect((0, _.createValidation)({
|
|
513
|
+
type: 'maxValue',
|
|
514
|
+
max: 50,
|
|
515
|
+
getMessage: () => 'MaxValue'
|
|
516
|
+
})(25)).toEqual({
|
|
517
|
+
data: 25,
|
|
518
|
+
errors: undefined
|
|
519
|
+
});
|
|
520
|
+
expect((0, _.createValidation)({
|
|
521
|
+
type: 'maxValue',
|
|
522
|
+
max: 5,
|
|
523
|
+
getMessage: () => 'MaxValue'
|
|
524
|
+
})(10)).toEqual({
|
|
525
|
+
data: undefined,
|
|
526
|
+
errors: ['MaxValue']
|
|
527
|
+
});
|
|
528
|
+
expect((0, _.createValidation)({
|
|
529
|
+
type: 'maxValue',
|
|
530
|
+
max: 100
|
|
531
|
+
})(200)).toEqual({
|
|
532
|
+
data: undefined,
|
|
533
|
+
errors: ['Must be not more than 100']
|
|
534
|
+
});
|
|
535
|
+
});
|
|
486
536
|
it('supports string', () => {
|
|
487
537
|
expect((0, _.createValidation)({
|
|
488
538
|
type: 'string',
|
package/lib/validators.js
CHANGED
|
@@ -105,16 +105,14 @@ const normalizeNumber = value => {
|
|
|
105
105
|
return value;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
if (value === '' || value === null || value === undefined) {
|
|
109
|
-
return NaN;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
108
|
return Number(value?.replace(',', '.'));
|
|
113
109
|
};
|
|
114
110
|
|
|
115
111
|
const number = ({
|
|
116
112
|
getMessage
|
|
117
|
-
}) => value => value
|
|
113
|
+
}) => value => value === '' ? {
|
|
114
|
+
data: undefined
|
|
115
|
+
} : value !== null && value !== undefined && isNaN(normalizeNumber(value)) ? {
|
|
118
116
|
errors: (0, _messageProcessor.default)(getMessage, '##number')
|
|
119
117
|
} : {
|
|
120
118
|
data: value === null || value === undefined ? value : normalizeNumber(value)
|
|
@@ -125,7 +123,9 @@ exports.number = number;
|
|
|
125
123
|
const minValue = ({
|
|
126
124
|
min,
|
|
127
125
|
getMessage
|
|
128
|
-
}) => value =>
|
|
126
|
+
}) => value => value === '' ? {
|
|
127
|
+
data: undefined
|
|
128
|
+
} : !isNaN(value) && Number(value) < min ? {
|
|
129
129
|
errors: (0, _messageProcessor.default)(getMessage, `##minValue`, string => string.replace(/{min}/g, min), min)
|
|
130
130
|
} : {
|
|
131
131
|
data: Number(value)
|
|
@@ -136,7 +136,9 @@ exports.minValue = minValue;
|
|
|
136
136
|
const maxValue = ({
|
|
137
137
|
max,
|
|
138
138
|
getMessage
|
|
139
|
-
}) => value =>
|
|
139
|
+
}) => value => value === '' ? {
|
|
140
|
+
data: undefined
|
|
141
|
+
} : !isNaN(value) && Number(value) > max ? {
|
|
140
142
|
errors: (0, _messageProcessor.default)(getMessage, `##maxValue`, string => string.replace(/{max}/g, max), max)
|
|
141
143
|
} : {
|
|
142
144
|
data: Number(value)
|