Skip to content

@zenstackhq/zod scalar array types are incorrect #2499

@haltcase

Description

@haltcase

Description and expected behavior
A clear and concise description of what the bug is and what's the expected behavior.

The TypeScript definition for inferring a Zod schema type from a Zen schema is incorrect for scalar arrays like String[]. The array-ness is lost, and the Zod types report z.ZodString instead of z.ZodArray<z.ZodString>.

This seems to be due to a missing ZodArrayIf in the scalar branch here:

export type GetModelFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
// scalar fields
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: Field]: ZodOptionalAndNullableIf<
MapModelFieldToZod<Schema, Model, Field>,
ModelFieldIsOptional<Schema, Model, Field>
>;

This means types do not match runtime, where the Zod schema is actually correct, because applyCardinality is used properly in makeScalarFieldSchema here:

return this.applyCardinality(base, fieldDef);

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • ZenStack version: 3.4.6 with @zenstackhq/zod@3.4.6
  • Database type: any
  • Node.js/Bun version: any
  • Package manager: any

Additional context
Add any other context about the problem here.

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