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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With