We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Using yup.array() for simple arrays [1, ''] or ['a', ''] is not returning the error messages.
yup.array()
[1, '']
['a', '']
The $state.isValid is working as expected, which is one of my workarounds, but the $errors is showing empty string for any validation error.
$state.isValid
$errors
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)); } });
https://stackblitz.com/edit/vitejs-vite-tflb23?file=src%2FApp.svelte,package.json&terminal=dev
When trying to display the error message, the value is an empty string:
{JSON.stringify($errors, null, 2)}
will display:
{ "scores": [ "", "" ] }
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
N/A
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.
The text was updated successfully, but these errors were encountered:
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
Sorry, something went wrong.
No branches or pull requests
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:
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:
will display:
What is the expected correct behavior?
It should display the message error for the second item of the array:
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
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:
To:
Eg:
Or, you can rely on the
$state.isValid
which still shows as false for simple arrays.The text was updated successfully, but these errors were encountered: