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 validationSchema to FormikContext #2090

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ldicocco
Copy link
Contributor

@ldicocco ldicocco commented Dec 6, 2019

useFormikContext doesn't return validationSchema, although the type says it should do and the connectin v1 did.
This PR fixes the problem and adds a specific test for this case.

@vercel
Copy link

vercel bot commented Dec 6, 2019

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/1dgk3anhx
✅ Preview: https://formik-docs-git-fork-ldicocco-bugfix-useformikcontext-72fb91.jared.now.sh

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 6, 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 e5a3cc3:

Sandbox Source
Formik TypeScript Playground Configuration

@josejulio
Copy link

Also hitting the same issue, if anything is needed to help, please let me know.

@desfero
Copy link

desfero commented Dec 23, 2019

@ldicocco also it probably worth to add validate the same way you did with validationSchema because if you look at types

export declare type FormikContextType<Values> = FormikProps<Values> & Pick<FormikConfig<Values>, 'validate' | 'validationSchema'>;

you can see that both validate and validationSchema are picked.

@ldicocco @josejulio is there any workaround to get validationSchema?

@jaredpalmer is it possible to merge this one and release as otherwise formik is broken in case someone is using yup metadata to get the desired UI

@SamKirkland
Copy link

This was actually a breaking change in v2.
I'm updating from v1.5.8 which used to provide validationSchema in <FormikConsumer>

Example 1 - change the imported formik version to v2.1 or above and validationSchema will no longer be in context
Example 2

@egoreburial
Copy link

cmon please merge it! There is no chance to dynamically check if come field is required or not and render it to UI !!!

@Maxim-Chugaev
Copy link

please merge it !

@stale stale bot removed the stale label Apr 29, 2020
@Maxim-Chugaev
Copy link

@josejulio What about workaround ?

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 small changes requested.

Testing useFormikContext may need further thought, unless it is acceptable to test props.

<FormikProvider
value={{
...formikbag,
validationSchema,
Copy link
Collaborator

Choose a reason for hiding this comment

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

add this to formikBag itself


const AComponenent: React.FC = () => {
const formikContext = useFormikContext();
expect(formikContext.validationSchema).toBe(validationSchema);
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it proper to put tests inside of components? I've never tried it. The normal formik renderer in the rest of the tests exposes the props to the test runner via a special API, like this:

        const validate = jest.fn(() => Promise.resolve({}));
        const validationSchema = {
          validate,
        };
        const { getProps } = renderFormik({
          validationSchema,
        });

        expect(getProps().validationSchema).toBe(validationSchema);

I don't see any tests using useFormikContext though. Maybe this is something we need to add? Or is validationSchema being passed via Formikbag enough proof that it's working?

describe('FormikContext', () => {
describe('useFormikContext', () => {
it('should return validationContext if set', () => {
const validationSchema = 'validationSchema';
Copy link
Collaborator

Choose a reason for hiding this comment

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

is string valid for validation schema? thought it had to be a Yup object. Other tests use { validate: jest.fn(() => Promise.resolve()) }.

@josejulio
Copy link

@josejulio What about workaround ?

I used the validation schema that I knew was in place directly.

@IevaDrauge
Copy link

Can someone please merge/fix this? It is a very crucial missing feature right now...

@raphaelpc
Copy link

Hello! Any news on this? Thanks!

@sudo-vaibhav
Copy link

This is a very important feature, I hope it gets merged soon.

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.

10 participants