Skip to content

Commit

Permalink
Properly handle nested array data in prepareDataForValidation. (#2265)
Browse files Browse the repository at this point in the history
Previously, we would always return an object even if `values` was an
array. Now, we test to see if `values` is an array and set `data`
accordingly.

Fixes #2213.
  • Loading branch information
jamesremuscat authored Feb 6, 2020
1 parent 2330905 commit f117c04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ export function validateYupSchema<T extends FormikValues>(
export function prepareDataForValidation<T extends FormikValues>(
values: T
): FormikValues {
let data: FormikValues = {};
let data: FormikValues = Array.isArray(values) ? [] : {};
for (let k in values) {
if (Object.prototype.hasOwnProperty.call(values, k)) {
const key = String(k);
Expand Down
12 changes: 12 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,18 @@ describe('<Formik>', () => {
date,
});
});

it('should work correctly for nested arrays', () => {
const expected = {
content: [
['a1', 'a2'],
['b1', 'b2'],
]
}

const dataForValidation = prepareDataForValidation(expected);
expect(dataForValidation).toEqual(expected);
});
});

// describe('componentDidUpdate', () => {
Expand Down

1 comment on commit f117c04

@vercel
Copy link

@vercel vercel bot commented on f117c04 Feb 6, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.