jaredpalmer / jaredpalmer/formik

prepareDataForValidation turns a nested array into an object

Open
#2,020 0 comments 4 reactions 1 assignee Claimed by @jaredpalmer View on GitHub
stale
Dominant language
TypeScript
Stars
34.3k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

## 🐛 Bug report

### Current Behavior

I have a fairly large complex data shape that goes into the form i'm working on, but the example piece is like this:

```
{
tableData: [
['1', '2']
]
}
```

Then when it runs through the validation function, it has the wrong shape by the time it reaches yup.

```
{
tableData: [
{ 0: '1', 1: '2' }
]
}
```

Can find the source for this here: https://github.com/jaredpalmer/formik/blob/master/src/Formik.tsx#L994. Since it's using `for in`, it's treated as an object regardless if it's an array or not.

### Expected behavior

I'd like it so the shape remains the same, and it can validate against my yup validation:

```
let scheme = yup.array().of(
yup
.array()
.of(yup.string().required('Is required'))
.required()
)
```

### Reproducible example

I tried running the code sandbox, but it seems to be giving me zero validation errors. https://codesandbox.io/s/formik-codesandbox-template-dtyw2

### Suggested solution(s)

Maybe not the cleanest approach, but i would probably just iterate the array differently from the object. Use Object.keys for an object, and if Array.isArray() returns true, use a for loop or .forEach instead.

### Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 2.0.3 |
| React | 16.9.0 |
| TypeScript | n/a |
| Browser | Firefox |
| npm/Yarn | 1.13.0 |
| Operating System | Mac OS 10.14.5 (mojave) |

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.