Skip to content

makeModelSchema: DateTime fields infer as unknown in z.input #2673

@ludovicmotte

Description

@ludovicmotte

Description and expected behavior
makeModelSchema: DateTime fields infer as unknown in z.input.
It breaks my usage of react-hook-form.

export const PostSchema = schemaFactory.makeModelSchema("Post", {
  optionality: "defaults", // fields that have a @default or @updatedAt attribute become optional
  omit: { authorId: true }, // scalar fields linking to relations are omitted
});

export type PostFormData = z.infer<typeof PostSchema>;

type _DebugOutput = z.infer<typeof PostSchema>["createdAt"];
// type _DebugOutput = Date | undefined

type _DebugInput = z.input<typeof PostSchema>["createdAt"];
//  type _DebugInput = unknown

// @zenstackhq/zod maps DateTime to z.ZodType<Date> instead of z.ZodDate,
// which makes z.input degrade to unknown.
// This breaks any library that uses the input type (like @hookform/resolvers).
// The fix  would be to use z.ZodDate (or the concrete coercion type) in FieldTypeZodMap.

Environment (please complete the following information):

  • ZenStack version: 3.6.4
  • Database type: Postgresql
  • Node.js version: v24.15.0
  • Package manager: npm

Additional context
With react-hook-form

  const {
    register,
    handleSubmit,
    reset,
    watch,
    setError,
    formState: { errors, isSubmitting, isSubmitSuccessful },
  } = useForm<PostFormData>({
    resolver: zodResolver(PostSchema),
    defaultValues: {
      title: post?.title ?? "",
      content: post?.content ?? "",
      published: post?.published ?? false,
    },
  });

zodResolver(PostSchema) has type issues.

Idem for Decimal, Bytes, ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions