question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Property 'toJSON' is missing in type 'ZodObject<...>'

See original GitHub issue

With 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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
albehrenscommented, Aug 6, 2021

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:

Type instantiation is excessively deep and possibly infinite

One project uses 3.6.1 and the other one 3.7.1. After updating 3.6.1 to 3.7.1 there were finally no errors anymore.

0reactions
arahansencommented, Dec 5, 2022

That seems likely to me too. The toJSON method was removed after v2.

@colinhacks what was the reason for removing this API? I have a use case it would be perfect for

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found