Skip to content

Zod type mismatch #233

Description

@muezz

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions