ZodObject not inferred when using ZodSchema #973
Replies: 6 comments
-
You can use |
Beta Was this translation helpful? Give feedback.
-
Hey @fnky! Thanks for the reply 😁 Just tried this again, and unfortunately this doesn't appear to be the case: export const ActiveServiceModel: ZodObject<ActiveService> = z
.object({
service_id: z.string().max(100),
team_id: z.string().uuid()
}) reports an error:
The signature for ZodObject<T,UnknownKeys,Catchall,Output,Input> extends ZodType If I substitute a generic |
Beta Was this translation helpful? Give feedback.
-
Yeah, there is not an easy way to extend a You will find that requiring a schema to conform to an interface will add friction like this in a lot of places, so I would caution against doing it unless there is a really good reason that is worth the trade offs. Zod has been designed to be the source of truth for the types, and while there are definitely valid cases for wanting to use something like If it's possible to let your schema's be the source of truth, that will resolve your issue. If not, I would suggest perhaps trying to use a shared "shape" rather than using interface ActiveService {
service_id: string;
team_id: string;
}
const shape = {
service_id: z.string(),
team_id: z.string().uuid()
};
const ActiveServiceModel: z.ZodSchema<ActiveService> = z.object(shape);
const ExtendedActiveServiceModel = z.object({
...shape,
bar: z.string()
}); —At the very least, you'll start getting errors if the shape and interface get out of sync. But extending this beyond the module boundary is pretty messy and unergonomic. |
Beta Was this translation helpful? Give feedback.
-
For reference the use case is ensuring the Zod schemas match the generated types from a Postgres database. Ultimately all that needs to occur is that build or CI should fail if the types are out of sync. @scotttrinh I think I can possibly see a solution based on your example above (given that a build-time failure is all I technically require), thanks for that - I'll check and report results. |
Beta Was this translation helpful? Give feedback.
-
Hi, I have a similar issue and needed to type import { z } from 'zod'
type Properties<Input> = Required<{
[K in keyof Input]: z.ZodType<Input[K], any, Input[K]>;
}>
type Range = {
min: number;
max: number;
}
const schema = z.object<Properties<Range>>({
min: z.number(),
max: z.number()
}); I've found that TS (I'm using v4.5.4) will error if a property is missing or mis-spelt or has an incorrect primitive type and so provides a certain level of safety. |
Beta Was this translation helpful? Give feedback.
-
@JRGranell type Properties<Input> = Required<{
[K in keyof Input]: z.ZodType<Input[K], any, Input[K]>;
}> thanks for that 🤙🏻 |
Beta Was this translation helpful? Give feedback.
-
I'm trying to extend a
ZodObject
schema which is bound to an existing interface withZodSchema
, however the schema is detected asZodType
which does not have theextend()
method.Is there some way to specify that the
ZodSchema
is specifically aZodObject
?Error Detail
Beta Was this translation helpful? Give feedback.
All reactions