`returnError` option for `validate` and `validateSync`
See original GitHub issueWould 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:
- Created 6 years ago
- Reactions:5
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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