Property 'toJSON' is missing in type 'ZodObject<...>'
See original GitHub issueWith the help of this comment (https://github.com/colinhacks/zod/issues/392#issuecomment-826227242) I wrote a little helper function that looks like this:
export default function validateSchema<T extends z.ZodTypeAny>(
schema: T,
payload: unknown
): z.infer<typeof schema> {
const validation = schema.safeParse(payload);
if (validation.success) {
return validation.data;
} else {
console.log(error);
throw error;
}
}
After months I updated my zod version and now TypeScript complains with the following error whenever I want to invoke the function, even though it used to work:
Argument of type 'ZodObject<{ createdAt: ZodString; lsi1: ZodOptional<ZodString>; pk: ZodString; sk: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to parameter of type 'ZodTypeAny'.
Property 'toJSON' is missing in type 'ZodObject<{ createdAt: ZodString; lsi1: ZodOptional<ZodString>; pk: ZodString; sk: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' but required in type 'ZodType<any, any>'.ts(2345)
base.d.ts(72, 14): 'toJSON' is declared here.
This is how I call validateSchema
:
const schema = z.object({
createdAt: z.string(),
lsi1: z.string().optional(),
pk: z.string(),
sk: z.string(),
});
validateSchema(schema, { some: "payload" });
How can I update validateSchema
to work as intended?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
colinhacks/zod: TypeScript-first schema validation ... - GitHub
TypeScript-first schema validation with static type inference - GitHub ... zod-to-json-schema : Convert your Zod schemas into JSON Schemas.
Read more >Typescript: "property getName is missing from type" when ...
The first workaround I can think of is to create a second Class to wrap around the first Class, use one class to...
Read more >TypeScript-First Schema Validation with Static Type Inference
To get autocompletion with a Zod enum, use the .enum property of your schema: ... TypeScript's built-in Pick and Omit utility types, all...
Read more >Designing the perfect Typescript schema validation library
tl;dr: I made a new Typescript validation library that has static type inference and the best DX this side of the Mississippi.
Read more >Untitled
I'm using the term "schema" to broadly refer to any data type/structure, ... all Zod object schemas have a `.partial` method that makes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Wow, thanks @scotttrinh, that was it. I am using yarn workspaces and I had one project with zod 1.x and one with zod 3.x. After updating my outdated project I had another issue:
One project uses
3.6.1
and the other one3.7.1
. After updating3.6.1
to3.7.1
there were finally no errors anymore.@colinhacks what was the reason for removing this API? I have a use case it would be perfect for