co.validation 2.6.1 → 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.
|
@@ -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) => {
|
|
@@ -420,8 +427,7 @@ describe('createValidation', () => {
|
|
|
420
427
|
expect((0, _.createValidation)({
|
|
421
428
|
type: 'number'
|
|
422
429
|
})('')).toEqual({
|
|
423
|
-
data: undefined
|
|
424
|
-
errors: ['Must be a number']
|
|
430
|
+
data: undefined
|
|
425
431
|
});
|
|
426
432
|
expect((0, _.createValidation)({
|
|
427
433
|
type: 'number',
|
|
@@ -438,6 +444,12 @@ describe('createValidation', () => {
|
|
|
438
444
|
});
|
|
439
445
|
});
|
|
440
446
|
it('supports minValue', () => {
|
|
447
|
+
expect((0, _.createValidation)({
|
|
448
|
+
type: 'minValue',
|
|
449
|
+
min: 1
|
|
450
|
+
})('')).toEqual({
|
|
451
|
+
data: undefined
|
|
452
|
+
});
|
|
441
453
|
expect((0, _.createValidation)({
|
|
442
454
|
type: 'minValue',
|
|
443
455
|
min: 1
|
|
@@ -476,6 +488,51 @@ describe('createValidation', () => {
|
|
|
476
488
|
errors: ['Must be at least 333']
|
|
477
489
|
});
|
|
478
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
|
+
});
|
|
479
536
|
it('supports string', () => {
|
|
480
537
|
expect((0, _.createValidation)({
|
|
481
538
|
type: 'string',
|
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,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)
|