toZod package - Inferring schema from return type

Anyone have experience with the toZod package? I've used it to infer schemas from types, but now I'm trying to infer a schema from a return type (generated from trpc's inferRouterOutputs): Typically I use it like this:
const ValueUnitSchema: toZod<ValueUnitType> = z.object({
id: z.string(),
name: z.string(),
});
const ValueUnitSchema: toZod<ValueUnitType> = z.object({
id: z.string(),
name: z.string(),
});
Where if I just had this:
const ValueUnitSchema: toZod<ValueUnitType>
const ValueUnitSchema: toZod<ValueUnitType>
And if you hovered over ValueUnitSchema, you'd see all of the properties that you needed to define with all of the suggested schemas.
const valueUnitSchemaFull: z.ZodObject<{
id: z.ZodString;
name: z.ZodString;
}, "strip", z.ZodTypeAny, ValueUnit, ValueUnit>
const valueUnitSchemaFull: z.ZodObject<{
id: z.ZodString;
name: z.ZodString;
}, "strip", z.ZodTypeAny, ValueUnit, ValueUnit>
Now I'm trying to use it with an inferred router output type:
const TestSchema: toZod<ReturnType> = ?
const TestSchema: toZod<ReturnType> = ?
Upon hovering over TestSchema, I see this:
const TestSchema: z.ZodOptional<z.ZodArray<z.ZodObject<{
product: z.ZodNullable<z.ZodObject<{
id: z.ZodString;
name: z.ZodNullable<z.ZodString>;
const TestSchema: z.ZodOptional<z.ZodArray<z.ZodObject<{
product: z.ZodNullable<z.ZodObject<{
id: z.ZodString;
name: z.ZodNullable<z.ZodString>;
So I tried something like this:
const TestSchema: toZod<ReturnTypeTest> = z.optional(z.array(z.object({
(properties go here but they're not popping up as a suggestion)
})))
const TestSchema: toZod<ReturnTypeTest> = z.optional(z.array(z.object({
(properties go here but they're not popping up as a suggestion)
})))
Anyone have any clue on this?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?