0

I need to validate an array of numbers when a boolean is checked and i'm getting this error in console:

Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
    at yupToFormErrors (formik.esm.js:701)

and this is my yup validationSchema.

export default yup.object<myFormikForm.FormValues>({
  allowOther: yup.boolean(),
  arrayNumbers: yup
    .array<number, *>(
      yup
        .number()
        .nullable(true)
        .transform((value, originalValue) =>
          originalValue === '' ? null : value,
        )
        .min(
          minSize,
          oneLine`
          Must be greater or equal than
          0
        `,
        ),
    )

    .when('allowOther', (allowOther: boolean, schema) =>
      allowOther
        ? schema
        : schema

            .required('Required')
            .test(
              'hasAtLeastOneValue',
              'Specify at least one value',
              value =>
                Array.isArray(value) ? value.some(Number.isFinite) : true,
            ),
    ),
});

by the way the form is working properly but i wanna to get rid of this error, any thoughts? Thanks.

1 Answers1

0

You can use yup.ref() in your case. Learn more about it here.

 allowOther: Yup.boolean(),
 arrayNumbers: Yup.array().nullable().min(Yup.ref('allowOther'), 'Must be greater or equal than 0').required(Yup.ref('allowOther'), "arrayNumbers are required"),
Nidhi Dadiya
  • 798
  • 12
  • 31