UNPKG

think_validtion

Version:
98 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Validated = exports.Valid = void 0; const tslib_1 = require("tslib"); /** * @ author: richen * @ copyright: Copyright (c) - <richenlin(at)gmail.com> * @ license: MIT * @ version: 2020-03-20 11:31:09 */ const helper = tslib_1.__importStar(require("think_lib")); const lib_1 = require("./lib"); const think_container_1 = require("think_container"); // export for manual verification var lib_2 = require("./lib"); Object.defineProperty(exports, "checkParams", { enumerable: true, get: function () { return lib_2.checkParams; } }); Object.defineProperty(exports, "checkParamsType", { enumerable: true, get: function () { return lib_2.checkParamsType; } }); Object.defineProperty(exports, "convertParamsType", { enumerable: true, get: function () { return lib_2.convertParamsType; } }); Object.defineProperty(exports, "paramterTypes", { enumerable: true, get: function () { return lib_2.paramterTypes; } }); Object.defineProperty(exports, "plainToClass", { enumerable: true, get: function () { return lib_2.plainToClass; } }); Object.defineProperty(exports, "PARAM_TYPE_KEY", { enumerable: true, get: function () { return lib_2.PARAM_TYPE_KEY; } }); Object.defineProperty(exports, "PARAM_RULE_KEY", { enumerable: true, get: function () { return lib_2.PARAM_RULE_KEY; } }); Object.defineProperty(exports, "PARAM_CHECK_KEY", { enumerable: true, get: function () { return lib_2.PARAM_CHECK_KEY; } }); tslib_1.__exportStar(require("./util"), exports); // export decorators from class-validator // export { IsHash } from "class-validator"; /** * Validtion paramer's type and values. * * @export * @param {(ValidRules | ValidRules[] | Function)} rule * @param {string} [message] * @returns {ParameterDecorator} */ function Valid(rule, message) { let rules = []; if (helper.isString(rule)) { rules = rule.split(","); } else { rules = rule; } return (target, propertyKey, descriptor) => { // 获取成员参数类型 const paramtypes = Reflect.getMetadata("design:paramtypes", target, propertyKey); const type = (paramtypes[descriptor] && paramtypes[descriptor].name) ? paramtypes[descriptor].name : "object"; think_container_1.IOCContainer.attachPropertyData(lib_1.PARAM_RULE_KEY, { name: propertyKey, rule: rules, message, index: descriptor, type }, target, propertyKey); }; } exports.Valid = Valid; /** * Validtion paramer's type and values from DTO class. * * @export * @returns {MethodDecorator} */ function Validated() { return (target, propertyKey, descriptor) => { // think_container_1.IOCContainer.savePropertyData(lib_1.PARAM_CHECK_KEY, { dtoCheck: 1 }, target, propertyKey); // 获取成员参数类型 // const paramtypes = Reflect.getMetadata("design:paramtypes", target, propertyKey) || []; // const { value, configurable, enumerable } = descriptor; // descriptor = { // configurable, // enumerable, // writable: true, // value: async function valid(...props: any[]) { // const ps: any[] = []; // // tslint:disable-next-line: no-unused-expression // (props || []).map((value: any, index: number) => { // const type = (paramtypes[index] && paramtypes[index].name) ? paramtypes[index].name : "any"; // if (!paramterTypes[type]) { // ps.push(ClassValidator.valid(paramtypes[index], value, true)); // } else { // ps.push(Promise.resolve(value)); // } // }); // if (ps.length > 0) { // props = await Promise.all(ps); // } // // tslint:disable-next-line: no-invalid-this // return value.apply(this, props); // } // }; // return descriptor; }; } exports.Validated = Validated; //# sourceMappingURL=index.js.map