Yup Validation For AutoComplete in Material UI

Viewed 205

I have an autocomplete that should be required if either of the two fields is true. My problem is that when I click submit, it outputs An unhandled error was caught during validation in <Formik validationSchema /> TypeError: You cannot concat() schema's of different types: string and object

  category: string().when(['new', 'exist'], {
    is: false,
    then: object({
      id: string(),
      name: string(),
    })
      .nullable()
      .required('field is required'),
  }),
  new: bool().nullable(),
  exist: bool().nullable(),
1 Answers

If I’m reading the error message correctly, then it seems that the string() you have immediately after category: should be replaced with object() instead, i.e.:

  category: object().when(['new', 'exist'], {
    is: false,
    then: object({
      id: string(),
      name: string(),
    })
      .nullable()
      .required('field is required'),
  }),
  new: bool().nullable(),
  exist: bool().nullable(),
Related