Created
June 6, 2023 16:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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