Skip to content

How to use setValues with nested objects #3898

Answered by logaretm
kevinG73 asked this question in Q&A
Discussion options

You must be logged in to vote

That's because the schema isn't reactive. It doesn't contain the new key you added which is one of the downsides to represent your object like that.

You can use yup.lazy to create a lazy schema so it evaluates the keys each time:

yup.lazy(() =>
  yup.object({
    area: yup.number().required().positive().min(1000),
    translations: yup.object({
      ...selectedLanguages.value.reduce((acc, curr) => {
        acc[curr] = yup.object({
          id: yup.string().notRequired(),
          name: yup.string().required().max(30).min(2),
          locale: yup.string().required().max(2).min(2),
        });

        return acc;
      }, {}),
    }),
  })
);

https://stackblitz.com/edit/vitejs-vite-ch…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kevinG73
Comment options

Answer selected by kevinG73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants