Open cbix opened 1 year ago
Hi, any update ?
Btw, this issue is not relevant to me anymore since we switched from openapi-typescript + ts-to-zod to openapi-zod-client.
We're still not supporting zod v3.21.4+?
I have faced the same issue.
I have been looking into this issue, and while I haven't found a complete solution yet, I hope I may have uncovered something that could be helpful (at least for beginners like me).
The property shape
is defined in the class ZodObject
(types.ts), not in ZodType
or ZodSchema
.
In the case of the above example, since the input interface test
is recursive, the output schema is wrapped by the function z.lazy
, which returns ZodLazy
.
ZodLazy
is not assignable to ZodObject
nor has a field shape
.
So, to resolve the issue, we have to avoid the (ab)use of z.lazy
.
Here are two possible solutions. Although I have no idea how to achieve these automatically, but it is easy to apply these manually to your code.
By editing the output as follows, the zod schema works fine.
export const testSchema: z.ZodObject<any, any, any, test> = // z.lazy(() =>
z.object({
schemas: z.object({
A: z.string().nullable(),
B: z.array(z.lazy(() => testSchema.shape.schemas.shape.A)), // z.lazy is moved to this line
}),
})/*,
)*/;
Applying z.lazy
to a small part (instead of wrapping the whole object), we can replace ZodType
with ZodObject
and safely access to the field shape
.
z.lazy
is written by the function transformRecursiveSchema
(transformRecursiveSchema.ts), which is called from the function generate
in generate.ts. But it seems to be difficult (at least for me) to edit these functions so that the above code is emitted.
Edit the input as follows.
type AType = string | null;
type BType = AType[];
export interface test {
schemas: {
A: AType;
B: BType;
};
}
Then ts-to-zod
emits the following zod schema, which works fine.
const aTypeSchema = z.string().nullable();
const bTypeSchema = z.array(aTypeSchema);
export const testSchema = z.object({
schemas: z.object({
A: aTypeSchema,
B: bTypeSchema,
}),
});
In order to use this solution in the specific situation of cbix, we need to add such feature to openapi-typescript
(and I have no idea for that).
Bug description
ts-to-zod v3.0.0 generates a Zod schema that fails type verification with this error:
Looking into the
ZodType
(exported asZodSchema
) definition it in fact doesn't have any field calledshape
.Input
The input file is generated by
openapi-typescript
v6.2.0 but I could reproduce the issue with a type definition as simple as this:Expected output
Not exactly sure ...
Actual output
Versions
v4.9.5
v3.21.4