Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Joi uri validation optional

Tags:

reactjs

joi

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

like image 903
o..o Avatar asked Mar 01 '26 08:03

o..o


1 Answers

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'
}
like image 72
soltex Avatar answered Mar 02 '26 21:03

soltex



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!