Skip to content

how to validate fields with nested objects in vue 3 with composition api #3882

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

You must be logged in to vote

You need to use yup.object to define the schema.

const {errors} = useForm({
  validationSchema: yup.object({
    area: yup.number().required().positive().min(1000),
    translations: yup.lazy(value => {
      if (value !== undefined) {
        return yup.object().shape({
          id: yup.string().notRequired(),
          name: yup.string().required().max(30).min(2),
          locale: yup.string().required().max(2).min(2),
        });
      }
      return yup.mixed().required();
    }),
  }),
  initialValues,
})

If the problem persists, please create an example on stackblitz.

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
9 replies
@logaretm
Comment options

@kevinG73
Comment options

@logaretm
Comment options

@logaretm
Comment options

@kevinG73
Comment options

Answer selected by kevinG73
Comment options

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

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