Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to run Yup validation in strict mode #1727

Closed
PatoBeltran opened this issue Aug 2, 2019 · 1 comment
Closed

Add a way to run Yup validation in strict mode #1727

PatoBeltran opened this issue Aug 2, 2019 · 1 comment
Labels

Comments

@PatoBeltran
Copy link
Contributor

PatoBeltran commented Aug 2, 2019

🚀 Feature request

Currently the code in validateYupSchema only sets the abortEarly and context options. I have a case where I need to mutate my form schema only on cast and not on every validation, so being able to set the strict option to yup validation would be useful.

Could it be possible to expose the validation options so that I can specify the validation needed? This could be as props to the formik component or with whatever method is easier.

Here is the yup docs that explain the validation options: https://github.com/jquense/yup#mixedvalidatevalue-any-options-object-promiseany-validationerror

And this is the code that would need to pass in the exposed options:

/**
 * Validate a yup schema.
 */
export function validateYupSchema<T extends FormikValues>(
  values: T,
  schema: any,
  sync: boolean = false,
  context: any = {}
): Promise<Partial<T>> {
  let validateData: FormikValues = {};
  for (let k in values) {
    if (values.hasOwnProperty(k)) {
      const key = String(k);
      validateData[key] = values[key] !== '' ? values[key] : undefined;
    }
  }
  return schema[sync ? 'validateSync' : 'validate'](validateData, {
    abortEarly: false,
    context: context,
  });
}
@stale stale bot added the stale label Oct 1, 2019
@maddhruv
Copy link
Collaborator

Not sure if exposing all the yup Options be right here, as more options can make things go complex around the core APIs.

For now, I would recommend you to use custom validation/form validation, more info here - https://formik.org/docs/guides/validation

where you can easily write custom yup validations and also pass the corresponding strict checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants