cvitool 1.0.759 → 1.0.761
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.
- package/build/src/cutil.d.ts +5 -1
- package/build/src/cutil.js +50 -5
- package/package.json +1 -1
- package/src/cutil.ts +57 -5
package/build/src/cutil.d.ts
CHANGED
|
@@ -24,6 +24,10 @@ interface validateParams {
|
|
|
24
24
|
range?: [number, number];
|
|
25
25
|
length?: number;
|
|
26
26
|
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
27
|
+
subEnums?: any[];
|
|
28
|
+
subRegex?: string | string[];
|
|
29
|
+
subRange?: [number, number];
|
|
30
|
+
subLength?: number;
|
|
27
31
|
subValidate?: {
|
|
28
32
|
[key: string]: validateParams;
|
|
29
33
|
};
|
|
@@ -140,7 +144,7 @@ declare function validate(value: {
|
|
|
140
144
|
[key: string]: any;
|
|
141
145
|
}, rule: {
|
|
142
146
|
[key: string]: validateParams;
|
|
143
|
-
}): {
|
|
147
|
+
}, finalFunc?: (value: any) => boolean): {
|
|
144
148
|
message: string;
|
|
145
149
|
field: string;
|
|
146
150
|
pass: boolean;
|
package/build/src/cutil.js
CHANGED
|
@@ -397,7 +397,7 @@ function uuid() {
|
|
|
397
397
|
* @param rule
|
|
398
398
|
* @returns
|
|
399
399
|
*/
|
|
400
|
-
function validate(value, rule) {
|
|
400
|
+
function validate(value, rule, finalFunc) {
|
|
401
401
|
const result = {
|
|
402
402
|
message: '',
|
|
403
403
|
field: '',
|
|
@@ -408,7 +408,7 @@ function validate(value, rule) {
|
|
|
408
408
|
const fieldValidate = rule[fieldName];
|
|
409
409
|
const fieldValue = value[fieldName];
|
|
410
410
|
result.field = fieldName;
|
|
411
|
-
const { required = true, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range, length, func } = fieldValidate;
|
|
411
|
+
const { required = true, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range, length, func, subEnums = [], subRegex, subRange, subLength } = fieldValidate;
|
|
412
412
|
if (required && fieldValue === undefined) {
|
|
413
413
|
result.message = 'field required';
|
|
414
414
|
result.pass = false;
|
|
@@ -440,12 +440,12 @@ function validate(value, rule) {
|
|
|
440
440
|
}
|
|
441
441
|
if (['string', 'number'].includes(type) && regex) {
|
|
442
442
|
if (getValueType(regex) === 'string' && !new RegExp(regex).test(String(fieldValue))) {
|
|
443
|
-
result.message = 'field value is not pass
|
|
443
|
+
result.message = 'field value is not pass validate';
|
|
444
444
|
result.pass = false;
|
|
445
445
|
return result;
|
|
446
446
|
}
|
|
447
447
|
if (getValueType(regex) === 'array' && regex.every(regexStr => !new RegExp(regexStr).test(String(fieldValue)))) {
|
|
448
|
-
result.message = 'field value is not pass
|
|
448
|
+
result.message = 'field value is not pass validate';
|
|
449
449
|
result.pass = false;
|
|
450
450
|
return result;
|
|
451
451
|
}
|
|
@@ -490,6 +490,43 @@ function validate(value, rule) {
|
|
|
490
490
|
result.pass = false;
|
|
491
491
|
return result;
|
|
492
492
|
}
|
|
493
|
+
if (subEnums.length && ['string', 'number'].includes(subType) && !subEnums.includes(subValue)) {
|
|
494
|
+
result.message = `field subValue must one of: [${subEnums.join(', ')}]`;
|
|
495
|
+
result.pass = false;
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
if (['string', 'number'].includes(subType) && subRange && subRange.length === 2 && subRange[0] <= subRange[1]) {
|
|
499
|
+
if (subType === 'number' && (subValue < subRange[0] || subValue > subRange[1])) {
|
|
500
|
+
result.message = `field subValue is not in range [${subRange[0]}, ${subRange[1]}]`;
|
|
501
|
+
result.pass = false;
|
|
502
|
+
return result;
|
|
503
|
+
}
|
|
504
|
+
if (subType === 'string') {
|
|
505
|
+
const len = subValue.length;
|
|
506
|
+
if (len < subRange[0] || len > subRange[1]) {
|
|
507
|
+
result.message = `field subValue length not in range [${subRange[0]}, ${subRange[1]}]`;
|
|
508
|
+
result.pass = false;
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (['string', 'array'].includes(subType) && subLength && subValue.length !== subLength) {
|
|
514
|
+
result.message = `field subValue length must be: ${subLength}`;
|
|
515
|
+
result.pass = false;
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
if (['string', 'number'].includes(subType) && subRegex) {
|
|
519
|
+
if (getValueType(subRegex) === 'string' && !new RegExp(subRegex).test(String(subValue))) {
|
|
520
|
+
result.message = 'field subValue is not pass validate';
|
|
521
|
+
result.pass = false;
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
if (getValueType(subRegex) === 'array' && subRegex.every(regexStr => !new RegExp(regexStr).test(String(subValue)))) {
|
|
525
|
+
result.message = 'field subValue is not pass validate';
|
|
526
|
+
result.pass = false;
|
|
527
|
+
return result;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
493
530
|
}
|
|
494
531
|
}
|
|
495
532
|
if (type === 'array' && subType === 'object' && subValidate) {
|
|
@@ -507,13 +544,21 @@ function validate(value, rule) {
|
|
|
507
544
|
for (const funcReg of funcs) {
|
|
508
545
|
const { fieldName, fieldValue, func } = funcReg;
|
|
509
546
|
if (!func(fieldValue)) {
|
|
510
|
-
result.message = 'field value not pass
|
|
547
|
+
result.message = 'field value not pass validate';
|
|
511
548
|
result.field = fieldName;
|
|
512
549
|
result.pass = false;
|
|
513
550
|
return result;
|
|
514
551
|
}
|
|
515
552
|
}
|
|
516
553
|
}
|
|
554
|
+
if (finalFunc) {
|
|
555
|
+
if (!finalFunc(value)) {
|
|
556
|
+
result.message = 'params not pass validate';
|
|
557
|
+
result.field = '';
|
|
558
|
+
result.pass = false;
|
|
559
|
+
return result;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
517
562
|
result.field = '';
|
|
518
563
|
return result;
|
|
519
564
|
}
|
package/package.json
CHANGED
package/src/cutil.ts
CHANGED
|
@@ -40,6 +40,10 @@ interface validateParams {
|
|
|
40
40
|
range?: [number, number],
|
|
41
41
|
length?: number,
|
|
42
42
|
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array',
|
|
43
|
+
subEnums?: any[],
|
|
44
|
+
subRegex?: string | string[],
|
|
45
|
+
subRange?: [number, number],
|
|
46
|
+
subLength?: number,
|
|
43
47
|
subValidate?: { [key: string]: validateParams },
|
|
44
48
|
func?: (value: any) => boolean
|
|
45
49
|
}
|
|
@@ -414,7 +418,7 @@ function uuid() {
|
|
|
414
418
|
* @param rule
|
|
415
419
|
* @returns
|
|
416
420
|
*/
|
|
417
|
-
function validate(value: { [key: string]: any }, rule: { [key: string]: validateParams }): { message: string, field: string, pass: boolean } {
|
|
421
|
+
function validate(value: { [key: string]: any }, rule: { [key: string]: validateParams }, finalFunc?: (value: any) => boolean): { message: string, field: string, pass: boolean } {
|
|
418
422
|
const result = {
|
|
419
423
|
message: '',
|
|
420
424
|
field: '',
|
|
@@ -425,7 +429,10 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
425
429
|
const fieldValidate = rule[fieldName];
|
|
426
430
|
const fieldValue = value[fieldName];
|
|
427
431
|
result.field = fieldName;
|
|
428
|
-
const {
|
|
432
|
+
const {
|
|
433
|
+
required = true, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range, length, func,
|
|
434
|
+
subEnums = [], subRegex, subRange, subLength
|
|
435
|
+
} = fieldValidate;
|
|
429
436
|
if (required && fieldValue === undefined) {
|
|
430
437
|
result.message = 'field required';
|
|
431
438
|
result.pass = false;
|
|
@@ -459,12 +466,12 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
459
466
|
}
|
|
460
467
|
if (['string', 'number'].includes(type) && regex) {
|
|
461
468
|
if (getValueType(regex) === 'string' && !new RegExp(regex as string).test(String(fieldValue))) {
|
|
462
|
-
result.message = 'field value is not pass
|
|
469
|
+
result.message = 'field value is not pass validate';
|
|
463
470
|
result.pass = false;
|
|
464
471
|
return result;
|
|
465
472
|
}
|
|
466
473
|
if (getValueType(regex) === 'array' && (regex as string[]).every(regexStr => !new RegExp(regexStr).test(String(fieldValue)))) {
|
|
467
|
-
result.message = 'field value is not pass
|
|
474
|
+
result.message = 'field value is not pass validate';
|
|
468
475
|
result.pass = false;
|
|
469
476
|
return result;
|
|
470
477
|
}
|
|
@@ -509,6 +516,43 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
509
516
|
result.pass = false;
|
|
510
517
|
return result;
|
|
511
518
|
}
|
|
519
|
+
if (subEnums.length && ['string', 'number'].includes(subType) && !subEnums.includes(subValue)) {
|
|
520
|
+
result.message = `field subValue must one of: [${subEnums.join(', ')}]`;
|
|
521
|
+
result.pass = false;
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
if (['string', 'number'].includes(subType) && subRange && subRange.length === 2 && subRange[0] <= subRange[1]) {
|
|
525
|
+
if (subType === 'number' && (subValue < subRange[0] || subValue > subRange[1])) {
|
|
526
|
+
result.message = `field subValue is not in range [${subRange[0]}, ${subRange[1]}]`;
|
|
527
|
+
result.pass = false;
|
|
528
|
+
return result;
|
|
529
|
+
}
|
|
530
|
+
if (subType === 'string') {
|
|
531
|
+
const len = (subValue as string).length;
|
|
532
|
+
if (len < subRange[0] || len > subRange[1]) {
|
|
533
|
+
result.message = `field subValue length not in range [${subRange[0]}, ${subRange[1]}]`;
|
|
534
|
+
result.pass = false;
|
|
535
|
+
return result;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (['string', 'array'].includes(subType) && subLength && (subValue as string | any[]).length !== subLength) {
|
|
540
|
+
result.message = `field subValue length must be: ${subLength}`;
|
|
541
|
+
result.pass = false;
|
|
542
|
+
return result;
|
|
543
|
+
}
|
|
544
|
+
if (['string', 'number'].includes(subType) && subRegex) {
|
|
545
|
+
if (getValueType(subRegex) === 'string' && !new RegExp(subRegex as string).test(String(subValue))) {
|
|
546
|
+
result.message = 'field subValue is not pass validate';
|
|
547
|
+
result.pass = false;
|
|
548
|
+
return result;
|
|
549
|
+
}
|
|
550
|
+
if (getValueType(subRegex) === 'array' && (subRegex as string[]).every(regexStr => !new RegExp(regexStr).test(String(subValue)))) {
|
|
551
|
+
result.message = 'field subValue is not pass validate';
|
|
552
|
+
result.pass = false;
|
|
553
|
+
return result;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
512
556
|
}
|
|
513
557
|
}
|
|
514
558
|
if (type === 'array' && subType === 'object' && subValidate) {
|
|
@@ -526,13 +570,21 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
526
570
|
for (const funcReg of funcs) {
|
|
527
571
|
const { fieldName, fieldValue, func } = funcReg;
|
|
528
572
|
if (!func(fieldValue)) {
|
|
529
|
-
result.message = 'field value not pass
|
|
573
|
+
result.message = 'field value not pass validate';
|
|
530
574
|
result.field = fieldName;
|
|
531
575
|
result.pass = false;
|
|
532
576
|
return result;
|
|
533
577
|
}
|
|
534
578
|
}
|
|
535
579
|
}
|
|
580
|
+
if (finalFunc) {
|
|
581
|
+
if (!finalFunc(value)) {
|
|
582
|
+
result.message = 'params not pass validate';
|
|
583
|
+
result.field = '';
|
|
584
|
+
result.pass = false;
|
|
585
|
+
return result;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
536
588
|
result.field = '';
|
|
537
589
|
return result;
|
|
538
590
|
}
|