0
votes

I not sure am I getting this error , anyone know how to solve this? Below is my code the error occur on extra: Yup.array().of(Yup.lazy((value)....

const claimFormValidationSchema = Yup.object().shape<ClaimApplicationForm>({
  id: Yup.number().notRequired(),
  claimType: Yup.mixed().required(),
  claimAmount: Yup.number().required('Claim Amount is Required'),
  currencyCode: Yup.string().required(),
  rate: Yup.number().required('Rate is Required'),
  convertedAmount: Yup.number().required('Converted Amount is Required'),
  extra: Yup.array().of(
    Yup.lazy((value) => {
      if ('required' in value) {
        if (value.required === true) {
          return Yup.object().shape({
            value: Yup.string().required(),
          });
        } else {
          return Yup.object().shape({
            value: Yup.string().notRequired(),
          });
        }
      }
    }),
  ),
});

Error:

Argument of type '(value: unknown) => ObjectSchema> | undefined' is not assignable to parameter of type '(value: unknown) => Schema'. Type 'ObjectSchema> | undefined' is not assignable to type 'Schema'. Type 'undefined' is not assignable to type 'Schema'.

2

2 Answers

1
votes

Your problem is in the extra property

  extra: Yup.array().of(
    Yup.lazy((value) => {
      // outter if
      if ('required' in value) {
        if (value.required === true) {
          return Yup.object().shape({
            value: Yup.string().required(),
          });
        } else {
          return Yup.object().shape({
            value: Yup.string().notRequired(),
          });
        }
      }          
      // end outter if

      // if reaches here, nothing is returned
    }),
  ),

You need to return something inside Yup.lazy, but if this condition isn't true 'required' in value, you won't return anything, giving you undefined and that error.

I'm not sure what you need to return, but it must be something.

0
votes

After some time , i got what i expect to do.

extra: Yup.array().of(
  Yup.lazy((data: any) => {
    if ('required' in data) {
      if (data.required === true) {
        return Yup.object().shape({
          value: Yup.string().required(),
        });
      } else {
        return Yup.object().shape({
          value: Yup.string().notRequired(),
        });
      }
    }
  }),
),

I solve this problem by setting the schema for 'extra' like this.