Skip to content

zenstack/zod package generates incompatible types for JsonValue #2550

@iPommes

Description

@iPommes

Description and expected behavior
When using the zenstack zod generator it generates incompatible types for JSONFields. Usually it should just convert it to "z.any()" maybe? Then it would not be incompatible with JsonValue.

Screenshots
No screenshots.

Environment (please complete the following information):
With having the following "schema.zmodel"

type BasicFields {
    id        String   @id @default(uuid())

    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    metaData  Json     @default("{}")
}

model Test with BasicFields {
    name String
}

and using the @orpc/zod generator:

import { createSchemaFactory } from "@zenstackhq/zod";
export const zodFactory = createSchemaFactory(schema);

It generates an incompatible type for any Json fields:

Types of property 'metaData' are incompatible.
  Type 'JsonValue' is not assignable to type 'string | number | boolean | Record<string, unknown> | unknown[] | null'.
  Type 'JsonArray' is not assignable to type 'string | number | boolean | Record<string, unknown> | unknown[] | null'.
  The type 'readonly (JsonValue | null)[]' is 'readonly' and cannot be assigned to the mutable type 'unknown[]'.ts(2345)

Then trying parsing a result by the database to an zod schema like this:

const schema = zodFactory.makeModelCreateSchema("Test");
const result: z.infer<typeof schema> = await db.test.findFirstOrThrow();
  • ZenStack version: 3.5.4
  • Database type: Postgresql
  • Node.js/Bun version: Bun v.1.3.11
  • Package manager: bun

Additional context
This is especially confusing and annoying when using zenstack with orpc.
But thanks for all the amazing work in zenstack! Loving it so far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions