sveltekit-superforms
sveltekit-superforms copied to clipboard
Array validation errors are not added when it is nested inside `z.record`
- [x] Before posting an issue, read the FAQ and search the previous issues.
Description
I would like to add a validation rule to ensure at least one checkbox is checked for each record.
I'm currently using z.record(z.string(), z.array(z.string()).min(1)) for the schema.
However, I found that validation indeed happened but the error was not added when I reassign the array with the checked options,
although it does work if the schema is with a fixed key (checkboxes: z.array(z.string()).min(1)).
After looking into it a bit, I found that the diff logic suggests that the first element has changed (checkbox.key1.0) but it does not match with the path having errors (checkbox.key1) so the error is not added.
I have made a MRE with the link below. Can you please look into it? I appreciate your help.
If applicable, a MRE Use this template project to create a minimal reproducible example that you can link to here: https://sveltelab.dev/github.com/rayrw/superforms-checkbox-validation (right click to open in a new tab)
To clarify, the desired behavior is for the validation error to appear immediately when the checkbox is checked and then unchecked.
This is an edge case, so for now, I'll work around it by manually triggering validation after the value updates or by using the onblur event.
It would still be nice if the type incompatibility could be solved, though.
<input
type="checkbox"
value={option}
onchange={(e) => {
if (e.currentTarget.checked) {
$form.checkboxes[key] = [...$form.checkboxes[key], e.currentTarget.value];
} else {
$form.checkboxes[key] = $form.checkboxes[key].filter(
(checkedOption) => checkedOption !== e.currentTarget.value
);
}
+ validate(`checkboxes.${key}` as FormPathLeaves<z.infer<typeof schema>>);
}}
/>