node.js - URL 输入验证 NestJs

标签 node.js nestjs class-validator

我正在尝试对网站 URL 进行输入验证。我使用了 URL 装饰器,但是当我没有输入任何 URL 时,它会给我错误消息。我应该怎么做才能让它也接受空字符串?

  @ApiProperty()
  @IsString()
  @IsUrl(undefined, { message: 'Company URL is not valid.' })
  companyURL: string;

最佳答案

您可以使用 @IsOptional() 类验证器或 @IsDefined(value: any)

@IsOptional()

Checks if given value is empty (=== null, === undefined) and if so, ignores all the validators on the property.

@IsDefined(值:任意)

Checks if value is defined (!== undefined, !== null). This is the only decorator that ignores skipMissingProperties option.

如果您需要更多信息,请查看 documentation about class-validators

关于node.js - URL 输入验证 NestJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66335078/

相关文章:

node.js - 异步功能不起作用

javascript - 如何使用dust.js在服务器和客户端上编写模板

nestjs - TypeORM 问题保存实体级联 true

mysql - 在 TypeORM 中使用 QueryBuilder 与 Repository 有哪些不同的用例?

javascript - 使用类验证器中的 @MinDate 会导致 TypeError : Cannot read property 'getTime' of undefined?

node.js - nodejs http.request保存在全局变量中

node.js - 为什么 redis 命令不能在我的 CoffeeScript 文件的 mocha 测试中运行?

typescript - 共享自定义 NestJS 模块出现 "not a part of the currently processed module"错误

javascript - 如何在类型中传递泛型类型?

node.js - 在 NestJs 项目中使用 i18n 库翻译类验证错误消息