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

Run validationSchema for validateField calls #1784

Merged
merged 7 commits into from
Jan 13, 2020
Merged

Run validationSchema for validateField calls #1784

merged 7 commits into from
Jan 13, 2020

Conversation

jamesmosier
Copy link
Contributor

Previously validateField would only work with a validate={Function} property on a field. Now you can perform validateField('myFieldName') and it will look at the provided validationSchema to determine any errors.

Fixes issue #1755

@michielmetcake
Copy link

I would love to see this merged 🙌🏻

Copy link
Collaborator

@johnrom johnrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts on design patterns.

src/Formik.tsx Outdated
@@ -423,7 +423,19 @@ export function useFormik<Values extends FormikValues = FormikValues>({
return Promise.resolve(maybePromise as string | undefined);
}
} else {
Copy link
Collaborator

@johnrom johnrom Sep 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be more readable to continue with positive checks and to return the resolved promise last.

if (isFunction(props.validate)) {
  // ...
} else if (props.validationSchema) { /* validation code*/ } 

return Promise.resolve();

Also, if in combination with my next review comment about validating a single field, we can have their callbacks match, we can combine these into a single if (maybePromise) { /* validate */ }

src/Formik.tsx Outdated Show resolved Hide resolved
@jamesmosier
Copy link
Contributor Author

Thanks for the comments @johnrom - I'll try to get to these this weekend or next week!

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 21, 2019

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9417de9:

Sandbox Source
Formik TypeScript Playground Configuration

@stale stale bot added the stale label Nov 20, 2019
@jaredpalmer
Copy link
Owner

@johnrom are you good with this?

@stale stale bot removed the stale label Nov 26, 2019
@JFGagnon
Copy link

@johnrom @jaredpalmer when can we expect this PR to be merged?

@johnrom
Copy link
Collaborator

johnrom commented Jan 3, 2020

In terms of validating a single field, I realized that a change might actually invalidate a different field, so my review can be disregarded. I don't use validation schema in my projects, so if you guys are good with it and have tested, feel free to move forward without my signoff!

I might recommend adding a test though, to prevent future regressions.

@jamesmosier
Copy link
Contributor Author

@johnrom I can fix the conflicts, add some tests, and also make some of the changes you suggested (such as “I think it’d be more readable to continue with positive checks and to return the resolved promise last...“. which is still relevant).

Thanks for following up!

@jaredpalmer
Copy link
Owner

Okay, update and I will merge

@vercel
Copy link

vercel bot commented Jan 10, 2020

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/jared/formik-docs/lntbbjn1r
✅ Preview: https://formik-docs-git-fork-jamesmosier-master.jared.now.sh

@jamesmosier
Copy link
Contributor Author

@jaredpalmer updated it with some tests & rearranged some logic

@jaredpalmer
Copy link
Owner

Cool. LGTM

@jaredpalmer jaredpalmer merged commit 7be340e into jaredpalmer:master Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants