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

Why isn't it possible to access some of the object methods? #41

Closed
pedropmedina opened this issue Dec 23, 2022 · 2 comments
Closed

Why isn't it possible to access some of the object methods? #41

pedropmedina opened this issue Dec 23, 2022 · 2 comments

Comments

@pedropmedina
Copy link

Hi,

I'm trying to .pick and .extend a generated zod schema, but it seems as if those methods aren't available. Is there a reason for this or am I doing something wrong?

Thanks

@chrishoermann
Copy link
Owner

Hey @pedropmedina,

this is actually a limitation of recursive schemas in zod.

To be able to write recursive schemas we need to provide an actual typescript type (from generated prisma types) via the z.ZodType<Type> utility type. I don't know why but this utility type does not have the full functionality of e.g. a z.object().

What you could do is use the generated model schemas to pick the fields you need. These schemas are just plain z.object() instances that provide the .pick, .omit and all the other methods.

for example:

model Test {
  /// @zod.string({ invalid_type_error: "some error with special chars: some + -*#'substring[]*#!§$%&/{}[]", required_error: "some other", description: "some description" }).cuid()
  id         String    @id @default(cuid())
  /// some comment @zod.string({ required_error: "error", invalid_type_error: "error"})
  name       String?
  value      MYValue
  /// @zod.custom.use(z.string().refine((val) => validator.isBIC(val), { message: 'BIC is not valid' }))
  bic        String?
  intTwo     Int
  int        Int?
  floatOpt   Float?
  float      Float
  decimal    Decimal
  decimalOpt Decimal?
  date       DateTime  @default(now())
  dateOpt    DateTime? /// @zod.date({ invalid_type_error: "wrong date type" })
  bigInt     BigInt /// @zod.bigint({ invalid_type_error: "error" })
  bigIntOpt  BigInt?
  json       Json
  jsonOpt    Json?
  bytes      Bytes /// @zod.custom.use(z.instanceof(Buffer).refine((val) => val ? true : false, { message: 'Value is not valid' }))
  bytesOpt   Bytes?
}

would generate the following schema you could pick fields from.

export const TestSchema = z.object({
  value: MYValueSchema,
  id: z
    .string({
      invalid_type_error:
        "some error with special chars: some + -*#'substring[]*#!§$%&/{}[]",
      required_error: 'some other',
      description: 'some description',
    })
    .cuid(),
  name: z
    .string({ required_error: 'error', invalid_type_error: 'error' })
    .nullish(),
  bic: z
    .string()
    .refine((val) => validator.isBIC(val), { message: 'BIC is not valid' })
    .nullish(),
  intTwo: z.number(),
  int: z.number().nullish(),
  floatOpt: z.number().nullish(),
  float: z.number(),
  decimal: z
    .number()
    .refine((v) => PrismaClient.Prisma.Decimal.isDecimal(v), {
      message: 'Field "decimal" must be a Decimal',
      path: ['Models', 'Test'],
    }),
  decimalOpt: z
    .number()
    .refine((v) => PrismaClient.Prisma.Decimal.isDecimal(v), {
      message: 'Field "decimalOpt" must be a Decimal',
      path: ['Models', 'Test'],
    })
    .nullish(),
  date: z.date(),
  dateOpt: z.date({ invalid_type_error: 'wrong date type' }).nullish(),
  bigInt: z.bigint({ invalid_type_error: 'error' }),
  bigIntOpt: z.bigint().nullish(),
  json: InputJsonValue,
  jsonOpt: NullableJsonValue.optional(),
  bytes: z
    .instanceof(Buffer)
    .refine((val) => (val ? true : false), { message: 'Value is not valid' }),
  bytesOpt: z.instanceof(Buffer).nullish(),
});


// Pick all the fields you need
const Picks = TestSchema.pick({
  id: true,
  name: true,
  int: true,
  float: true,
  decimal: true,
  date: true,
  bigInt: true,
  json: true,
  bytes: true,
});

Maybe this helps.

@pedropmedina
Copy link
Author

Thanks a lot @chrishoermann. That works. For some reason my LSP wasn't picking up those schema types.

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

2 participants