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

$errors is not returning error messages for simple arrays (yup.array(yup.string().require()) #185

Open
leocaseiro opened this issue Feb 27, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@leocaseiro
Copy link

leocaseiro commented Feb 27, 2023

Summary

Using yup.array() for simple arrays [1, ''] or ['a', ''] is not returning the error messages.

The $state.isValid is working as expected, which is one of my workarounds, but the $errors is showing empty string for any validation error.

Steps to reproduce

Create a schema that has an array:
Eg:

const { form, errors, state, handleChange, handleSubmit } = createForm({
  initialValues: {
    scores: [1,'',3]
  },
  validationSchema: yup.object().shape({
    scores: yup
      .array(yup.number())
      .required(),
  }),
  onSubmit: values => {
    alert(JSON.stringify(values));
  }
});

Example Project

https://stackblitz.com/edit/vitejs-vite-tflb23?file=src%2FApp.svelte,package.json&terminal=dev

What is the current bug behavior?

When trying to display the error message, the value is an empty string:

{JSON.stringify($errors, null, 2)}

will display:

{ "scores": [ "", "" ] }

What is the expected correct behavior?

It should display the message error for the second item of the array:

  { "scores[1]": "scores[1] must be a `number` type, but the final value was: `NaN` (cast from the value `\"\"`). }"

PS: This message can be seen in a vanilla JS with the same schema using yup: https://codesandbox.io/s/yup-with-vanilla-javascript-forked-qykywv?file=/src/index.js

Relevant logs and/or screenshots

N/A

Possible fixes

UPDATE: I'm almost sure that this PR introduces that issue, so this might be the exactly place to fix it: #80

I haven't looked into the source code yet, but for now there is a workaround:

A workaround is to use object instead of a simple array:
https://stackblitz.com/edit/vitejs-vite-1dnkbs?file=src%2FApp.svelte,package.json&terminal=dev

From:

{ scores: [1, ''] }

To:

{ scores: [{val: 1}, {val: ''}] }

Eg:

createForm({
      initialValues: {
        scores: [{val: 1}, {val: ''}]
      },
      validationSchema: yup.object().shape({
        scores: yup
          .array()
          .of(yup.object().shape({val: yup.number().required()})),
      }),
      onSubmit: values => {
        alert(JSON.stringify(values));
      }
    });

Or, you can rely on the $state.isValid which still shows as false for simple arrays.

@leocaseiro leocaseiro added the bug Something isn't working label Feb 27, 2023
@leocaseiro
Copy link
Author

I found that there used to have a test for primitive arrays, but this test is no longer there. Maybe we should add it and try to fix it.

d21d08b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant