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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ export function Formik<
ExtraProps = {}
>(props: FormikConfig<Values> & ExtraProps) {
const formikbag = useFormik<Values>(props);
const { component, children, render, innerRef } = props;
const { component, children, render, innerRef, validationSchema } = props;

// This allows folks to pass a ref to <Formik />
React.useImperativeHandle(innerRef, () => formikbag);
Expand All @@ -1020,7 +1020,12 @@ export function Formik<
// eslint-disable-next-line
}, []);
return (
<FormikProvider value={formikbag}>
<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

}}
>
{component
? React.createElement(component as any, formikbag)
: render
Expand Down
28 changes: 28 additions & 0 deletions packages/formik/test/FormikContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { render } from 'react-testing-library';
import { Formik } from '../src/Formik';
import { useFormikContext } from '../src/FormikContext';

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()) }.


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?

return null;
};

render(
<Formik
initialValues={{ test: '' }}
validationSchema={validationSchema}
onSubmit={() => {}}
>
<AComponenent />
</Formik>
);
});
});
});