Skip to content
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

Resolves #88 - satisfies type instead of inferring #89

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

owenr88
Copy link

@owenr88 owenr88 commented Oct 27, 2023

Description

See #88 in reference to the issue. This implements the satisfies keyword for the type instead of inferring the zodType directly.

import { z } from 'zod';

import type { Prisma } from '@prisma/client';

const Schema = z
  .object({
    id: z.number().optional(),
    email: z.string().optional(),
  })
  .strict() satisfies z.ZodType<Prisma.UserWhereUniqueInput>;

export const UserWhereUniqueInputObjectSchema = Schema;

With the above, you can now

const ExtendedSchema = UserWhereUniqueInputObjectSchema.extend({
  example: z.string()
})
// And
const ExampleSchema = UserWhereUniqueInputObjectSchema.shape.example;

References

Fixes #88. And a similar discussion in the zod repo.

@owenr88 owenr88 marked this pull request as draft October 27, 2023 10:39
@owenr88
Copy link
Author

owenr88 commented Oct 27, 2023

I've done a little more investigating and it seems this has an unintended effect on schemas with z.lazy() where it makes the type an any. Argh! I've marked this PR as draft for now.

Screenshot 2023-10-27 at 11 38 25

Any suggestions how we could get around this? One solution is keeping the original, but also exposing the raw z.object schema:

const Schema = z
  .object({
    id: z.number().optional(),
    email: z.string().optional(),
  })
  .strict() satisfies z.ZodType<Prisma.UserWhereUniqueInput>;

export const _UserWhereUniqueInputObjectSchema = Schema;
export const UserWhereUniqueInputObjectSchema: z.ZodType<Prisma.UserWhereUniqueInput> = Schema;

Keen for thoughts on this!

@angelinashepherd
Copy link

hi omar, thank you so much for this extremely helpful library! Is it possible for you to make it compatible with Prisma 5 as well?

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

Successfully merging this pull request may close these issues.

Re-use Zod Schemas as a z.ZodObject not z.ZodType
2 participants