question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`returnError` option for `validate` and `validateSync`

See original GitHub issue

Would you accept a PR for a returnError option for validate and validateSync? The default would be false. This would allow people to write their own assertion and validation methods that augment e.message or any part of e without having to wrap validateSync in a try, catch block.

const schema = yup.object().shape({ name: yup.string().required() };)
const validate = (value) => {
  const result = schema.validateSync(value, { returnError: true });
  if (result instanceof Error) {
     result.message = `[mine] ${result.message} for the user data type.`;
  }

  return result;
};

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
tecfucommented, Feb 3, 2020

whats wrong with wrapping in a try catch block? I think throwing an exception is a reasonable interface here, and avoids needing to check if result is an error

Runtime Errors.

A Runtime Error could be thrown from your yup validation call, which the pattern here will always incorrectly handle as a Validation Error.

That’s clunky and a potential security risk for developers for whom it is not obvious to.

Consequently, the recommended usage for yup.validateSync() should actually go something like this:

try {
    yup.validateSync(user)
}
catch (err) {
    if (err.name === 'ValidationError' ) throw new ValidationError(err)         
    throw new Error(err)
}
2reactions
jquensecommented, Nov 6, 2017

whats wrong with wrapping in a try catch block? I think throwing an exception is a reasonable interface here, and avoids needing to check if result is an error

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between validate and validateSync in mongoose
It returns ValidationError if there are errors during validation, or undefined if there is no error. const err = course.validateSync(); if (err) ...
Read more >
How to use Yup context variables in Formik validation
This article demonstrates the power of custom functions with Yup context variables and Formik validation in React forms.
Read more >
Schemas | Zuze - GitHub Pages
validateSync (schema,"ture") // ValidationError - must be a boolean ... If accepts an option of multi: true to return error messages as an...
Read more >
Simple React form validation with Formik, Yup and/or Spected
We I use just simple value => value === true . Next, we just call method validateSync() on this schema: function validate(values) {...
Read more >
Validation - Formik
Formik is designed to manage forms with complex validation with ease. ... <Formik> and withFormik() take a prop/option called validate that accepts either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found