使用接口作为招摇模式 - NestJS DTO

问题描述

我有以下代码

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;
}

enter image description here

我正在尝试将接口 Product 作为 swagger 上的架构,但它不起作用,我收到错误消息。 有什么想法吗?

解决方法

type 属性需要一个在运行时可用的值,例如实际的 Class 或 Javascript String 类型。接口仅在编译时存在,因此以这种方式传递它们是无效的。

如果你想手动传递它,你必须把它转换成一个类,否则你可以看看使用 NestJS Swagger 编译器插件,它使用一些很酷的编译时魔法来自动尝试和计算其中的一些东西为你而来

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...