zod-prisma-types icon indicating copy to clipboard operation
zod-prisma-types copied to clipboard

[BUG] type error in generated InputJsonValueSchema

Open jason-curtis opened this issue 1 year ago • 5 comments

Describe the bug a type error in generated InputJsonValueSchema

Screenshots Running tsc:

db/generated/zod/index.ts:39:14 - error TS2322: Type 'ZodLazy<ZodUnion<[ZodString, ZodNumber, ZodBoolean, ZodObject<{ toJSON: ZodFunction<ZodTuple<[], null>, ZodAny>; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodRecord<...>, ZodArray<...>]>>' is not assignable to type 'ZodType<InputJsonValue, ZodTypeDef, InputJsonValue>'.
  Types of property '_type' are incompatible.
    Type 'string | number | boolean | any[] | { toJSON?: (...args: unknown[]) => any; } | Record<string, any>' is not assignable to type 'InputJsonValue'.
      Type '{ toJSON?: (...args: unknown[]) => any; }' is not assignable to type 'InputJsonValue'.
        Type '{ toJSON?: (...args: unknown[]) => any; }' is not assignable to type '{ toJSON(): unknown; }'.
          Property 'toJSON' is optional in type '{ toJSON?: (...args: unknown[]) => any; }' but required in type '{ toJSON(): unknown; }'.

39 export const InputJsonValueSchema: z.ZodType<Prisma.InputJsonValue> = z.lazy(() =>
                ~~~~~~~~~~~~~~~~~~~~


Found 1 error in db/generated/zod/index.ts:3

Contents of the generated inputJsonValueSchema:

export const InputJsonValueSchema: z.ZodType<Prisma.InputJsonValue> = z.lazy(() =>
  z.union([
    z.string(),
    z.number(),
    z.boolean(),
    z.object({ toJSON: z.function(z.tuple([]), z.any()) }),
    z.record(z.lazy(() => z.union([InputJsonValueSchema, z.literal(null)]))),
    z.array(z.lazy(() => z.union([InputJsonValueSchema, z.literal(null)]))),
  ])
);

Package versions (please complete the following information):

  • zod:
> yarn why zod -R
└─ root-workspace-0b6124@workspace:.
  ├─ zod-prisma-types@npm:3.1.6 (via npm:^3.1.6)
  │  └─ zod@npm:3.23.5 (via npm:^3.22.4)
  └─ zod@npm:3.21.1 (via npm:3.21.1)
  • prisma:
> yarn why prisma
└─ @redwoodjs/cli@npm:7.0.6
  └─ prisma@npm:5.9.1 (via npm:5.9.1)

Additional context Zod generator in schema.prisma file:

generator zod {
  provider                         = "zod-prisma-types"
  createOptionalDefaultValuesTypes = true
  useDefaultValidators             = false
  writeNullishInModelTypes         = true // Accept "undefined" in nullable fields
  useTypeAssertions                = true // silence typescript issues :p
}

jason-curtis avatar May 01 '24 15:05 jason-curtis

I am facing this issue, are there any fixes yet?

imprakharshukla avatar Jul 17 '24 11:07 imprakharshukla

Me too~~~

pengshengjie avatar Aug 05 '24 07:08 pengshengjie

Same here.

aktkro avatar Sep 14 '24 08:09 aktkro

My temporary work around is transforming

export const InputJsonValueSchema: z.ZodType<Prisma.InputJsonValue> = z.lazy(() =>
  z.union([
    z.string(),
    z.number(),
    z.boolean(),
    z.object({ toJSON: z.function(z.tuple([]), z.any()) }),
    z.record(z.lazy(() => z.union([InputJsonValueSchema, z.literal(null)]))),
    z.array(z.lazy(() => z.union([InputJsonValueSchema, z.literal(null)]))),
  ])
);

into

export const InputJsonValueSchema: z.ZodType<Prisma.InputJsonValue> = z.lazy(() =>
  z.union([
    z.string(),
    z.number(),
    z.boolean(),
    z.record(z.lazy(() => InputJsonValueSchema)),
    z.array(z.lazy(() => InputJsonValueSchema)),
  ])
);

It would be nice to have a fix though 🙏🏻. @chrishoermann I just discovered this library and am really loving having it—thanks for your work!

mwalkerwells avatar Oct 04 '24 06:10 mwalkerwells

You must use the strict mode or enable strictNullChecks in your tsconfig.json. It's worked to me.

smilecc avatar Nov 21 '24 07:11 smilecc