| 56 | } |
| 57 | |
| 58 | function _descriptor(v: ParamOption<any>) { |
| 59 | return function desc(this: Handler, target: any, funcName: string, obj: any) { |
| 60 | target.__param ||= {}; |
| 61 | target.__param[target.constructor.name] ||= {}; |
| 62 | if (!target.__param[target.constructor.name][funcName]) { |
| 63 | const originalMethod = obj.value; |
| 64 | const val = originalMethod.toString(); |
| 65 | const firstArg = val.split(')')[0]?.split(',')[0]?.split('(')[1]?.trim() || ''; |
| 66 | const domainIdStyle = firstArg.toLowerCase().startsWith('domainid'); |
| 67 | target.__param[target.constructor.name][funcName] = []; |
| 68 | obj.value = function validate(this: Handler, rawArgs: any, ...extra: any[]) { |
| 69 | if (typeof rawArgs !== 'object' || extra.length) return originalMethod.call(this, rawArgs, ...extra); |
| 70 | const c = []; |
| 71 | const arglist: ParamOption<any>[] = target.__param[target.constructor.name][funcName]; |
| 72 | if (typeof rawArgs.domainId !== 'string' || !rawArgs.domainId) throw new ValidationError('domainId'); |
| 73 | for (const item of arglist) { |
| 74 | const src = item.source === 'all' |
| 75 | ? rawArgs |
| 76 | : item.source === 'get' |
| 77 | ? this.request.query |
| 78 | : item.source === 'route' |
| 79 | ? { ...this.request.params, domainId: this.args.domainId } |
| 80 | : this.request.body; |
| 81 | const value = src[item.name]; |
| 82 | if (!item.isOptional || value) { |
| 83 | if (value === undefined || value === null || value === '') throw new ValidationError(item.name); |
| 84 | if (item.validate && !item.validate(value)) throw new ValidationError(item.name); |
| 85 | if (item.convert) c.push(item.convert(value)); |
| 86 | else c.push(value); |
| 87 | } else if (item.isOptional === 'convert') { |
| 88 | c.push(item.convert ? item.convert(value) : value); |
| 89 | } else c.push(undefined); |
| 90 | } |
| 91 | return domainIdStyle ? originalMethod.call(this, rawArgs.domainId, ...c) : originalMethod.call(this, rawArgs, ...c); |
| 92 | }; |
| 93 | } |
| 94 | target.__param[target.constructor.name][funcName].unshift(v); |
| 95 | return obj; |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | type DescriptorBuilder = |
| 100 | ((name: string, type: Type<any>) => MethodDecorator) |