additionalFields validator input don't return zod issues to client, but return 500 #1195

Closed
opened 2026-03-13 08:27:38 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @reslear on GitHub (May 12, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Need add like:
user: {
    additionalFields: {
      username: {
        type: 'string',
        required: true,
        sortable: true,
        unique: true,
        returned: true,
        input: true,
        transform: {
          input(value) {
            return value?.toString().toLowerCase()
          },
        },
        validator: {
          input: z.string().regex(/^[\w.]+$/).min(3).max(30)
        },
      },
    },

  },
  1. Try put update user username

Image

Current vs. Expected behavior

Current:

return 500

# SERVER_ERROR:  ZodError: [
  {
    "validation": "regex",
    "code": "invalid_string",
    "message": "Invalid",
    "path": []
  }
]

 {
  issues: [
    {
      validation: 'regex',
      code: 'invalid_string',
      message: 'Invalid',
      path: []
    }
  ],
  addIssue: [Function (anonymous)],
  addIssues: [Function (anonymous)],
  errors: [
    {
      validation: 'regex',
      code: 'invalid_string',
      message: 'Invalid',
      path: []
    }
  ]
}

Expected behavior:

return zod errors to client

I'm guessing it's because the throw is not being processed.

a017cbbaca/packages/better-auth/src/api/routes/update-user.ts (L93)

maybe need add try/catch and send zod errors

or use safeParse

What version of Better Auth are you using?

latest

Provide environment information

not relevant

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },

user: {
    additionalFields: {
      username: {
        type: 'string',
        required: true,
        sortable: true,
        unique: true,
        returned: true,
        input: true,
        transform: {
          input(value) {
            return value?.toString().toLowerCase()
          },
        },
        validator: {
          input: z.string().regex(/^[\w.]+$/).min(3).max(30)
        },
      },
    },

  },
});

Additional context

No response

Originally created by @reslear on GitHub (May 12, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Need add like: ```ts user: { additionalFields: { username: { type: 'string', required: true, sortable: true, unique: true, returned: true, input: true, transform: { input(value) { return value?.toString().toLowerCase() }, }, validator: { input: z.string().regex(/^[\w.]+$/).min(3).max(30) }, }, }, }, ``` 2. Try put update user `username` ![Image](https://github.com/user-attachments/assets/6e8c95a3-a6c0-4732-80c2-de5ce776ff35) ### Current vs. Expected behavior Current: return 500 ```log # SERVER_ERROR: ZodError: [ { "validation": "regex", "code": "invalid_string", "message": "Invalid", "path": [] } ] { issues: [ { validation: 'regex', code: 'invalid_string', message: 'Invalid', path: [] } ], addIssue: [Function (anonymous)], addIssues: [Function (anonymous)], errors: [ { validation: 'regex', code: 'invalid_string', message: 'Invalid', path: [] } ] } ``` Expected behavior: return zod errors to client I'm guessing it's because the throw is not being processed. https://github.com/better-auth/better-auth/blob/a017cbbaca2f4d44c4545f3c77d52e5d7266270b/packages/better-auth/src/api/routes/update-user.ts#L93 maybe need add try/catch and send zod errors or use safeParse ### What version of Better Auth are you using? latest ### Provide environment information ```bash not relevant ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, user: { additionalFields: { username: { type: 'string', required: true, sortable: true, unique: true, returned: true, input: true, transform: { input(value) { return value?.toString().toLowerCase() }, }, validator: { input: z.string().regex(/^[\w.]+$/).min(3).max(30) }, }, }, }, }); ``` ### Additional context _No response_
Author
Owner

@Kinfe123 commented on GitHub (May 28, 2025):

for the client one you can use zod on client side on your app to help with that but once it reached the server it will validated against your validator and returns 500 if it is not valid. and we are using zod as standard for validations.

@Kinfe123 commented on GitHub (May 28, 2025): for the client one you can use zod on client side on your app to help with that but once it reached the server it will validated against your validator and returns 500 if it is not valid. and we are using zod as standard for validations.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1195