jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.98k stars 2.79k forks source link

How to use 'when' with validateschema? #1665

Closed Enzyoh closed 4 years ago

Enzyoh commented 5 years ago

❓Question

In using Yup independently of Formik, I can pass options during the validation process. Something like

yupSchema.validate(sampleObjectToValidate, {context: {options})

This is very handy when you use 'when' (want to conditionally change your schema with different code paths). How do I pass this context when using formik's validationSchema.

maddhruv commented 4 years ago

You can find more info on using yup.when here - https://www.npmjs.com/package/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema And also the validationSchema accepts a yup schema directly, more info - https://formik.org/docs/api/withFormik#validationschema-schema--props-props--schema Feel free to re-open the issue, if you need more help around this.

codecadwallader commented 3 years ago

Recommend looking at work-around in #506 for anyone else coming to this issue (i.e. define validate function to mimic the validationSchema behavior).