cvitool 1.0.756 → 1.0.757
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 +2 -0
- package/build/src/cutil.js +55 -31
- package/package.json +1 -1
- package/src/cutil.ts +58 -32
package/build/src/cutil.d.ts
CHANGED
|
@@ -22,10 +22,12 @@ interface validateParams {
|
|
|
22
22
|
enums?: any[];
|
|
23
23
|
regex?: string | string[];
|
|
24
24
|
range?: [number, number];
|
|
25
|
+
length?: number;
|
|
25
26
|
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
26
27
|
subValidate?: validateParams | {
|
|
27
28
|
[key: string]: validateParams;
|
|
28
29
|
};
|
|
30
|
+
func?: (value: any) => boolean;
|
|
29
31
|
}
|
|
30
32
|
declare const RegStr: {
|
|
31
33
|
zzs: string;
|
package/build/src/cutil.js
CHANGED
|
@@ -402,84 +402,97 @@ function validate(value, rule) {
|
|
|
402
402
|
field: '',
|
|
403
403
|
pass: true
|
|
404
404
|
};
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
405
|
+
const funcs = [];
|
|
406
|
+
for (const fieldName in rule) {
|
|
407
|
+
const fieldValidate = rule[fieldName];
|
|
408
|
+
const fieldValue = value[fieldName];
|
|
409
|
+
result.field = fieldName;
|
|
410
|
+
const { required = true, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range, length, func } = fieldValidate;
|
|
411
|
+
if (required && fieldValue === undefined) {
|
|
412
|
+
result.message = 'field required';
|
|
412
413
|
result.pass = false;
|
|
413
414
|
return result;
|
|
414
415
|
}
|
|
415
|
-
if (
|
|
416
|
-
if (!allowNull &&
|
|
417
|
-
result.message = '
|
|
416
|
+
if (fieldValue !== undefined) {
|
|
417
|
+
if (!allowNull && fieldValue === null) {
|
|
418
|
+
result.message = 'field value can not null';
|
|
418
419
|
result.pass = false;
|
|
419
420
|
return result;
|
|
420
421
|
}
|
|
421
|
-
if (
|
|
422
|
-
if (getValueType(
|
|
423
|
-
result.message = `
|
|
422
|
+
if (fieldValue !== null) {
|
|
423
|
+
if (getValueType(fieldValue) !== type) {
|
|
424
|
+
result.message = `field type must be: ${type}`;
|
|
424
425
|
result.pass = false;
|
|
425
426
|
return result;
|
|
426
427
|
}
|
|
427
|
-
if ((type === 'string' && !allowEmpty &&
|
|
428
|
-
(type === 'array' && !allowEmpty &&
|
|
429
|
-
(type === 'object' && !allowEmpty && Object.keys(
|
|
430
|
-
result.message = '
|
|
428
|
+
if ((type === 'string' && !allowEmpty && fieldValue === '') ||
|
|
429
|
+
(type === 'array' && !allowEmpty && fieldValue.length === 0) ||
|
|
430
|
+
(type === 'object' && !allowEmpty && Object.keys(fieldValue).length === 0)) {
|
|
431
|
+
result.message = 'field value can not empty';
|
|
431
432
|
result.pass = false;
|
|
432
433
|
return result;
|
|
433
434
|
}
|
|
434
|
-
if (['string', 'number'].includes(type) && enums.length && !enums.includes(
|
|
435
|
-
result.message = `
|
|
435
|
+
if (['string', 'number'].includes(type) && enums.length && !enums.includes(fieldValue)) {
|
|
436
|
+
result.message = `field value must one of: [${enums.join(', ')}]`;
|
|
436
437
|
result.pass = false;
|
|
437
438
|
return result;
|
|
438
439
|
}
|
|
439
440
|
if (['string', 'number'].includes(type) && regex) {
|
|
440
|
-
if (getValueType(regex) === 'string' && !new RegExp(regex).test(String(
|
|
441
|
-
result.message = '
|
|
441
|
+
if (getValueType(regex) === 'string' && !new RegExp(regex).test(String(fieldValue))) {
|
|
442
|
+
result.message = 'field value is not pass regex';
|
|
442
443
|
result.pass = false;
|
|
443
444
|
return result;
|
|
444
445
|
}
|
|
445
|
-
if (getValueType(regex) === 'array' && regex.every(regexStr => !new RegExp(regexStr).test(String(
|
|
446
|
-
result.message = '
|
|
446
|
+
if (getValueType(regex) === 'array' && regex.every(regexStr => !new RegExp(regexStr).test(String(fieldValue)))) {
|
|
447
|
+
result.message = 'field value is not pass regex';
|
|
447
448
|
result.pass = false;
|
|
448
449
|
return result;
|
|
449
450
|
}
|
|
450
451
|
}
|
|
451
452
|
if (['string', 'number'].includes(type) && range && range.length === 2 && range[0] <= range[1]) {
|
|
452
|
-
if (type === 'number' && (
|
|
453
|
-
result.message = `
|
|
453
|
+
if (type === 'number' && (fieldValue < range[0] || fieldValue > range[1])) {
|
|
454
|
+
result.message = `field value is not in range [${range[0]}, ${range[1]}]`;
|
|
454
455
|
result.pass = false;
|
|
455
456
|
return result;
|
|
456
457
|
}
|
|
457
458
|
if (type === 'string') {
|
|
458
|
-
const len =
|
|
459
|
+
const len = fieldValue.length;
|
|
459
460
|
if (len < range[0] || len > range[1]) {
|
|
460
|
-
result.message = `
|
|
461
|
+
result.message = `field value length not in range [${range[0]}, ${range[1]}]`;
|
|
461
462
|
result.pass = false;
|
|
462
463
|
return result;
|
|
463
464
|
}
|
|
464
465
|
}
|
|
465
466
|
}
|
|
467
|
+
if (['string', 'array'].includes(type) && length && fieldValue.length !== length) {
|
|
468
|
+
result.message = `field value length must be: ${length}`;
|
|
469
|
+
result.pass = false;
|
|
470
|
+
return result;
|
|
471
|
+
}
|
|
472
|
+
if (func) {
|
|
473
|
+
funcs.push({
|
|
474
|
+
fieldName,
|
|
475
|
+
fieldValue,
|
|
476
|
+
func
|
|
477
|
+
});
|
|
478
|
+
}
|
|
466
479
|
if (type === 'object' && subValidate) {
|
|
467
|
-
const res = validate(
|
|
480
|
+
const res = validate(fieldValue, subValidate);
|
|
468
481
|
if (!res.pass) {
|
|
469
482
|
return res;
|
|
470
483
|
}
|
|
471
484
|
}
|
|
472
485
|
if (type === 'array' && subType) {
|
|
473
|
-
for (const subValue of
|
|
486
|
+
for (const subValue of fieldValue) {
|
|
474
487
|
if (getValueType(subValue) !== subType) {
|
|
475
|
-
result.message = `
|
|
488
|
+
result.message = `field type must be: ${type}`;
|
|
476
489
|
result.pass = false;
|
|
477
490
|
return result;
|
|
478
491
|
}
|
|
479
492
|
}
|
|
480
493
|
}
|
|
481
494
|
if (type === 'array' && subType === 'object' && subValidate) {
|
|
482
|
-
for (const subValue of
|
|
495
|
+
for (const subValue of fieldValue) {
|
|
483
496
|
const res = validate(subValue, subValidate);
|
|
484
497
|
if (!res.pass) {
|
|
485
498
|
return res;
|
|
@@ -489,6 +502,17 @@ function validate(value, rule) {
|
|
|
489
502
|
}
|
|
490
503
|
}
|
|
491
504
|
}
|
|
505
|
+
if (funcs.length) {
|
|
506
|
+
for (const funcReg of funcs) {
|
|
507
|
+
const { fieldName, fieldValue, func } = funcReg;
|
|
508
|
+
if (!func(fieldValue)) {
|
|
509
|
+
result.message = 'field value not pass function validate';
|
|
510
|
+
result.field = fieldName;
|
|
511
|
+
result.pass = false;
|
|
512
|
+
return result;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
492
516
|
result.field = '';
|
|
493
517
|
return result;
|
|
494
518
|
}
|
package/package.json
CHANGED
package/src/cutil.ts
CHANGED
|
@@ -38,8 +38,10 @@ interface validateParams {
|
|
|
38
38
|
enums?: any[],
|
|
39
39
|
regex?: string | string[],
|
|
40
40
|
range?: [number, number],
|
|
41
|
+
length?: number,
|
|
41
42
|
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array',
|
|
42
|
-
subValidate?: validateParams | { [key: string]: validateParams }
|
|
43
|
+
subValidate?: validateParams | { [key: string]: validateParams },
|
|
44
|
+
func?: (value: any) => boolean
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
const RegStr = {
|
|
@@ -418,86 +420,99 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
418
420
|
field: '',
|
|
419
421
|
pass: true
|
|
420
422
|
};
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
423
|
+
const funcs: { fieldName: string, fieldValue: any, func: (value: any) => boolean }[] = [];
|
|
424
|
+
for (const fieldName in rule) {
|
|
425
|
+
const fieldValidate = rule[fieldName];
|
|
426
|
+
const fieldValue = value[fieldName];
|
|
427
|
+
result.field = fieldName;
|
|
428
|
+
const { required = true, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range, length, func } = fieldValidate;
|
|
429
|
+
if (required && fieldValue === undefined) {
|
|
430
|
+
result.message = 'field required';
|
|
428
431
|
result.pass = false;
|
|
429
432
|
return result;
|
|
430
433
|
}
|
|
431
|
-
if (
|
|
432
|
-
if (!allowNull &&
|
|
433
|
-
result.message = '
|
|
434
|
+
if (fieldValue !== undefined) {
|
|
435
|
+
if (!allowNull && fieldValue === null) {
|
|
436
|
+
result.message = 'field value can not null';
|
|
434
437
|
result.pass = false;
|
|
435
438
|
return result;
|
|
436
439
|
}
|
|
437
|
-
if (
|
|
438
|
-
if (getValueType(
|
|
439
|
-
result.message = `
|
|
440
|
+
if (fieldValue !== null) {
|
|
441
|
+
if (getValueType(fieldValue) !== type) {
|
|
442
|
+
result.message = `field type must be: ${type}`;
|
|
440
443
|
result.pass = false;
|
|
441
444
|
return result;
|
|
442
445
|
}
|
|
443
446
|
if (
|
|
444
|
-
(type === 'string' && !allowEmpty &&
|
|
445
|
-
(type === 'array' && !allowEmpty && (
|
|
446
|
-
(type === 'object' && !allowEmpty && Object.keys(
|
|
447
|
+
(type === 'string' && !allowEmpty && fieldValue === '') ||
|
|
448
|
+
(type === 'array' && !allowEmpty && (fieldValue as any[]).length === 0) ||
|
|
449
|
+
(type === 'object' && !allowEmpty && Object.keys(fieldValue).length === 0)
|
|
447
450
|
) {
|
|
448
|
-
result.message = '
|
|
451
|
+
result.message = 'field value can not empty';
|
|
449
452
|
result.pass = false;
|
|
450
453
|
return result;
|
|
451
454
|
}
|
|
452
|
-
if (['string', 'number'].includes(type) && enums.length && !enums.includes(
|
|
453
|
-
result.message = `
|
|
455
|
+
if (['string', 'number'].includes(type) && enums.length && !enums.includes(fieldValue)) {
|
|
456
|
+
result.message = `field value must one of: [${enums.join(', ')}]`;
|
|
454
457
|
result.pass = false;
|
|
455
458
|
return result;
|
|
456
459
|
}
|
|
457
460
|
if (['string', 'number'].includes(type) && regex) {
|
|
458
|
-
if (getValueType(regex) === 'string' && !new RegExp(regex as string).test(String(
|
|
459
|
-
result.message = '
|
|
461
|
+
if (getValueType(regex) === 'string' && !new RegExp(regex as string).test(String(fieldValue))) {
|
|
462
|
+
result.message = 'field value is not pass regex';
|
|
460
463
|
result.pass = false;
|
|
461
464
|
return result;
|
|
462
465
|
}
|
|
463
|
-
if (getValueType(regex) === 'array' && (regex as string[]).every(regexStr => !new RegExp(regexStr).test(String(
|
|
464
|
-
result.message = '
|
|
466
|
+
if (getValueType(regex) === 'array' && (regex as string[]).every(regexStr => !new RegExp(regexStr).test(String(fieldValue)))) {
|
|
467
|
+
result.message = 'field value is not pass regex';
|
|
465
468
|
result.pass = false;
|
|
466
469
|
return result;
|
|
467
470
|
}
|
|
468
471
|
}
|
|
469
472
|
if (['string', 'number'].includes(type) && range && range.length === 2 && range[0] <= range[1]) {
|
|
470
|
-
if (type === 'number' && (
|
|
471
|
-
result.message = `
|
|
473
|
+
if (type === 'number' && (fieldValue < range[0] || fieldValue > range[1])) {
|
|
474
|
+
result.message = `field value is not in range [${range[0]}, ${range[1]}]`;
|
|
472
475
|
result.pass = false;
|
|
473
476
|
return result;
|
|
474
477
|
}
|
|
475
478
|
if (type === 'string') {
|
|
476
|
-
const len = (
|
|
479
|
+
const len = (fieldValue as string).length;
|
|
477
480
|
if (len < range[0] || len > range[1]) {
|
|
478
|
-
result.message = `
|
|
481
|
+
result.message = `field value length not in range [${range[0]}, ${range[1]}]`;
|
|
479
482
|
result.pass = false;
|
|
480
483
|
return result;
|
|
481
484
|
}
|
|
482
485
|
}
|
|
483
486
|
}
|
|
487
|
+
if (['string', 'array'].includes(type) && length && (fieldValue as string | any[]).length !== length) {
|
|
488
|
+
result.message = `field value length must be: ${length}`;
|
|
489
|
+
result.pass = false;
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
if (func) {
|
|
493
|
+
funcs.push({
|
|
494
|
+
fieldName,
|
|
495
|
+
fieldValue,
|
|
496
|
+
func
|
|
497
|
+
});
|
|
498
|
+
}
|
|
484
499
|
if (type === 'object' && subValidate) {
|
|
485
|
-
const res = validate(
|
|
500
|
+
const res = validate(fieldValue, subValidate as { [key: string]: validateParams });
|
|
486
501
|
if (!res.pass) {
|
|
487
502
|
return res;
|
|
488
503
|
}
|
|
489
504
|
}
|
|
490
505
|
if (type === 'array' && subType) {
|
|
491
|
-
for (const subValue of
|
|
506
|
+
for (const subValue of fieldValue) {
|
|
492
507
|
if (getValueType(subValue) !== subType) {
|
|
493
|
-
result.message = `
|
|
508
|
+
result.message = `field type must be: ${type}`;
|
|
494
509
|
result.pass = false;
|
|
495
510
|
return result;
|
|
496
511
|
}
|
|
497
512
|
}
|
|
498
513
|
}
|
|
499
514
|
if (type === 'array' && subType === 'object' && subValidate) {
|
|
500
|
-
for (const subValue of
|
|
515
|
+
for (const subValue of fieldValue) {
|
|
501
516
|
const res = validate(subValue, subValidate as { [key: string]: validateParams });
|
|
502
517
|
if (!res.pass) {
|
|
503
518
|
return res;
|
|
@@ -507,6 +522,17 @@ function validate(value: { [key: string]: any }, rule: { [key: string]: validate
|
|
|
507
522
|
}
|
|
508
523
|
}
|
|
509
524
|
}
|
|
525
|
+
if (funcs.length) {
|
|
526
|
+
for (const funcReg of funcs) {
|
|
527
|
+
const { fieldName, fieldValue, func } = funcReg;
|
|
528
|
+
if (!func(fieldValue)) {
|
|
529
|
+
result.message = 'field value not pass function validate';
|
|
530
|
+
result.field = fieldName;
|
|
531
|
+
result.pass = false;
|
|
532
|
+
return result;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
510
536
|
result.field = '';
|
|
511
537
|
return result;
|
|
512
538
|
}
|