Repository: jaredpalmer/formik (ts)

Issue:
Is there a way to pass props to validationSchema => for dynamic validation
Something like:

```
  validationSchema: props => {
    const schema = {};

    props.answers.forEach(answer => {
      schema[answer.id] = Yup.string().required('Required');
    });

    return Yup.object().shape(schema);
  },
```

Pull request:
#96. Make validationSchema accept a fn of props
Closes #96 

Changes `validationSchema?: any` -> `validationSchema? (props: Props) => any | any`

Patch:
diff --git a/src/formik.tsx b/src/formik.tsx
index 5be202c..bcb5e90 100644
--- a/src/formik.tsx
+++ b/src/formik.tsx
@@ -51,7 +51,7 @@ export interface FormikConfig<
     props: Props
   ) => void | FormikErrors<Values> | Promise<any>;
   /** A Yup Schema */
-  validationSchema?: any;
+  validationSchema?: ((props: Props) => any) | any;
   /** Submission handler */
   handleSubmit: (
     values: Values | DeprecatedPayload,
@@ -262,7 +262,10 @@ export function Formik<Props, Values extends FormikValues, Payload = Values>({
        * Run validation against a Yup schema and optionally run a function if successful
        */
       runValidationSchema = (values: Values, onSuccess?: Function) => {
-        validateYupSchema<Values>(values, validationSchema).then(
+        const schema = isFunction(validationSchema)
+          ? validationSchema(this.props)
+          : validationSchema;
+        validateYupSchema<Values>(values, schema).then(
           () => {
             this.setState({ errors: {} });
             if (onSuccess) {


Test patch:
diff --git a/test/formik.test.tsx b/test/formik.test.tsx
index 014f29e..f4f0ed9 100644
--- a/test/formik.test.tsx
+++ b/test/formik.test.tsx
@@ -375,6 +375,32 @@ describe('Formik', () => {
           expect(handleSubmit).not.toHaveBeenCalled();
         });
       });
+
+      describe('with validationSchema (ASYNC)', () => {
+        it('should run validationSchema if present', () => {
+          const validate = jest.fn(() => Promise.resolve({}));
+          const ValidateForm = FormFactory({ validationSchema: { validate } });
+          const tree = shallow(<ValidateForm user={{ name: 'jared' }} />);
+          tree.find(Form).dive().find('form').simulate('submit', {
+            preventDefault: noop,
+          });
+          expect(validate).toHaveBeenCalled();
+        });
+
+        it('should call validationSchema if it is a function and present', () => {
+          const validate = jest.fn(() => Promise.resolve({}));
+          const ValidateForm = FormFactory({
+            validationSchema: () => ({
+              validate,
+            }),
+          });
+          const tree = shallow(<ValidateForm user={{ name: 'jared' }} />);
+          tree.find(Form).dive().find('form').simulate('submit', {
+            preventDefault: noop,
+          });
+          expect(validate).toHaveBeenCalled();
+        });
+      });
     });
   });