Can I use the data given to Joi.validate() function instead of the result.value that it returns

Viewed 145

The documented way:

 const data = {...someDataHere}


 const { error, value } = joi.validate(data, schema);

 if (error) return console.log(error);

    
...do stuff using `value` here

Using try/catch:

const data = {...someDataHere}

try {
  const { error, value } = joi.validate(data, schema);

  if (error) throw new Error(error)
} catch (error) {
  console.log(error);
}

...do stuff using `data` here since we can not access `value`

Question:

In regards to Joi validation library

Since I can't access value here because it's in a try/catch block, can I use data instead? Is it the exact same data all the time? And is safe/okay?

0 Answers
Related