Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zod type mismatch #233

Open
muezz opened this issue Jan 25, 2025 · 0 comments
Open

Zod type mismatch #233

muezz opened this issue Jan 25, 2025 · 0 comments

Comments

@muezz
Copy link

muezz commented Jan 25, 2025

Disclaimer: I am not a 100% sure if this is a zsa bug or my mistake.

I have the following zod schema:

export const TableParamsSchema = z
  .object({
    page: z.coerce.number().positive().default(1),
    size: z.coerce.number().positive().default(10),
    hide: z
      .union([z.string().transform((val) => [val]), z.array(z.string())])
      .default([]),
    selected: z
      .union([z.string().transform((val) => [val]), z.array(z.string())])
      .default([]),
    order: z
      .union([z.string().transform((val) => [val]), z.array(z.string())])
      .transform((val) =>
        val.map((v) => {
          const parts = v.split(".");
          return {
            column: parts[0],
            direction: parts[1],
            ascending: parts[1] === "asc",
          };
        })
      )
      .refine((val) => val.every((v) => ["asc", "desc"].includes(v.direction)))
      .default([]),
  })
  .transform((val) => {
    return {
      ...val,
      end: val.size * val.page,
      start: val.size * val.page - val.size,
    };
  });

It looks fairly complicated but at the end of the day, it just parses and validates the search params related to some table operations.

Here is a zsa server function:

export const getHostingTickets = createServerAction()
  .input(
    z.object({
      filter: z.string().optional(),
      completed: z.boolean().default(false),
      before_today: z.boolean().default(false),
      id_client: z.string().uuid().optional(),
      sites: z.array(z.string()).default([]),
      models: z.array(z.number()).default([]),
      tableParams: TableParamsSchema.optional(),
    })
  )
  .handler(async ({ input }) => {
    // I do what I need to do here. All inferred types in "input" are 100% correct.
  });

Here is how I call this server function:

const tableParams = TableParamsSchema.parse(searchParams);

  console.log(tableParams);

  const [data, err] = await getHostingTickets({
    filter: searchParams.filter,
    models: searchParams.miner_model,
    sites: searchParams.site,
    completed: completed,
    tableParams: tableParams,
  });
  if (err) throw err;

The console log shows correctly validated data in the correct form. But tableParams in the input is underlined and says this:
Image

This error goes away as soon as I remove the transform from the order property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant