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

Open
opened 2026-03-13 12:53:32 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5869
Author: @GautamBytes
Created: 11/9/2025
Status: 🔄 Open

Base: canaryHead: feat/zod-additional-fields


📝 Commits (2)

  • 70334a2 feat(db): Allow Zod schema for additionalFields
  • 5eb3669 Correct Zod type-level and runtime logic

📊 Changes

7 files changed (+315 additions, -41 deletions)

View changed files

📝 packages/better-auth/src/db/field.ts (+11 -3)
packages/better-auth/src/db/from-zod-types.ts (+49 -0)
packages/better-auth/src/db/from-zod.ts (+164 -0)
📝 packages/better-auth/src/db/get-tables.ts (+16 -3)
📝 packages/better-auth/src/db/index.ts (+2 -0)
📝 packages/better-auth/src/db/schema.ts (+40 -11)
📝 packages/better-auth/src/plugins/additional-fields/client.ts (+33 -24)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5869 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 11/9/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/zod-additional-fields` --- ### 📝 Commits (2) - [`70334a2`](https://github.com/better-auth/better-auth/commit/70334a2b686d69bc9564d3513510046f4157cf4d) feat(db): Allow Zod schema for additionalFields - [`5eb3669`](https://github.com/better-auth/better-auth/commit/5eb3669ea5a87dfd433a2a1a00b9d9bf87a4500f) Correct Zod type-level and runtime logic ### 📊 Changes **7 files changed** (+315 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/field.ts` (+11 -3) ➕ `packages/better-auth/src/db/from-zod-types.ts` (+49 -0) ➕ `packages/better-auth/src/db/from-zod.ts` (+164 -0) 📝 `packages/better-auth/src/db/get-tables.ts` (+16 -3) 📝 `packages/better-auth/src/db/index.ts` (+2 -0) 📝 `packages/better-auth/src/db/schema.ts` (+40 -11) 📝 `packages/better-auth/src/plugins/additional-fields/client.ts` (+33 -24) </details> ### 📄 Description 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. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:53:32 -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#6287