Skip to content

Instantly share code, notes, and snippets.

@jsmolina
Created June 6, 2023 16:31
Show Gist options
  • Save jsmolina/394625ee50fe660a0d076334647f8f3e to your computer and use it in GitHub Desktop.
Save jsmolina/394625ee50fe660a0d076334647f8f3e to your computer and use it in GitHub Desktop.
YUP: Validating a dynamic field object (profiles) that appears when another field (selected_groups) is checked.
profiles: Yup.lazy((value, { parent }) => {
if (value && parent.selected_groups?.length) {
const newEntries = parent.selected_groups.reduce(
(acc, val) => ({
...acc,
[val]: Yup.string().test("hasValue", "item cannot be empty", (value) => {
return !!value;
}),
}),
{}
);
return Yup.object().shape(newEntries);
}
return Yup.object().notRequired();
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment