Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could take optional, if not required.

myKey: oi.string()[modifier ? 'required' : 'optional']()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading