[PR #5869] feat: allow Zod schema for additionalFields and inferAdditionalFields #14534

Open
opened 2026-04-13 09:31:22 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5869

State: open
Merged: No


Closes #5854

This PR adds support for using a z.ZodObject to define additionalFields, both in the main BetterAuthOptions on the server and with the inferAdditionalFields helper on the client.

This solves the problem of needing to manually type the DBFieldAttribute map on the client, which was cumbersome and error-prone.

Changes

  • from-zod.ts (new): Adds a runtime converter fromZodSchema that maps a z.ZodObject to the internal DBFieldAttribute format.
  • from-zod-types.ts (new): Adds a compile-time type converter ZodSchemaToDBFields to provide full type-safety and inference.
  • get-tables.ts & schema.ts: Updated to use the fromZodSchema converter at runtime when a Zod schema is provided in the options.
  • client.ts: Updated inferAdditionalFields to use the ZodSchemaToDBFields type for compile-time inference.
  • field.ts & index.ts: Updated to export the new utilities and integrate the types.

Summary by cubic

Adds first-class Zod support for additionalFields on server and client, with runtime conversion and full type inference. This removes manual DBFieldAttribute maps and improves input validation.

  • New Features

    • Accept z.ZodObject for user/session/account additionalFields; convert at runtime via fromZodSchema.
    • inferAdditionalFields now infers types from Zod using ZodSchemaToDBFields.
    • InferFieldsFromOptions updated to support Zod schemas.
    • Export fromZodSchema and ZodSchemaToDBFields from the db package.
  • Bug Fixes

    • Input parsing detects Zod validators and returns Zod error details.
    • Default values are executed when functions, not stored as functions.

Written for commit 5eb3669ea5. Summary will update automatically on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5869 **State:** open **Merged:** No --- Closes #5854 This PR adds support for using a `z.ZodObject` to define `additionalFields`, both in the main `BetterAuthOptions` on the server and with the `inferAdditionalFields` helper on the client. This solves the problem of needing to manually type the `DBFieldAttribute` map on the client, which was cumbersome and error-prone. #### Changes * **`from-zod.ts` (new):** Adds a runtime converter `fromZodSchema` that maps a `z.ZodObject` to the internal `DBFieldAttribute` format. * **`from-zod-types.ts` (new):** Adds a compile-time type converter `ZodSchemaToDBFields` to provide full type-safety and inference. * **`get-tables.ts` & `schema.ts`:** Updated to use the `fromZodSchema` converter at runtime when a Zod schema is provided in the options. * **`client.ts`:** Updated `inferAdditionalFields` to use the `ZodSchemaToDBFields` type for compile-time inference. * **`field.ts` & `index.ts`:** Updated to export the new utilities and integrate the types. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds first-class Zod support for additionalFields on server and client, with runtime conversion and full type inference. This removes manual DBFieldAttribute maps and improves input validation. - **New Features** - Accept z.ZodObject for user/session/account additionalFields; convert at runtime via fromZodSchema. - inferAdditionalFields now infers types from Zod using ZodSchemaToDBFields. - InferFieldsFromOptions updated to support Zod schemas. - Export fromZodSchema and ZodSchemaToDBFields from the db package. - **Bug Fixes** - Input parsing detects Zod validators and returns Zod error details. - Default values are executed when functions, not stored as functions. <sup>Written for commit 5eb3669ea5a87dfd433a2a1a00b9d9bf87a4500f. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 09:31:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#14534