koatty_validation 1.0.4 → 1.0.12

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/dist/index.d.ts CHANGED
@@ -1,19 +1,9 @@
1
- import { ValidRules } from './util';
2
- export { checkParams, checkParamsType, convertParamsType, paramterTypes, plainToClass, PARAM_TYPE_KEY, PARAM_RULE_KEY, PARAM_CHECK_KEY } from "./lib";
3
- export * from "./util";
4
1
  /**
5
- * Validation parameter's type and values.
6
- *
7
- * @export
8
- * @param {(ValidRules | ValidRules[] | Function)} rule
9
- * @param {string} [message]
10
- * @returns {ParameterDecorator}
2
+ * @ author: richen
3
+ * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
4
+ * @ license: MIT
5
+ * @ version: 2020-03-20 11:31:09
11
6
  */
12
- export declare function Valid(rule: ValidRules | ValidRules[] | Function, message?: string): ParameterDecorator;
13
- /**
14
- * Validation parameter's type and values from DTO class.
15
- *
16
- * @export
17
- * @returns {MethodDecorator}
18
- */
19
- export declare function Validated(): MethodDecorator;
7
+ export * from "./rule";
8
+ export * from "./decorator";
9
+ export { checkParamsType, convertParamsType, convertDtoParamsType, plainToClass, getOriginMetadata } from "./util";
package/dist/index.js CHANGED
@@ -1,98 +1,21 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Validated = exports.Valid = exports.PARAM_CHECK_KEY = exports.PARAM_RULE_KEY = exports.PARAM_TYPE_KEY = exports.plainToClass = exports.paramterTypes = exports.convertParamsType = exports.checkParamsType = exports.checkParams = void 0;
4
- const tslib_1 = require("tslib");
5
2
  /**
6
3
  * @ author: richen
7
4
  * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
8
5
  * @ license: MIT
9
6
  * @ version: 2020-03-20 11:31:09
10
7
  */
11
- const helper = tslib_1.__importStar(require("koatty_lib"));
12
- const lib_1 = require("./lib");
13
- const koatty_container_1 = require("koatty_container");
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.getOriginMetadata = exports.plainToClass = exports.convertDtoParamsType = exports.convertParamsType = exports.checkParamsType = void 0;
10
+ const tslib_1 = require("tslib");
14
11
  // export for manual verification
15
- var lib_2 = require("./lib");
16
- Object.defineProperty(exports, "checkParams", { enumerable: true, get: function () { return lib_2.checkParams; } });
17
- Object.defineProperty(exports, "checkParamsType", { enumerable: true, get: function () { return lib_2.checkParamsType; } });
18
- Object.defineProperty(exports, "convertParamsType", { enumerable: true, get: function () { return lib_2.convertParamsType; } });
19
- Object.defineProperty(exports, "paramterTypes", { enumerable: true, get: function () { return lib_2.paramterTypes; } });
20
- Object.defineProperty(exports, "plainToClass", { enumerable: true, get: function () { return lib_2.plainToClass; } });
21
- Object.defineProperty(exports, "PARAM_TYPE_KEY", { enumerable: true, get: function () { return lib_2.PARAM_TYPE_KEY; } });
22
- Object.defineProperty(exports, "PARAM_RULE_KEY", { enumerable: true, get: function () { return lib_2.PARAM_RULE_KEY; } });
23
- Object.defineProperty(exports, "PARAM_CHECK_KEY", { enumerable: true, get: function () { return lib_2.PARAM_CHECK_KEY; } });
24
- tslib_1.__exportStar(require("./util"), exports);
12
+ (0, tslib_1.__exportStar)(require("./rule"), exports);
13
+ (0, tslib_1.__exportStar)(require("./decorator"), exports);
14
+ var util_1 = require("./util");
15
+ Object.defineProperty(exports, "checkParamsType", { enumerable: true, get: function () { return util_1.checkParamsType; } });
16
+ Object.defineProperty(exports, "convertParamsType", { enumerable: true, get: function () { return util_1.convertParamsType; } });
17
+ Object.defineProperty(exports, "convertDtoParamsType", { enumerable: true, get: function () { return util_1.convertDtoParamsType; } });
18
+ Object.defineProperty(exports, "plainToClass", { enumerable: true, get: function () { return util_1.plainToClass; } });
19
+ Object.defineProperty(exports, "getOriginMetadata", { enumerable: true, get: function () { return util_1.getOriginMetadata; } });
25
20
  // export decorators from class-validator
26
- // export { IsHash } from "class-validator";
27
- /**
28
- * Validation parameter's type and values.
29
- *
30
- * @export
31
- * @param {(ValidRules | ValidRules[] | Function)} rule
32
- * @param {string} [message]
33
- * @returns {ParameterDecorator}
34
- */
35
- function Valid(rule, message) {
36
- let rules = [];
37
- if (helper.isString(rule)) {
38
- rules = rule.split(",");
39
- }
40
- else {
41
- rules = rule;
42
- }
43
- return (target, propertyKey, descriptor) => {
44
- // 获取成员参数类型
45
- const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
46
- const type = (paramTypes[descriptor] && paramTypes[descriptor].name) ? paramTypes[descriptor].name : "object";
47
- koatty_container_1.IOCContainer.attachPropertyData(lib_1.PARAM_RULE_KEY, {
48
- name: propertyKey,
49
- rule: rules,
50
- message,
51
- index: descriptor,
52
- type
53
- }, target, propertyKey);
54
- };
55
- }
56
- exports.Valid = Valid;
57
- /**
58
- * Validation parameter's type and values from DTO class.
59
- *
60
- * @export
61
- * @returns {MethodDecorator}
62
- */
63
- function Validated() {
64
- return (target, propertyKey, descriptor) => {
65
- //
66
- koatty_container_1.IOCContainer.savePropertyData(lib_1.PARAM_CHECK_KEY, {
67
- dtoCheck: 1
68
- }, target, propertyKey);
69
- // 获取成员参数类型
70
- // const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey) || [];
71
- // const { value, configurable, enumerable } = descriptor;
72
- // descriptor = {
73
- // configurable,
74
- // enumerable,
75
- // writable: true,
76
- // value: async function valid(...props: any[]) {
77
- // const ps: any[] = [];
78
- // // tslint:disable-next-line: no-unused-expression
79
- // (props || []).map((value: any, index: number) => {
80
- // const type = (paramTypes[index] && paramTypes[index].name) ? paramTypes[index].name : "any";
81
- // if (!paramterTypes[type]) {
82
- // ps.push(ClassValidator.valid(paramTypes[index], value, true));
83
- // } else {
84
- // ps.push(Promise.resolve(value));
85
- // }
86
- // });
87
- // if (ps.length > 0) {
88
- // props = await Promise.all(ps);
89
- // }
90
- // // tslint:disable-next-line: no-invalid-this
91
- // return value.apply(this, props);
92
- // }
93
- // };
94
- // return descriptor;
95
- };
96
- }
97
- exports.Validated = Validated;
98
21
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA;;;;;GAKG;AACH,2DAAqC;AAErC,+BAAwD;AACxD,uDAAgD;AAEhD,iCAAiC;AACjC,6BAAsJ;AAA7I,kGAAA,WAAW,OAAA;AAAE,sGAAA,eAAe,OAAA;AAAE,wGAAA,iBAAiB,OAAA;AAAE,oGAAA,aAAa,OAAA;AAAE,mGAAA,YAAY,OAAA;AAAE,qGAAA,cAAc,OAAA;AAAE,qGAAA,cAAc,OAAA;AAAE,sGAAA,eAAe,OAAA;AACtI,iDAAuB;AACvB,yCAAyC;AACzC,4CAA4C;AAE5C;;;;;;;GAOG;AACH,SAAgB,KAAK,CAAC,IAA0C,EAAE,OAAgB;IAC9E,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;QACvB,KAAK,GAAY,IAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KACrC;SAAM;QACH,KAAK,GAAG,IAAI,CAAC;KAChB;IACD,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,UAAe,EAAE,EAAE;QACzD,WAAW;QACX,MAAM,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,mBAAmB,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACjF,MAAM,IAAI,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC;QAE9G,+BAAY,CAAC,kBAAkB,CAAC,oBAAc,EAAE;YAC5C,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE,KAAK;YACX,OAAO;YACP,KAAK,EAAE,UAAU;YACjB,IAAI;SACP,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC5B,CAAC,CAAC;AACN,CAAC;AApBD,sBAoBC;AAED;;;;;GAKG;AACH,SAAgB,SAAS;IACrB,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,UAA8B,EAAE,EAAE;QACxE,EAAE;QACF,+BAAY,CAAC,gBAAgB,CAAC,qBAAe,EAAE;YAC3C,QAAQ,EAAE,CAAC;SACd,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QAExB,WAAW;QACX,0FAA0F;QAE1F,0DAA0D;QAC1D,iBAAiB;QACjB,oBAAoB;QACpB,kBAAkB;QAClB,sBAAsB;QACtB,qDAAqD;QACrD,gCAAgC;QAChC,4DAA4D;QAC5D,6DAA6D;QAC7D,2GAA2G;QAC3G,0CAA0C;QAC1C,iFAAiF;QACjF,uBAAuB;QACvB,mDAAmD;QACnD,gBAAgB;QAChB,cAAc;QACd,+BAA+B;QAC/B,6CAA6C;QAC7C,YAAY;QACZ,uDAAuD;QACvD,2CAA2C;QAC3C,QAAQ;QACR,KAAK;QACL,qBAAqB;IACzB,CAAC,CAAC;AACN,CAAC;AAnCD,8BAmCC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,iCAAiC;AACjC,sDAAuB;AACvB,2DAA4B;AAC5B,+BAIgB;AAHZ,uGAAA,eAAe,OAAA;AAAE,yGAAA,iBAAiB,OAAA;AAClC,4GAAA,oBAAoB,OAAA;AAAE,oGAAA,YAAY,OAAA;AAClC,yGAAA,iBAAiB,OAAA;AAErB,yCAAyC"}
package/dist/rule.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
2
+ export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
3
+ export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
4
+ export declare const ENABLE_VALIDATED = "ENABLE_VALIDATED";
5
+ /**
6
+ * paramterTypes
7
+ *
8
+ * @export
9
+ * @enum {number}
10
+ */
11
+ export declare enum paramterTypes {
12
+ "Number" = 0,
13
+ "number" = 1,
14
+ "String" = 2,
15
+ "string" = 3,
16
+ "Boolean" = 4,
17
+ "boolean" = 5,
18
+ "Array" = 6,
19
+ "array" = 7,
20
+ "Tuple" = 8,
21
+ "tuple" = 9,
22
+ "Object" = 10,
23
+ "object" = 11,
24
+ "Enum" = 12,
25
+ "enum" = 13,
26
+ "Bigint" = 14,
27
+ "bigint" = 15,
28
+ "Null" = 16,
29
+ "null" = 17,
30
+ "Undefined" = 18,
31
+ "undefined" = 19
32
+ }
33
+ /**
34
+ * type checked rules
35
+ *
36
+ * @export
37
+ * @type {number}
38
+ */
39
+ export declare type ValidRules = "IsNotEmpty" | "IsDate" | "IsEmail" | "IsIP" | "IsPhoneNumber" | "IsUrl" | "IsHash" | "IsCnName" | "IsIdNumber" | "IsZipCode" | "IsMobile" | "IsPlateNumber";
40
+ declare class ValidateClass {
41
+ private static instance;
42
+ private constructor();
43
+ /**
44
+ *
45
+ *
46
+ * @static
47
+ * @returns
48
+ * @memberof ValidateUtil
49
+ */
50
+ static getInstance(): ValidateClass;
51
+ /**
52
+ * validated data vs dto class
53
+ *
54
+ * @param {*} Clazz
55
+ * @param {*} data
56
+ * @param {boolean} [convert=false] auto convert parameters type
57
+ * @returns {Promise<any>}
58
+ * @memberof ValidateClass
59
+ */
60
+ valid(Clazz: any, data: any, convert?: boolean): Promise<any>;
61
+ }
62
+ /**
63
+ * ClassValidator for manual
64
+ */
65
+ export declare const ClassValidator: ValidateClass;
66
+ /**
67
+ * Validator Functions
68
+ */
69
+ export declare const FunctionValidator: any;
70
+ /**
71
+ * Use functions or built-in rules for validation.
72
+ *
73
+ * @export
74
+ * @param {string} name
75
+ * @param {*} value
76
+ * @param {string} type
77
+ * @param {(ValidRules | ValidRules[] | Function)} rule
78
+ * @param {string} [message]
79
+ * @param {boolean} [checkType=true]
80
+ * @returns
81
+ */
82
+ export declare function ValidatorFuncs(name: string, value: any, type: string, rule: ValidRules | ValidRules[] | Function, message?: string, checkType?: boolean): any;
83
+ export {};
package/dist/rule.js ADDED
@@ -0,0 +1,291 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidatorFuncs = exports.FunctionValidator = exports.ClassValidator = exports.paramterTypes = exports.ENABLE_VALIDATED = exports.PARAM_CHECK_KEY = exports.PARAM_RULE_KEY = exports.PARAM_TYPE_KEY = void 0;
4
+ const tslib_1 = require("tslib");
5
+ /*
6
+ * @Description:
7
+ * @Usage:
8
+ * @Author: richen
9
+ * @Date: 2021-11-25 10:47:04
10
+ * @LastEditTime: 2021-11-25 11:07:02
11
+ */
12
+ const helper = (0, tslib_1.__importStar)(require("koatty_lib"));
13
+ const util_1 = require("./util");
14
+ const class_validator_1 = require("class-validator");
15
+ // constant
16
+ exports.PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
17
+ exports.PARAM_RULE_KEY = 'PARAM_RULE_KEY';
18
+ exports.PARAM_CHECK_KEY = 'PARAM_CHECK_KEY';
19
+ exports.ENABLE_VALIDATED = "ENABLE_VALIDATED";
20
+ /**
21
+ * paramterTypes
22
+ *
23
+ * @export
24
+ * @enum {number}
25
+ */
26
+ var paramterTypes;
27
+ (function (paramterTypes) {
28
+ paramterTypes[paramterTypes["Number"] = 0] = "Number";
29
+ paramterTypes[paramterTypes["number"] = 1] = "number";
30
+ paramterTypes[paramterTypes["String"] = 2] = "String";
31
+ paramterTypes[paramterTypes["string"] = 3] = "string";
32
+ paramterTypes[paramterTypes["Boolean"] = 4] = "Boolean";
33
+ paramterTypes[paramterTypes["boolean"] = 5] = "boolean";
34
+ paramterTypes[paramterTypes["Array"] = 6] = "Array";
35
+ paramterTypes[paramterTypes["array"] = 7] = "array";
36
+ paramterTypes[paramterTypes["Tuple"] = 8] = "Tuple";
37
+ paramterTypes[paramterTypes["tuple"] = 9] = "tuple";
38
+ paramterTypes[paramterTypes["Object"] = 10] = "Object";
39
+ paramterTypes[paramterTypes["object"] = 11] = "object";
40
+ paramterTypes[paramterTypes["Enum"] = 12] = "Enum";
41
+ paramterTypes[paramterTypes["enum"] = 13] = "enum";
42
+ paramterTypes[paramterTypes["Bigint"] = 14] = "Bigint";
43
+ paramterTypes[paramterTypes["bigint"] = 15] = "bigint";
44
+ paramterTypes[paramterTypes["Null"] = 16] = "Null";
45
+ paramterTypes[paramterTypes["null"] = 17] = "null";
46
+ paramterTypes[paramterTypes["Undefined"] = 18] = "Undefined";
47
+ paramterTypes[paramterTypes["undefined"] = 19] = "undefined";
48
+ })(paramterTypes = exports.paramterTypes || (exports.paramterTypes = {}));
49
+ class ValidateClass {
50
+ constructor() {
51
+ }
52
+ /**
53
+ *
54
+ *
55
+ * @static
56
+ * @returns
57
+ * @memberof ValidateUtil
58
+ */
59
+ static getInstance() {
60
+ return this.instance || (this.instance = new ValidateClass());
61
+ }
62
+ /**
63
+ * validated data vs dto class
64
+ *
65
+ * @param {*} Clazz
66
+ * @param {*} data
67
+ * @param {boolean} [convert=false] auto convert parameters type
68
+ * @returns {Promise<any>}
69
+ * @memberof ValidateClass
70
+ */
71
+ async valid(Clazz, data, convert = false) {
72
+ let obj = {};
73
+ if (data instanceof Clazz) {
74
+ obj = data;
75
+ }
76
+ else {
77
+ obj = (0, util_1.plainToClass)(Clazz, data, convert);
78
+ }
79
+ let errors = [];
80
+ if (convert) {
81
+ errors = await (0, class_validator_1.validate)(obj);
82
+ }
83
+ else {
84
+ errors = await (0, class_validator_1.validate)(obj, { skipMissingProperties: true });
85
+ }
86
+ if (errors.length > 0) {
87
+ const err = new Error(Object.values(errors[0].constraints)[0]);
88
+ err.code = 400;
89
+ err.status = 400;
90
+ throw err;
91
+ }
92
+ return obj;
93
+ }
94
+ }
95
+ /**
96
+ * ClassValidator for manual
97
+ */
98
+ exports.ClassValidator = ValidateClass.getInstance();
99
+ /**
100
+ * Validator Functions
101
+ */
102
+ exports.FunctionValidator = {
103
+ /**
104
+ * Checks if value matches ("===") the comparison.
105
+ */
106
+ Equals: (value, comparison) => {
107
+ return (0, class_validator_1.equals)(value, comparison);
108
+ },
109
+ /**
110
+ * Checks if value does not match ("!==") the comparison.
111
+ */
112
+ NotEquals: (value, comparison) => {
113
+ return (0, class_validator_1.notEquals)(value, comparison);
114
+ },
115
+ /**
116
+ * Checks if the string contains the seed. If given value is not a string, then it returns false.
117
+ */
118
+ Contains: (value, seed) => {
119
+ return (0, class_validator_1.contains)(value, seed);
120
+ },
121
+ /**
122
+ * Checks if given value is in a array of allowed values.
123
+ */
124
+ IsIn: (value, possibleValues) => {
125
+ return (0, class_validator_1.isIn)(value, possibleValues);
126
+ },
127
+ /**
128
+ * Checks if given value not in a array of allowed values.
129
+ */
130
+ IsNotIn: (value, possibleValues) => {
131
+ return (0, class_validator_1.isNotIn)(value, possibleValues);
132
+ },
133
+ /**
134
+ * Checks if a given value is a real date.
135
+ */
136
+ IsDate: (value) => {
137
+ return helper.isDate(value);
138
+ },
139
+ /**
140
+ * Checks if the first number is greater than or equal to the second.
141
+ */
142
+ Min: (num, min) => {
143
+ return helper.toNumber(num) >= min;
144
+ },
145
+ /**
146
+ * Checks if the first number is less than or equal to the second.
147
+ */
148
+ Max: (num, max) => {
149
+ return helper.toNumber(num) <= max;
150
+ },
151
+ /**
152
+ * Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
153
+ * If given value is not a string, then it returns false.
154
+ */
155
+ Length: (value, min, max) => {
156
+ return (0, class_validator_1.length)(value, min, max);
157
+ },
158
+ /**
159
+ * Checks if the string is an email. If given value is not a string, then it returns false.
160
+ */
161
+ IsEmail: (value, options) => {
162
+ return (0, class_validator_1.isEmail)(value, options);
163
+ },
164
+ /**
165
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
166
+ */
167
+ IsIP: (value, version) => {
168
+ return (0, class_validator_1.isIP)(value, version);
169
+ },
170
+ /**
171
+ * Checks if the string is a valid phone number.
172
+ * @param value — the potential phone number string to test
173
+ * @param region 2 characters uppercase country code (e.g. DE, US, CH). If users must enter the intl.
174
+ * prefix (e.g. +41), then you may pass "ZZ" or null as region.
175
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
176
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
177
+ */
178
+ IsPhoneNumber: (value, region) => {
179
+ return (0, class_validator_1.isPhoneNumber)(value, region);
180
+ },
181
+ /**
182
+ * Checks if the string is an url. If given value is not a string, then it returns false.
183
+ */
184
+ IsUrl: (value, options) => {
185
+ return (0, class_validator_1.isURL)(value, options);
186
+ },
187
+ /**
188
+ * check if the string is a hash of type algorithm. Algorithm is one of
189
+ * ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
190
+ */
191
+ IsHash: (value, algorithm) => {
192
+ return (0, class_validator_1.isHash)(value, algorithm);
193
+ },
194
+ /**
195
+ * Checks if value is a chinese name.
196
+ */
197
+ IsCnName: (value) => {
198
+ if (!helper.isString(value)) {
199
+ return false;
200
+ }
201
+ return (0, util_1.cnName)(value);
202
+ },
203
+ /**
204
+ * Checks if value is a idcard number.
205
+ */
206
+ IsIdNumber: (value) => {
207
+ if (!helper.isString(value)) {
208
+ return false;
209
+ }
210
+ return (0, util_1.idNumber)(value);
211
+ },
212
+ /**
213
+ * Checks if value is a zipCode.
214
+ */
215
+ IsZipCode: (value) => {
216
+ if (!helper.isString(value)) {
217
+ return false;
218
+ }
219
+ return (0, util_1.zipCode)(value);
220
+ },
221
+ /**
222
+ * Checks if value is a mobile phone number.
223
+ */
224
+ IsMobile: (value) => {
225
+ if (!helper.isString(value)) {
226
+ return false;
227
+ }
228
+ return (0, util_1.mobile)(value);
229
+ },
230
+ /**
231
+ * Checks if value is a plateNumber.
232
+ */
233
+ IsPlateNumber: (value) => {
234
+ if (!helper.isString(value)) {
235
+ return false;
236
+ }
237
+ return (0, util_1.plateNumber)(value);
238
+ },
239
+ /**
240
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
241
+ * tabs, formfeeds, etc.), returns false
242
+ */
243
+ IsNotEmpty: (value) => {
244
+ return !helper.isEmpty(value);
245
+ }
246
+ };
247
+ /**
248
+ * Use functions or built-in rules for validation.
249
+ *
250
+ * @export
251
+ * @param {string} name
252
+ * @param {*} value
253
+ * @param {string} type
254
+ * @param {(ValidRules | ValidRules[] | Function)} rule
255
+ * @param {string} [message]
256
+ * @param {boolean} [checkType=true]
257
+ * @returns
258
+ */
259
+ function ValidatorFuncs(name, value, type, rule, message, checkType = true) {
260
+ // check type
261
+ if (checkType && !(0, util_1.checkParamsType)(value, type)) {
262
+ const err = new Error(`TypeError: invalid arguments '${name}'.`);
263
+ err.code = 400;
264
+ err.status = 400;
265
+ throw err;
266
+ }
267
+ if (helper.isFunction(rule)) {
268
+ if (!rule(value)) {
269
+ const err = new Error(message || `ValidatorError: invalid arguments[${name}].`);
270
+ err.code = 400;
271
+ err.status = 400;
272
+ throw err;
273
+ }
274
+ return value;
275
+ }
276
+ else {
277
+ const funcs = rule;
278
+ if (helper.isString(rule)) {
279
+ funcs.push(rule);
280
+ }
281
+ if (funcs.some((it) => exports.FunctionValidator[it] && !exports.FunctionValidator[it](value))) {
282
+ const err = new Error(message || `ValidatorError: invalid arguments[${name}].`);
283
+ err.code = 400;
284
+ err.status = 400;
285
+ throw err;
286
+ }
287
+ }
288
+ return value;
289
+ }
290
+ exports.ValidatorFuncs = ValidatorFuncs;
291
+ //# sourceMappingURL=rule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule.js","sourceRoot":"","sources":["../src/rule.ts"],"names":[],"mappings":";;;;AAAA;;;;;;GAMG;AACH,gEAAqC;AAGrC,iCAAuG;AACvG,qDAA6J;AAE7J,WAAW;AACE,QAAA,cAAc,GAAG,gBAAgB,CAAC;AAClC,QAAA,cAAc,G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