0

Need help,i used zod to validate my data and try to used function useMemo when i cursor to the value as in image the value can read from zod validation but when i put the value the error come out as image. I tried to put index to the value also doesn't work. What should i do?

const apiScheme = z.tuple([
      z.object({
         total_work_hours: z.number().nullable(),
         normal_hours: z.number().nullable(),
         overtime: z.number(),
         public_holiday_hours: z.number(),
         total_work_days: z.number(),
         total_medical_leave: z.number(),
         total_emergency_leave: z.number(),
         total_public_holiday: z.number(),
      }),
      z.array(
         z.object({
            date: z.string(),
            attendance: z.array(
               z.object({
                  outlet_name: z.string().nullable(),
                  clock_in_type: z.string().nullable(),
                  clock_in: z.number().nullable(),
                  clock_out: z.number().nullable(),
                  work_hour: z.number().nullable(),
               }),
            
            ),
         }),
      ),
   ]);
   // console.log(apiScheme);

   const data1 = useMemo(() => {
      if (responseAttendanceData) {
         const parseData = apiScheme.parse(responseAttendanceData);
         if (parseData) {
        return parseData.map((value)=>{
         return{
            total_work_hours: value.total_work_hours,
            normal_hours: z.number().nullable(),
            overtime: z.number(),
            public_holiday_hours: z.number(),
            total_work_days: z.number(),
            total_medical_leave: z.number(),
            total_emergency_leave: z.number(),
            total_public_holiday: z.number(),
            
         }
        })
         }
      }
      return null;
   }, [responseAttendanceData]);
nur alliffah
  • 21
  • 1
  • 2
  • Do you think you could simplify your problem somewhat? I noticed for one thing that you're `map`ping what is likely the `tuple` response you got from a successful `parse`. That means you're treating it like an array so the first value will be the shape with `total_work_hours` but the second value is going to be the other shape with an array of date and attendance. It also looks like you're returning something that sort of looks like a zod schema except for `total_work_hours` which is a parsed value. It's hard to say where your issue is without clear expectations for input and output. – Souperman Jul 25 '22 at 12:55

1 Answers1

1

I just read your issues. UseMemo doesn't work with the async functions. So you can't use it with your data. If you use UseEffect and UseState to implement your idea, you can solve that problem. Thank you for reading my advice.