问题描述
import { IsNotEmpty,IsArray,ArrayMinSize } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class PublishDto {
@IsNotEmpty()
@IsArray()
@ArrayMinSize(1)
@ApiProperty({
type: [Product]
})
products: Product[];
}
interface Product {
id: string;
title: string;
sku: string;
stock: number;
description: string;
shortDescription: string;
imagesUrl: string[];
price: number;
department: string;
category: string;
brand: string;
keywords: string[];
isActive: boolean;
}
我正在尝试将接口 Product
作为 swagger 上的架构,但它不起作用,我收到错误消息。
有什么想法吗?
解决方法
type 属性需要一个在运行时可用的值,例如实际的 Class 或 Javascript String
类型。接口仅在编译时存在,因此以这种方式传递它们是无效的。
如果你想手动传递它,你必须把它转换成一个类,否则你可以看看使用 NestJS Swagger 编译器插件,它使用一些很酷的编译时魔法来自动尝试和计算其中的一些东西为你而来