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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepareDataForValidation turns a nested array into an object #2020

Open
agmcleod opened this issue Nov 12, 2019 · 0 comments
Open

prepareDataForValidation turns a nested array into an object #2020

agmcleod opened this issue Nov 12, 2019 · 0 comments
Assignees
Labels

Comments

@agmcleod
Copy link

馃悰 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)
@jaredpalmer jaredpalmer self-assigned this Jan 7, 2020
@stale stale bot added the stale label Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants