我是NestJS的新手,正在尝试从查询参数填充过滤器DTO。
这是我所拥有的:
查询:
本地主机:3000 / api / checklists?stations = 114630,114666,114667,114668
控制者
@Get()
public async getChecklists(@Query(ValidationPipe) filter: ChecklistFilter): Promise<ChecklistDto[]> {
// ...
}
DTO
export class ChecklistFilter {
@IsOptional()
@IsArray()
@IsString({ each: true })
@Type(() => String)
@Transform((value: string) => value.split(','))
stations?: string[];
// ...
}
这样,类验证器就不会抱怨,但是,在过滤器对象站中实际上不是数组,而是单个字符串。
我想将其转换为验证管道内的数组。我该如何实现?
您可以传递ValidationPipe
而不是类的实例,这样做时,可以传递诸如transform: true
将生成class-validator
并 class-transformer
运行的选项,这些选项应将转换后的值传回。
@Get()
public async getChecklists(@Query(new ValidationPipe({ transform: true })) filter: ChecklistFilter): Promise<ChecklistDto[]> {
// ...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句