gpt4 book ai didi

node.js - URL 输入验证 NestJs

转载 作者:行者123 更新时间:2023-12-02 16:17:13 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com