kliuyyyy
kliuyyyy

Reputation: 29

Yup Formik Validation Schema with Reduce Function Not working

I have the following schema I'm generating using a reduce method to build an object including my desired nested keys/values.

  const validationSchema = Yup.object().shape({
    units: Yup.object().shape(
      supplyUnits.reduce((acc, unit) => {
        acc[unit.upc] = Yup.object().shape({
          assignNewInventory: Yup.boolean(),
          inventoryValue: Yup.number()
            .min(0, "Value must be positive or zero")
            .required(`Inventory value is required for UPC ${unit.upc}`),
        });
        return acc;
      }, {})
    ),
  });

This object stores the fields on each UPC.

The problem is that my errors object from Formik is not being populated correctly. When there's an error, the errors object just looks like

{
    units: []
}

I'm not sure what I'm doing wrong but it seems to be an issue with the validation schema specifically as the values and touched objects are populating correctly. Please advise

Upvotes: 0

Views: 16

Answers (0)

Related Questions