我正在尝试对网站 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/