cvitool 1.0.754 → 1.0.755
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 +42 -1
- package/build/src/cutil.js +118 -0
- package/package.json +1 -1
- package/src/cutil.ts +134 -1
package/build/src/cutil.d.ts
CHANGED
|
@@ -14,6 +14,32 @@ interface getMediaFileTypeRes {
|
|
|
14
14
|
type: 'image' | 'video' | 'audio' | 'unknow';
|
|
15
15
|
extname: 'jpg' | 'png' | 'webp' | 'bmp' | 'heic' | 'gif' | 'mp3' | 'wav' | 'pcm' | 'flac' | 'm4a' | 'aac' | 'mp4' | 'mov' | 'flv' | 'mkv' | 'avi' | 'webm' | 'ogg' | 'm4v' | 'wmv' | 'unknow';
|
|
16
16
|
}
|
|
17
|
+
interface validateParams {
|
|
18
|
+
required: boolean;
|
|
19
|
+
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
20
|
+
allowEmpty?: boolean;
|
|
21
|
+
allowNull?: boolean;
|
|
22
|
+
enums?: any[];
|
|
23
|
+
regex?: string | string[];
|
|
24
|
+
range?: [number, number];
|
|
25
|
+
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
26
|
+
subValidate?: validateParams | {
|
|
27
|
+
[key: string]: validateParams;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
declare const RegStr: {
|
|
31
|
+
zzs: string;
|
|
32
|
+
zxs: (digit: number) => string;
|
|
33
|
+
zrs: string;
|
|
34
|
+
fzs: string;
|
|
35
|
+
fxs: (digit: number) => string;
|
|
36
|
+
zs: string;
|
|
37
|
+
uri: string;
|
|
38
|
+
url: string;
|
|
39
|
+
emali: string;
|
|
40
|
+
mobilePhoneNumber: string;
|
|
41
|
+
idNumber: string;
|
|
42
|
+
};
|
|
17
43
|
/**
|
|
18
44
|
* 获取一个随机字符串
|
|
19
45
|
* @param length 字符串长度
|
|
@@ -101,4 +127,19 @@ declare function readJsonFileSync(path: string, toObj?: boolean): CustomObject |
|
|
|
101
127
|
* @returns
|
|
102
128
|
*/
|
|
103
129
|
declare function uuid(): string;
|
|
104
|
-
|
|
130
|
+
/**
|
|
131
|
+
* 参数校验
|
|
132
|
+
* @param value
|
|
133
|
+
* @param rule
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
declare function validate(value: {
|
|
137
|
+
[key: string]: any;
|
|
138
|
+
}, rule: {
|
|
139
|
+
[key: string]: validateParams;
|
|
140
|
+
}): {
|
|
141
|
+
message: string;
|
|
142
|
+
field: string;
|
|
143
|
+
pass: boolean;
|
|
144
|
+
};
|
|
145
|
+
export { randomStringOptions, getMediaFileTypeRes, CustomObject, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType, checkURLResource, writeJsonFileSync, readJsonFileSync, uuid, validate, RegStr };
|
package/build/src/cutil.js
CHANGED
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RegStr = void 0;
|
|
12
13
|
exports.randomString = randomString;
|
|
13
14
|
exports.encryptCBC = encryptCBC;
|
|
14
15
|
exports.decryptCBC = decryptCBC;
|
|
@@ -20,10 +21,25 @@ exports.checkURLResource = checkURLResource;
|
|
|
20
21
|
exports.writeJsonFileSync = writeJsonFileSync;
|
|
21
22
|
exports.readJsonFileSync = readJsonFileSync;
|
|
22
23
|
exports.uuid = uuid;
|
|
24
|
+
exports.validate = validate;
|
|
23
25
|
const crypto_1 = require("crypto");
|
|
24
26
|
const child_process_1 = require("child_process");
|
|
25
27
|
const fs_1 = require("fs");
|
|
26
28
|
const hgo = require("./hgo");
|
|
29
|
+
const RegStr = {
|
|
30
|
+
zzs: '^[1-9]\\d*$',
|
|
31
|
+
zxs: (digit) => `^(0|[1-9]\\d*)(\\.\\d{1,${digit}})$`,
|
|
32
|
+
zrs: '^(0|[1-9]\\d*)$',
|
|
33
|
+
fzs: '^-[1-9]\\d*$',
|
|
34
|
+
fxs: (digit) => `^-(0|[1-9]\\d*)(\\.\\d{1,${digit}})$`,
|
|
35
|
+
zs: '^(0|-?[1-9]\\d*)$',
|
|
36
|
+
uri: '^[a-zA-z]+://\\S+$',
|
|
37
|
+
url: '^(https?)://([a-z\\d-]+)(\\.[a-z\\d-]+)*/\\S*$',
|
|
38
|
+
emali: '^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$',
|
|
39
|
+
mobilePhoneNumber: '^1[3-9]\\d{9}$',
|
|
40
|
+
idNumber: '^\\d{17}[\\dXx]$'
|
|
41
|
+
};
|
|
42
|
+
exports.RegStr = RegStr;
|
|
27
43
|
/**
|
|
28
44
|
* 获取一个随机字符串
|
|
29
45
|
* @param length 字符串长度
|
|
@@ -373,3 +389,105 @@ function readJsonFileSync(path, toObj = true) {
|
|
|
373
389
|
function uuid() {
|
|
374
390
|
return (0, crypto_1.randomUUID)().replace(/-/g, '');
|
|
375
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* 参数校验
|
|
394
|
+
* @param value
|
|
395
|
+
* @param rule
|
|
396
|
+
* @returns
|
|
397
|
+
*/
|
|
398
|
+
function validate(value, rule) {
|
|
399
|
+
const result = {
|
|
400
|
+
message: '',
|
|
401
|
+
field: '',
|
|
402
|
+
pass: true
|
|
403
|
+
};
|
|
404
|
+
for (const filedName in rule) {
|
|
405
|
+
const filedValidate = rule[filedName];
|
|
406
|
+
const filedValue = value[filedName];
|
|
407
|
+
result.field = filedName;
|
|
408
|
+
const { required, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range } = filedValidate;
|
|
409
|
+
if (required && filedValue === undefined) {
|
|
410
|
+
result.message = 'filed required';
|
|
411
|
+
result.pass = false;
|
|
412
|
+
return result;
|
|
413
|
+
}
|
|
414
|
+
if (filedValue !== undefined) {
|
|
415
|
+
if (!allowNull && filedValue === null) {
|
|
416
|
+
result.message = 'filed value can not null';
|
|
417
|
+
result.pass = false;
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
if (filedValue !== null) {
|
|
421
|
+
if (getValueType(filedValue) !== type) {
|
|
422
|
+
result.message = `filed type must be: ${type}`;
|
|
423
|
+
result.pass = false;
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
if ((type === 'string' && !allowEmpty && filedValue === '') ||
|
|
427
|
+
(type === 'array' && !allowEmpty && filedValue.length === 0) ||
|
|
428
|
+
(type === 'object' && !allowEmpty && Object.keys(filedValue).length === 0)) {
|
|
429
|
+
result.message = 'filed value can not empty';
|
|
430
|
+
result.pass = false;
|
|
431
|
+
return result;
|
|
432
|
+
}
|
|
433
|
+
if (['string', 'number'].includes(type) && enums.length && !enums.includes(filedValue)) {
|
|
434
|
+
result.message = `filed value must one of: [${enums.join(', ')}]`;
|
|
435
|
+
result.pass = false;
|
|
436
|
+
return result;
|
|
437
|
+
}
|
|
438
|
+
if (['string', 'number'].includes(type) && regex) {
|
|
439
|
+
if (getValueType(regex) === 'string' && !new RegExp(regex).test(String(filedValue))) {
|
|
440
|
+
result.message = 'filed value is not pass regex';
|
|
441
|
+
result.pass = false;
|
|
442
|
+
return result;
|
|
443
|
+
}
|
|
444
|
+
if (getValueType(regex) === 'array' && regex.every(regexStr => !new RegExp(regexStr).test(String(filedValue)))) {
|
|
445
|
+
result.message = 'filed value is not pass regex';
|
|
446
|
+
result.pass = false;
|
|
447
|
+
return result;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (['string', 'number'].includes(type) && range && range.length === 2 && range[0] <= range[1]) {
|
|
451
|
+
if (type === 'number' && (filedValue < range[0] || filedValue > range[1])) {
|
|
452
|
+
result.message = `filed value is not in range [${range[0]}, ${range[1]}]`;
|
|
453
|
+
result.pass = false;
|
|
454
|
+
return result;
|
|
455
|
+
}
|
|
456
|
+
if (type === 'string') {
|
|
457
|
+
const len = filedValue.length;
|
|
458
|
+
if (len < range[0] || len > range[1]) {
|
|
459
|
+
result.message = `filed value length not in range [${range[0]}, ${range[1]}]`;
|
|
460
|
+
result.pass = false;
|
|
461
|
+
return result;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (type === 'object' && subValidate) {
|
|
466
|
+
const res = validate(filedValue, subValidate);
|
|
467
|
+
if (!res.pass) {
|
|
468
|
+
return res;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (type === 'array' && subType) {
|
|
472
|
+
for (const subValue of filedValue) {
|
|
473
|
+
if (getValueType(subValue) !== subType) {
|
|
474
|
+
result.message = `filed type must be: ${type}`;
|
|
475
|
+
result.pass = false;
|
|
476
|
+
return result;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (type === 'array' && subType === 'object' && subValidate) {
|
|
481
|
+
for (const subValue of filedValue) {
|
|
482
|
+
const res = validate(subValue, subValidate);
|
|
483
|
+
if (!res.pass) {
|
|
484
|
+
return res;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
result.field = '';
|
|
492
|
+
return result;
|
|
493
|
+
}
|
package/package.json
CHANGED
package/src/cutil.ts
CHANGED
|
@@ -30,6 +30,32 @@ interface getMediaFileTypeRes {
|
|
|
30
30
|
extname: 'jpg' | 'png' | 'webp' | 'bmp' | 'heic' | 'gif' | 'mp3' | 'wav' | 'pcm' | 'flac' | 'm4a' | 'aac' | 'mp4' | 'mov' | 'flv' | 'mkv' | 'avi' | 'webm' | 'ogg' | 'm4v' | 'wmv' | 'unknow'
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
interface validateParams {
|
|
34
|
+
required: boolean,
|
|
35
|
+
type: 'string' | 'number' | 'boolean' | 'object' | 'array',
|
|
36
|
+
allowEmpty?: boolean,
|
|
37
|
+
allowNull?: boolean,
|
|
38
|
+
enums?: any[],
|
|
39
|
+
regex?: string | string[],
|
|
40
|
+
range?: [number, number],
|
|
41
|
+
subType?: 'string' | 'number' | 'boolean' | 'object' | 'array',
|
|
42
|
+
subValidate?: validateParams | { [key: string]: validateParams }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const RegStr = {
|
|
46
|
+
zzs: '^[1-9]\\d*$',
|
|
47
|
+
zxs: (digit: number) => `^(0|[1-9]\\d*)(\\.\\d{1,${digit}})$`,
|
|
48
|
+
zrs: '^(0|[1-9]\\d*)$',
|
|
49
|
+
fzs: '^-[1-9]\\d*$',
|
|
50
|
+
fxs: (digit: number) => `^-(0|[1-9]\\d*)(\\.\\d{1,${digit}})$`,
|
|
51
|
+
zs: '^(0|-?[1-9]\\d*)$',
|
|
52
|
+
uri: '^[a-zA-z]+://\\S+$',
|
|
53
|
+
url: '^(https?)://([a-z\\d-]+)(\\.[a-z\\d-]+)*/\\S*$',
|
|
54
|
+
emali: '^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$',
|
|
55
|
+
mobilePhoneNumber: '^1[3-9]\\d{9}$',
|
|
56
|
+
idNumber: '^\\d{17}[\\dXx]$'
|
|
57
|
+
};
|
|
58
|
+
|
|
33
59
|
/**
|
|
34
60
|
* 获取一个随机字符串
|
|
35
61
|
* @param length 字符串长度
|
|
@@ -379,6 +405,111 @@ function uuid() {
|
|
|
379
405
|
return randomUUID().replace(/-/g, '');
|
|
380
406
|
}
|
|
381
407
|
|
|
408
|
+
/**
|
|
409
|
+
* 参数校验
|
|
410
|
+
* @param value
|
|
411
|
+
* @param rule
|
|
412
|
+
* @returns
|
|
413
|
+
*/
|
|
414
|
+
function validate(value: { [key: string]: any }, rule: { [key: string]: validateParams }): { message: string, field: string, pass: boolean } {
|
|
415
|
+
const result = {
|
|
416
|
+
message: '',
|
|
417
|
+
field: '',
|
|
418
|
+
pass: true
|
|
419
|
+
};
|
|
420
|
+
for (const filedName in rule) {
|
|
421
|
+
const filedValidate = rule[filedName];
|
|
422
|
+
const filedValue = value[filedName];
|
|
423
|
+
result.field = filedName;
|
|
424
|
+
const { required, type, allowEmpty = false, allowNull = false, enums = [], regex, subType, subValidate, range } = filedValidate;
|
|
425
|
+
if (required && filedValue === undefined) {
|
|
426
|
+
result.message = 'filed required';
|
|
427
|
+
result.pass = false;
|
|
428
|
+
return result;
|
|
429
|
+
}
|
|
430
|
+
if (filedValue !== undefined) {
|
|
431
|
+
if (!allowNull && filedValue === null) {
|
|
432
|
+
result.message = 'filed value can not null';
|
|
433
|
+
result.pass = false;
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
if (filedValue !== null) {
|
|
437
|
+
if (getValueType(filedValue) !== type) {
|
|
438
|
+
result.message = `filed type must be: ${type}`;
|
|
439
|
+
result.pass = false;
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
if (
|
|
443
|
+
(type === 'string' && !allowEmpty && filedValue === '') ||
|
|
444
|
+
(type === 'array' && !allowEmpty && (filedValue as any[]).length === 0) ||
|
|
445
|
+
(type === 'object' && !allowEmpty && Object.keys(filedValue).length === 0)
|
|
446
|
+
) {
|
|
447
|
+
result.message = 'filed value can not empty';
|
|
448
|
+
result.pass = false;
|
|
449
|
+
return result;
|
|
450
|
+
}
|
|
451
|
+
if (['string', 'number'].includes(type) && enums.length && !enums.includes(filedValue)) {
|
|
452
|
+
result.message = `filed value must one of: [${enums.join(', ')}]`;
|
|
453
|
+
result.pass = false;
|
|
454
|
+
return result;
|
|
455
|
+
}
|
|
456
|
+
if (['string', 'number'].includes(type) && regex) {
|
|
457
|
+
if (getValueType(regex) === 'string' && !new RegExp(regex as string).test(String(filedValue))) {
|
|
458
|
+
result.message = 'filed value is not pass regex';
|
|
459
|
+
result.pass = false;
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
if (getValueType(regex) === 'array' && (regex as string[]).every(regexStr => !new RegExp(regexStr).test(String(filedValue)))) {
|
|
463
|
+
result.message = 'filed value is not pass regex';
|
|
464
|
+
result.pass = false;
|
|
465
|
+
return result;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (['string', 'number'].includes(type) && range && range.length === 2 && range[0] <= range[1]) {
|
|
469
|
+
if (type === 'number' && (filedValue < range[0] || filedValue > range[1])) {
|
|
470
|
+
result.message = `filed value is not in range [${range[0]}, ${range[1]}]`;
|
|
471
|
+
result.pass = false;
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
if (type === 'string') {
|
|
475
|
+
const len = (filedValue as string).length;
|
|
476
|
+
if (len < range[0] || len > range[1]) {
|
|
477
|
+
result.message = `filed value length not in range [${range[0]}, ${range[1]}]`;
|
|
478
|
+
result.pass = false;
|
|
479
|
+
return result;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (type === 'object' && subValidate) {
|
|
484
|
+
const res = validate(filedValue, subValidate as { [key: string]: validateParams });
|
|
485
|
+
if (!res.pass) {
|
|
486
|
+
return res;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (type === 'array' && subType) {
|
|
490
|
+
for (const subValue of filedValue) {
|
|
491
|
+
if (getValueType(subValue) !== subType) {
|
|
492
|
+
result.message = `filed type must be: ${type}`;
|
|
493
|
+
result.pass = false;
|
|
494
|
+
return result;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (type === 'array' && subType === 'object' && subValidate) {
|
|
499
|
+
for (const subValue of filedValue) {
|
|
500
|
+
const res = validate(subValue, subValidate as { [key: string]: validateParams });
|
|
501
|
+
if (!res.pass) {
|
|
502
|
+
return res;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
result.field = '';
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
|
|
382
513
|
export {
|
|
383
514
|
randomStringOptions,
|
|
384
515
|
getMediaFileTypeRes,
|
|
@@ -393,5 +524,7 @@ export {
|
|
|
393
524
|
checkURLResource,
|
|
394
525
|
writeJsonFileSync,
|
|
395
526
|
readJsonFileSync,
|
|
396
|
-
uuid
|
|
527
|
+
uuid,
|
|
528
|
+
validate,
|
|
529
|
+
RegStr
|
|
397
530
|
};
|