2
votes

I'm using Joi validation in React with this schema:

...
intro_video_link:
    Joi.string()
    .uri()
    .label("Intro Video"),
...

The intro_video_link is also required. I want Joi to validate field for uri if it is filled, but also allow it to be empty. There is no .required() called.

How?

Thank you

1

1 Answers

4
votes

You probably want to combine .allow() with .required():

 intro_video_link:  Joi.string().uri().label("Intro Video").required().allow('')

This way, the following objects will pass the validation:

{
   intro_video_link: ''
}


{
   intro_video_link: 'https://github.com'
}