Open4
NestJS メモ

Response Validator

これでも解決しないですな...

このあたりも確認してますが...
// tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}

class 側にメソッド名と紐付けたメタデータが存在しているため、メソッド名を正しく付けてやる必要がありました。解決!
Object.defineProperty(descriptor.value, 'name', { value: propertyName, writable: false });
フルコード(copyMethodMetadata は今回の主たる問題とは関係ないが、場合によっては必要なはず)
import 'reflect-metadata';
import { plainToInstance } from "class-transformer";
import { validateOrReject } from "class-validator";
function copyMethodMetadata(source: Function, target: Function) {
for (const key of Reflect.getMetadataKeys(source)) {
Reflect.defineMetadata(key, Reflect.getMetadata(key,source), target);
}
}
export function ValidateResponse<T>(classType: { new (...args: any[]): T }) {
return function (target: Object, propertyName: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
const response = await originalMethod.apply(this, args)
const responseAsDto = plainToInstance(classType, response) as object;
await validateOrReject(responseAsDto);
return response;
}
Object.defineProperty(descriptor.value, 'name', { value: propertyName, writable: false });
copyMethodMetadata(originalMethod, descriptor.value);
}
}