Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL input validation NestJs

I am trying an input validation on a website URL. I used the URL decorator but when I don't input any URL it gives me my error message. what should I do to make it accept empty string also?

  @ApiProperty()
  @IsString()
  @IsUrl(undefined, { message: 'Company URL is not valid.' })
  companyURL: string;
like image 713
johnsteel Avatar asked Oct 28 '25 08:10

johnsteel


1 Answers

You can use the @IsOptional() class-validator or the @IsDefined(value: any).

@IsOptional()

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

@IsDefined(value: any)

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

If you need more information about it, check the documentation about class-validators

like image 153
Hugo Sohm Avatar answered Oct 31 '25 00:10

Hugo Sohm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!