almighty-tool 0.0.26 → 0.0.28

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/bun.lockb CHANGED
Binary file
@@ -0,0 +1,42 @@
1
+ import OriginalSchema, { ExecuteRule as ValidateExecuteRule, ExecuteValidator as ValidateExecuteValidator, InternalRuleItem as ValidateInternalRuleItem, InternalValidateMessages as ValidateInternalValidateMessages, ValidateError as OriginalValidateError, Rule as ValidateRule, RuleItem as ValidateRuleItem, Rules as ValidateRules, RuleType as ValidateRuleType, RuleValuePackage as ValidateRuleValuePackage, ValidateCallback as ValidateValidateCallback, ValidateFieldsError as ValidateValidateFieldsError, ValidateMessages as ValidateValidateMessages, ValidateOption as ValidateValidateOption, ValidateResult as ValidateValidateResult, Value as ValidateValue, Values as ValidateValues } from 'async-validator';
2
+ export declare class ValidateSchema extends OriginalSchema {
3
+ }
4
+ export type { ValidateExecuteRule, ValidateExecuteValidator, ValidateInternalRuleItem, ValidateInternalValidateMessages, ValidateRule, ValidateRuleItem, ValidateRules, ValidateRuleType, ValidateRuleValuePackage, ValidateValidateCallback, ValidateValidateFieldsError, ValidateValidateMessages, ValidateValidateOption, ValidateValidateResult, ValidateValue, ValidateValues, };
5
+ export interface ValidateError extends OriginalValidateError {
6
+ code?: string;
7
+ }
8
+ export interface ValidateResponse {
9
+ /** 是否成功 */
10
+ success: boolean;
11
+ /** 错误信息 */
12
+ errors?: ValidateError[];
13
+ }
14
+ /** 校验工具 */
15
+ declare const validateUtil: {
16
+ /**
17
+ * 获取校验器
18
+ * @param rules 校验规则
19
+ * @returns 校验器
20
+ */
21
+ getSchema: (rules: ValidateRules | ValidateSchema) => ValidateSchema;
22
+ /**
23
+ * 获取错误信息
24
+ * @param error 错误信息
25
+ * @returns 错误信息
26
+ */
27
+ getErrorMessage: (error: unknown) => any;
28
+ /**
29
+ * 获取错误信息
30
+ * @param error 错误信息
31
+ * @returns 错误信息
32
+ */
33
+ getErrors: (error: unknown) => ValidateError[];
34
+ /**
35
+ * 校验数据
36
+ * @param rules 校验规则
37
+ * @param data 数据
38
+ * @returns 校验结果
39
+ */
40
+ validate: (rules: ValidateRules | ValidateSchema, data: ValidateValues) => Promise<ValidateResponse>;
41
+ };
42
+ export default validateUtil;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidateSchema = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var inflection = (0, tslib_1.__importStar)(require("inflection"));
6
+ var async_validator_1 = (0, tslib_1.__importDefault)(require("async-validator"));
7
+ var ValidateSchema = /** @class */ (function (_super) {
8
+ (0, tslib_1.__extends)(ValidateSchema, _super);
9
+ function ValidateSchema() {
10
+ return _super !== null && _super.apply(this, arguments) || this;
11
+ }
12
+ return ValidateSchema;
13
+ }(async_validator_1.default));
14
+ exports.ValidateSchema = ValidateSchema;
15
+ /** 校验工具 */
16
+ var validateUtil = {
17
+ /**
18
+ * 获取校验器
19
+ * @param rules 校验规则
20
+ * @returns 校验器
21
+ */
22
+ getSchema: function (rules) {
23
+ if (rules instanceof ValidateSchema) {
24
+ return rules;
25
+ }
26
+ return new ValidateSchema(rules);
27
+ },
28
+ /**
29
+ * 获取错误信息
30
+ * @param error 错误信息
31
+ * @returns 错误信息
32
+ */
33
+ getErrorMessage: function (error) {
34
+ if (typeof error === 'object' && error !== null && 'message' in error) {
35
+ return Reflect.get(error, 'message');
36
+ }
37
+ if (typeof error === 'string') {
38
+ return error;
39
+ }
40
+ return "".concat(error);
41
+ },
42
+ /**
43
+ * 获取错误信息
44
+ * @param error 错误信息
45
+ * @returns 错误信息
46
+ */
47
+ getErrors: function (error) {
48
+ if (typeof error === 'object' && error !== null && 'errors' in error) {
49
+ var messageToCode_1 = function (str) {
50
+ var regex = /[a-zA-Z0-9]+/g;
51
+ var matches = str.match(regex) || [];
52
+ return matches.map(function (word) { return word.toLowerCase(); }).join('_');
53
+ };
54
+ return Reflect.get(error, 'errors').map(function (err) {
55
+ var _a;
56
+ var parts = ((_a = Reflect.get(err, 'message')) !== null && _a !== void 0 ? _a : '').split('.').map(function (part) {
57
+ return inflection.camelize(messageToCode_1(part));
58
+ });
59
+ var code = "ValidateError.".concat(parts.length ? parts.join('.') : 'Failed');
60
+ return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, err), { code: code });
61
+ });
62
+ }
63
+ return [
64
+ {
65
+ message: validateUtil.getErrorMessage(error),
66
+ code: 'Error',
67
+ fieldValue: '',
68
+ field: '',
69
+ },
70
+ ];
71
+ },
72
+ /**
73
+ * 校验数据
74
+ * @param rules 校验规则
75
+ * @param data 数据
76
+ * @returns 校验结果
77
+ */
78
+ validate: function (rules, data) { return (0, tslib_1.__awaiter)(void 0, void 0, void 0, function () {
79
+ var error_1;
80
+ return (0, tslib_1.__generator)(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0:
83
+ _a.trys.push([0, 2, , 3]);
84
+ return [4 /*yield*/, validateUtil.getSchema(rules).validate(data)];
85
+ case 1:
86
+ _a.sent();
87
+ return [2 /*return*/, {
88
+ success: true,
89
+ }];
90
+ case 2:
91
+ error_1 = _a.sent();
92
+ return [2 /*return*/, {
93
+ success: false,
94
+ errors: validateUtil.getErrors(error_1),
95
+ }];
96
+ case 3: return [2 /*return*/];
97
+ }
98
+ });
99
+ }); },
100
+ };
101
+ exports.default = validateUtil;
102
+ //# sourceMappingURL=validate.util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.util.js","sourceRoot":"","sources":["../../src/utils/validate.util.ts"],"names":[],"mappings":";;;;AAAA,kEAAyC;AAEzC,iFAkByB;AAEzB;IAAoC,+CAAc;IAAlD;;IAAoD,CAAC;IAAD,qBAAC;AAAD,CAAC,AAArD,CAAoC,yBAAc,GAAG;AAAxC,wCAAc;AAgC3B,WAAW;AACX,IAAM,YAAY,GAAG;IACnB;;;;OAIG;IACH,SAAS,EAAE,UAAC,KAAqC;QAC/C,IAAI,KAAK,YAAY,c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,CAAC;gBAEH,IAAM,IAAI,GAAG,wBAAiB,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAE,CAAC;gBAE1E,uDACK,GAAG,KACN,IAAI,MAAA,IACJ;YACJ,CAAC,CAAC,CAAC;SACJ;QAED,OAAO;YACL;gBACE,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,KAAK,CAAC;gBAC5C,IAAI,EAAE,OAAO;gBACb,UAAU,EAAE,EAAE;gBACd,KAAK,EAAE,EAAE;aACV;SACiB,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,QAAQ,EAAE,UAAO,KAAqC,EAAE,IAAoB;;;;;;oBAExE,qBAAM,YAAY,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAA;;oBAAlD,SAAkD,CAAC;oBACnD,sBAAO;4BACL,OAAO,EAAE,IAAI;yBACd,EAAC;;;oBAEF,sBAAO;4BACL,OAAO,EAAE,KAAK;4BACd,MAAM,EAAE,YAAY,CAAC,SAAS,CAAC,OAAK,CAAC;yBACtC,EAAC;;;;SAEL;CACF,CAAC;AAEF,kBAAe,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "almighty-tool",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "Almighty Tool",
5
5
  "author": "happy",
6
6
  "private": false,
@@ -36,10 +36,11 @@
36
36
  },
37
37
  "typings": "types/index.d.ts",
38
38
  "dependencies": {
39
+ "async-validator": "^4.2.5",
39
40
  "base64-js": "^1.5.1",
40
41
  "dayjs": "^1.11.7",
41
42
  "deepmerge": "^4.3.0",
42
- "inflection": "^3.0.0",
43
+ "inflection": "^1.13.4",
43
44
  "is-what": "^4.1.8",
44
45
  "lodash": "^4.17.21",
45
46
  "mustache": "^4.2.0",
@@ -60,6 +61,7 @@
60
61
  "@babel/preset-typescript": "^7.21.0",
61
62
  "@types/debug": "^4.1.8",
62
63
  "@types/html5plus": "1.0.2",
64
+ "@types/inflection": "^1.13.2",
63
65
  "@types/jest": "29.4.0",
64
66
  "@types/lodash": "4.14.191",
65
67
  "@types/mustache": "^4.2.5",
@@ -68,7 +68,10 @@ const codeUtil = {
68
68
  const template = fs.readFileSync(templatePath, 'utf-8');
69
69
 
70
70
  // 使用变量渲染模板
71
- const result = mustache.render(template, variables);
71
+ const result = mustache
72
+ .render(template, variables)
73
+ .replace(/&lbrace;/g, '{')
74
+ .replace(/&rbrace;/g, '}');
72
75
 
73
76
  // 写入目标文件
74
77
  fs.writeFileSync(targetPath, result, 'utf-8');