Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional chaining in JavaScript or TypeScript

Is there a syntax to add a function to a chain under conditions?

In this example, I would like myKey to be Joi.string().required() if modifier === true, but just Joi.string() if it is false:

function customJoi(modifier) {
  return Joi.object({
    myKey: Joi.string() //#If(modifier) .required() #EndIf
  });
}

I know I could do without this feature, with multiple steps. I'm just wondering if there is a nice way to write it concisely for large objects.

like image 401
Seeven Avatar asked Jul 21 '26 08:07

Seeven


1 Answers

You could take optional, if not required.

myKey: Joi.string()[modifier ? 'required' : 'optional']()
like image 124
Nina Scholz Avatar answered Jul 22 '26 20:07

Nina Scholz



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!