Zod type mismatch
Disclaimer: I am not a 100% sure if this is a `zsa` bug or my mistake.
I have the following `zod` schema:
```typescript
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:
```typescript
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:
```typescript
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:
<img width="740" alt="Image" src="https://github.com/user-attachments/assets/a14135ca-7663-48fe-9049-e3df0e9a8f59" />
This error goes away as soon as I remove the `transform` from the `order` property.
0 条评论