Skip to content

Feature Request: Surface the underlying object #21

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

Closed
joshribakoff-sm opened this issue Mar 7, 2022 · 5 comments · Fixed by #23
Closed

Feature Request: Surface the underlying object #21

joshribakoff-sm opened this issue Mar 7, 2022 · 5 comments · Fixed by #23
Labels
good first issue Good for newcomers

Comments

@joshribakoff-sm
Copy link

export function UpdateUserRequestSchema(): z.ZodSchema<UpdateUserRequest> {
  return z.object({
    email: z.string().nullish(),
    enabled: z.boolean().nullish(),
    first_name: z.string().nullish(),
    last_name: z.string().nullish(),
    role: RoleSchema.nullish(),
    team_id: z.number().nullish(),
  });
}

This cannot be extended, instead my idea is to have an option so it outputs this:

const updateUserRequestFields = {
    email: z.string().nullish(),
    enabled: z.boolean().nullish(),
    first_name: z.string().nullish(),
    last_name: z.string().nullish(),
    role: RoleSchema.nullish(),
    team_id: z.number().nullish(),
 }

export function UpdateUserRequestSchema(): z.ZodSchema<UpdateUserRequest> {
  return z.object(updateUserRequestFields);
}

I could then import updateUserRequestFields and extend it prior to passing it to z.object() in the consuming code

export function MyCustomSchema(): z.ZodSchema<UpdateUserRequest> {
  const fields = updateUserRequestFields
  fields.email.min(1) // extending the validations :)
  return z.object(fields);
}

It could also optionally append:

export function UpdateUserRequestSchema(): z.ZodSchema<UpdateUserRequest> {
  return z.object(updateUserRequestFields);
}
@github-actions github-actions bot added the good first issue Good for newcomers label Mar 7, 2022
@Code-Hex
Copy link
Owner

Code-Hex commented Mar 8, 2022

@joshribakoff-sm Thanks. I think you can do by extend method like UpdateUserRequestSchema().extend({ email: z.string().nullish().email() }) .

see: https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/tree/main/example/zod

@joshribakoff-sm
Copy link
Author

Interesting, thanks for the heads up! Why not surface this on your main readme? There is no way I would have found this on my own 😆

@Code-Hex
Copy link
Owner

Code-Hex commented Mar 8, 2022

@joshribakoff-sm I have written like

You can check example directory if you want to see more complex config example or how is generated some files.

...And I wrote some tips in there.

@joshribakoff-sm
Copy link
Author

joshribakoff-sm commented Mar 8, 2022

Bottom line is that it wasn't clear (to me). I would suggest to reword it better (such as grammar), and inline the example into the main readme.

Something like:

you may want to extend or modify the generated schemas. There are two options to do this. Client side with .extend(), and server side with directives.

Below this, there could be two headings labelled "extending schema server side with directives", "extending schema client side with .extend().

It also doesn't work. See the resulting type error I get when following the example:

Screen Shot 2022-03-08 at 9 36 44 AM

Looks like the underlying z.ZodObject is what has extend method, but the generated code annotates the return type to ZodSchema which has no extend method.

@Code-Hex
Copy link
Owner

Code-Hex commented Mar 9, 2022

@joshribakoff-sm thanks for reporting.
I fixed returned type to z.ZodObject at v0.4.0

About documentation, welcome PR.

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

Successfully merging a pull request may close this issue.

2 participants