Skip to content

new Method .schemaResolve() (or something like this) for nested schemas at runtime #4292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lghartmann opened this issue Apr 28, 2025 · 0 comments

Comments

@lghartmann
Copy link

This week i got a task that involves a lot of API testing with different versions of the same JSON body, i thought of a VSCODE extension that generates JSON body from a zod schema, didnt find any, then i started coding my own extension, my extension repo. I got it working with simple "primitive" schemas such as z.object({id: z.number(), name: z.string()}) but when it comes to nested schemas i started getting in trouble because of the many ways we can import stuff in TS. I tried a lot of deepseek generated code to make it work, if it is a skill issue please let me know. I'm planning on contributing to zod with this method if noone is doing it already, if i was not clear enough please let me know. Here's the idea:

import { z } from 'zod';

export const BirthdaySearchSchema = z.object({
  idSync: z.string(),
  componentType: z.literal('birthday'),
  farmIdSync: z.string(),
  name: z.string(),
  email: z.string().nullable(),
  phone: z.string().nullable(),
  birthDate: z.coerce.date(),
});

export type BirthdaySearchDto = z.infer<typeof BirthdaySearchSchema>;

my current version of the extension works with this schema (there's no nested schema)

but when i need to generate a JSON body from a schema that contains a nested schema i would need the method .resolveSchema() (or something like this) to get it as a another z.object...

import { z } from 'zod';

import { UserPartialSchema } from '../../User';

export const BirthdaySchema = z.object({
  idSync: z.string(),
  farmIdSync: z.string(),
  componentType: z.literal('birthday'),
  name: z.string(),
  email: z.string().nullable(),
  phone: z.string().nullable(),
  birthDate: z.string().datetime(),
  createdBy: UserPartialSchema,
});

export type BirthdayDto = z.infer<typeof BirthdaySchema>;

in this schema i would call the method to resolve it's shape at runtime so i could mock it on the extension side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant