Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Jan 25, 2026

fixes #592

Summary by CodeRabbit

  • New Features

    • Enum-like field types now map correctly in generated schemas.
    • String and number default values supported for scalar fields in generated schemas.
  • Tests

    • Added end-to-end tests for CLI schema generation and verification, including coverage for custom defaults and auth configurations.
  • Chores

    • Added test script and updated development tooling entries in package configuration.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings January 25, 2026 14:46
@coderabbitai
Copy link

coderabbitai bot commented Jan 25, 2026

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Schema generator now accepts enum-like field types (string arrays) and string/number defaults; adds tests for CLI generate/check and two auth fixtures; updates the better-auth package.json with a test script and devDependency adjustments.

Changes

Cohort / File(s) Summary
Package Configuration
packages/auth-adapters/better-auth/package.json
Added "test": "vitest run" script; pinned/updated devDependencies (@better-auth/core and better-auth1.4.17), and added @better-auth/cli@1.4.17, @types/tmp: catalog:, @zenstackhq/cli: workspace:*, tmp: catalog:.
Schema Generator
packages/auth-adapters/better-auth/src/schema-generator.ts
Recognize enum-like field types expressed as arrays of literal strings (or comma-separated string) and map them to String; added handling for string and numeric default values via createNumberAttributeArg; imported NumberLiteral.
Test Fixtures
packages/auth-adapters/better-auth/test/auth.ts, packages/auth-adapters/better-auth/test/auth-custom.ts
New fixtures exporting auth configurations; auth-custom.ts defines additional user fields (role, lang, age, admin) with types, required flags, and defaults.
CLI Integration Tests
packages/auth-adapters/better-auth/test/cli-generate.test.ts
New tests that run the Better Auth CLI generate/check flows, verifying generated schema and asserting presence of expected default values for custom fields.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐇 I nibbled through arrays and commas bright,
Mapped unions to strings with gentle bite.
Numbers and defaults tucked in snug and warm,
I hopped the tests and watched the CLI perform.
Hooray — the schema blooms in meadowed form!

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: support for enum-like field types (arrays of literal strings) and default values in better-auth schema generation.
Linked Issues check ✅ Passed The PR successfully addresses issue #592 by adding support for Array enum-like fields and string/number defaults in the better-auth schema generator.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing issue #592: schema generator enhancements, test configurations, and comprehensive test coverage for the enum field and default value functionality.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude
Copy link
Contributor

claude bot commented Jan 25, 2026

Claude finished @ymc9's task —— View job


Code Review Complete ✅

I've completed my review of PR #621 which fixes issue #592 for better-auth adapter support of custom table enum field types and defaults.

Todo List:

  • Gather context by reading the changed files to understand the PR changes
  • Analyze the schema generator changes for code quality and potential issues
  • Review the new test files for coverage and test quality
  • Check for security concerns and best practices
  • Provide comprehensive review feedback via GitHub comment

Summary

This PR enhances the better-auth adapter's schema generator to support:

  1. Custom enum field types - Arrays like ['user', 'admin'] and comma-separated strings are now mapped to String types
  2. Additional default value types - String and number default values are now supported alongside existing boolean support

Code Quality Assessment

Positive Aspects

  1. Robust Type Handling: The changes in getMappedFieldType() add proper support for enum types with clear logic:

    // Lines 264-267: schema-generator.ts
    if (Array.isArray(type) || (typeof type === 'string' && type.includes(','))) {
        return { type: 'String' };
    }
  2. Complete Default Value Support: The enhancement properly handles string and number defaults with dedicated helper functions:

    // Lines 341-344: schema-generator.ts
    } else if (typeof field.defaultValue === 'string') {
        addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);
    } else if (typeof field.defaultValue === 'number') {
        addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);
  3. Excellent Test Coverage: The new test suite comprehensively validates the functionality:

    • Tests both simple and custom configurations
    • Verifies generated schema content for all supported default value types
    • Includes integration tests with the CLI
  4. Consistent Code Style: New helper function createNumberAttributeArg() follows the same pattern as existing helper functions

⚠️ Areas for Improvement

  1. Missing Import Declaration: The NumberLiteral import is added but could be alphabetically sorted with other AST imports for consistency.

  2. Enum Mapping Strategy: Currently maps all enums to String type. Consider documenting why this approach was chosen over creating actual enum types in the schema.

  3. Error Handling: No validation that enum arrays are non-empty or contain valid string values.

Security Assessment ✅

  • No security concerns identified
  • The changes handle user input appropriately by mapping enum types to safe String types
  • Default value handling uses proper type checking and dedicated helper functions

Performance Considerations ✅

  • Changes are minimal and don't introduce performance overhead
  • String type mapping for enums is efficient
  • Helper functions follow existing patterns without additional complexity

Test Coverage Assessment ✅

The test suite is comprehensive:

  • Tests simple configuration (auth.ts)
  • Tests complex configuration with custom fields and defaults (auth-custom.ts)
  • Validates schema generation and verification
  • Checks specific content patterns in generated schemas
  • Includes integration testing with CLI tools

Recommendations

  1. Consider enum documentation: Add a comment explaining the design decision to map enums to String types rather than creating actual enum definitions.

  2. Optional validation: Consider adding validation for enum array values to ensure they contain valid strings.

  3. Future enhancement: The foundation is now laid for supporting actual enum types in the schema if needed in the future.

Final Assessment

This is a well-implemented fix that addresses the issue comprehensively. The code is clean, well-tested, and follows existing patterns. The changes are minimal and focused, reducing risk while adding the requested functionality.

Recommendation: ✅ Approve


Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #592 by adding support for custom table enum field types and default values in the ZenStack better-auth adapter. The issue occurred when better-auth's schema generator encountered enum-like field types (arrays of literal strings) which were not previously supported.

Changes:

  • Added enum type handling to map array and comma-separated enum types to String type in the schema generator
  • Added support for string and number default values in addition to existing boolean support
  • Added test suite to verify schema generation with custom field types and defaults

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
packages/auth-adapters/better-auth/src/schema-generator.ts Adds enum type detection and handling in getMappedFieldType, adds createNumberAttributeArg and createStringAttributeArg functions, imports NumberLiteral type
packages/auth-adapters/better-auth/test/cli-generate.test.ts New test file that generates schemas via CLI and verifies correctness
packages/auth-adapters/better-auth/test/auth.ts Simple auth configuration for basic test case
packages/auth-adapters/better-auth/test/auth-custom.ts Custom auth configuration with enum fields and various default value types
packages/auth-adapters/better-auth/package.json Adds test script and test dependencies (@better-auth/cli, tmp, @types/tmp, @zenstackhq/cli)
pnpm-lock.yaml Updates lock file with new dependencies and their transitive dependencies
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@claude
Copy link
Contributor

claude bot commented Jan 25, 2026

Claude finished @ymc9's task —— View job


Code Review Complete ✅

I've completed my review of PR #621 which fixes issue #592 for better-auth adapter support of custom table enum field types and defaults.

Todo List:

  • Gather context by reading the changed files to understand the PR changes
  • Analyze the schema generator changes for code quality and potential issues
  • Review the new test files for coverage and test quality
  • Check for security concerns and best practices
  • Provide comprehensive review feedback via GitHub comment

Summary

This PR enhances the better-auth adapter's schema generator to support:

  1. Custom enum field types - Arrays like ['user', 'admin'] and comma-separated strings are now mapped to String types
  2. Additional default value types - String and number default values are now supported alongside existing boolean support

Code Quality Assessment

Positive Aspects

  1. Robust Type Handling: The changes in getMappedFieldType() add proper support for enum types with clear logic:

    // Lines 264-267: schema-generator.ts
    if (Array.isArray(type) || (typeof type === 'string' && type.includes(','))) {
        return { type: 'String' };
    }
  2. Complete Default Value Support: The enhancement properly handles string and number defaults with dedicated helper functions:

    // Lines 341-344: schema-generator.ts
    } else if (typeof field.defaultValue === 'string') {
        addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);
    } else if (typeof field.defaultValue === 'number') {
        addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);
  3. Excellent Test Coverage: The new test suite comprehensively validates the functionality:

    • Tests both simple and custom configurations
    • Verifies generated schema content for all supported default value types
    • Includes integration tests with the CLI
  4. Consistent Code Style: New helper function createNumberAttributeArg() follows the same pattern as existing helper functions

⚠️ Areas for Improvement

  1. Missing Import Declaration: The NumberLiteral import is added but could be alphabetically sorted with other AST imports for consistency.

  2. Enum Mapping Strategy: Currently maps all enums to String type. Consider documenting why this approach was chosen over creating actual enum types in the schema.

  3. Error Handling: No validation that enum arrays are non-empty or contain valid string values.

Security Assessment ✅

  • No security concerns identified
  • The changes handle user input appropriately by mapping enum types to safe String types
  • Default value handling uses proper type checking and dedicated helper functions

Performance Considerations ✅

  • Changes are minimal and don't introduce performance overhead
  • String type mapping for enums is efficient
  • Helper functions follow existing patterns without additional complexity

Test Coverage Assessment ✅

The test suite is comprehensive:

  • Tests simple configuration (auth.ts)
  • Tests complex configuration with custom fields and defaults (auth-custom.ts)
  • Validates schema generation and verification
  • Checks specific content patterns in generated schemas
  • Includes integration testing with CLI tools

Recommendations

  1. Consider enum documentation: Add a comment explaining the design decision to map enums to String types rather than creating actual enum definitions.

  2. Optional validation: Consider adding validation for enum array values to ensure they contain valid strings.

  3. Future enhancement: The foundation is now laid for supporting actual enum types in the schema if needed in the future.

Final Assessment

This is a well-implemented fix that addresses the issue comprehensively. The code is clean, well-tested, and follows existing patterns. The changes are minimal and focused, reducing risk while adding the requested functionality.

Recommendation: ✅ Approve


@claude
Copy link
Contributor

claude bot commented Jan 26, 2026

Claude finished @ymc9's task —— View job


Code Review Complete ✅

I've completed my review of PR #621 which fixes issue #592 for better-auth adapter support of custom table enum field types and defaults.

Todo List:

  • Gather context by reading the changed files to understand the PR changes
  • Analyze the schema generator changes for code quality and potential issues
  • Review the new test files for coverage and test quality
  • Check for security concerns and best practices
  • Provide comprehensive review feedback via GitHub comment

Summary

This PR enhances the better-auth adapter's schema generator to support:

  1. Custom enum field types - Arrays like ['user', 'admin'] and comma-separated strings are now mapped to String types
  2. Additional default value types - String and number default values are now supported alongside existing boolean support

Code Quality Assessment

Positive Aspects

  1. Robust Type Handling: The changes in getMappedFieldType() add proper support for enum types with clear logic:

    // Lines 264-267: schema-generator.ts
    if (Array.isArray(type) || (typeof type === 'string' && type.includes(','))) {
        return { type: 'String' };
    }
  2. Complete Default Value Support: The enhancement properly handles string and number defaults with dedicated helper functions:

    // Lines 341-344: schema-generator.ts
    } else if (typeof field.defaultValue === 'string') {
        addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);
    } else if (typeof field.defaultValue === 'number') {
        addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);
  3. Excellent Test Coverage: The new test suite comprehensively validates the functionality:

    • Tests both simple and custom configurations
    • Verifies generated schema content for all supported default value types
    • Includes integration tests with the CLI
  4. Consistent Code Style: New helper function createNumberAttributeArg() follows the same pattern as existing helper functions

⚠️ Areas for Improvement

  1. Missing Import Declaration: The NumberLiteral import is added but could be alphabetically sorted with other AST imports for consistency.

  2. Enum Mapping Strategy: Currently maps all enums to String type. Consider documenting why this approach was chosen over creating actual enum types in the schema.

  3. Error Handling: No validation that enum arrays are non-empty or contain valid string values.

Security Assessment ✅

  • No security concerns identified
  • The changes handle user input appropriately by mapping enum types to safe String types
  • Default value handling uses proper type checking and dedicated helper functions

Performance Considerations ✅

  • Changes are minimal and don't introduce performance overhead
  • String type mapping for enums is efficient
  • Helper functions follow existing patterns without additional complexity

Test Coverage Assessment ✅

The test suite is comprehensive:

  • Tests simple configuration (auth.ts)
  • Tests complex configuration with custom fields and defaults (auth-custom.ts)
  • Validates schema generation and verification
  • Checks specific content patterns in generated schemas
  • Includes integration testing with CLI tools

Recommendations

  1. Consider enum documentation: Add a comment explaining the design decision to map enums to String types rather than creating actual enum definitions.

  2. Optional validation: Consider adding validation for enum array values to ensure they contain valid strings.

  3. Future enhancement: The foundation is now laid for supporting actual enum types in the schema if needed in the future.

Final Assessment

This is a well-implemented fix that addresses the issue comprehensively. The code is clean, well-tested, and follows existing patterns. The changes are minimal and focused, reducing risk while adding the requested functionality.

Recommendation: ✅ Approve


@ymc9 ymc9 merged commit 8fdd349 into dev Jan 26, 2026
9 checks passed
@ymc9 ymc9 deleted the fix/issue-592 branch January 26, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant