Skip to content

Support for Array<LiteralString> in schema generation of zenstack's better-auth adapter #592

@MarcelCoder2002

Description

@MarcelCoder2002

Describe the bug

When generating schemas using the @zenstackhq/better-auth adapter, fields typed as an array of literal strings string[] (not "string[]") are not supported and cause schema generation to fail with an unsupported field type error.

To Reproduce

Steps to reproduce the behavior:

  • Create a ZenStack project using v3 and install @zenstackhq/better-auth.
  • Define a model with a field that uses a string union array type (enum like).
export const auth = betterAuth({
  database: zenstackBetterAuthAdapter,
  ...,
  user: {
    ...,
    additionalFields: {
      ...,
      gender: {
        type: ["MALE", "FEMALE", "OTHER"],
        required: false,
        defaultValue: "OTHER",
      },
      ...
    },
  },
});
  • Run schema generation with better-auth cli.
npx @better-auth/cli@latest generate

The following error is thrown:

⠙ preparing schema...file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:214
    throw new Error(`Unsupported field type: ${type}`);
          ^

Error: Unsupported field type: MALE,FEMALE,OTHER
    at file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:214:11
    at R.otherwise (file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/index.js:1:7823)
    at getMappedFieldType (file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:213:7)
    at addOrUpdateModel (file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:240:31)
    at updateSchema (file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:64:15)
    at async generateSchema (file:///Users/mac/WebstormProjects/marketplace/node_modules/.pnpm/@zenstackhq+better-auth@3.2.0_3a29c8d062343b3626c9d8e956a3c0ef/node_modules/@zenstackhq/better-auth/dist/index.js:25:18)
    at async Command.generateAction (file:///Users/mac/Library/Caches/pnpm/dlx/4547bc99f64ad53a46f476928a2dbd376977838838508c302612f1e11c6f9437/19bad31fcfb-e7dc/node_modules/.pnpm/@better-auth+cli@1.4.10_@better-fetch+fetch@1.1.21_better-call@1.1.7_zod@4.3.5__jose@6._57191032f4e5315b88c32194d7e6b2da/node_modules/@better-auth/cli/dist/index.mjs:1028:17)

Expected behavior

The schema generator should either:

  • Treat better-auth's Array<LiteralString> as a supported type string[],
  • Mapping Array<LiteralString> to enum or String type in schema.zmodel

This would allow TS developers to define fields like role or gender using string unions without losing type safety or having to manually convert to string[].

Thanks for the awesome work! 🙌

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions