Skip to content

Conversation

sanny-io
Copy link

@sanny-io sanny-io commented Oct 6, 2025

v3 produces an error when generating the .ts schema with literals passed to functions like uuid() and nanoid()

model User {
    id String @id @default(uuid(7))
}

error TS2322: Type 'number' is not assignable to type 'Expression'.

This PR fixes that.

Summary by CodeRabbit

  • New Features
    • None
  • Bug Fixes
    • Corrects TypeScript schema generation for default function calls with literal arguments (e.g., UUID with a numeric parameter), ensuring defaults are accurately represented and consistent.
  • Tests
    • Added tests validating correct handling of default literal function arguments in generated schemas.

Copy link

coderabbitai bot commented Oct 6, 2025

Walkthrough

Updates the TypeScript schema generator to wrap default function-call arguments with ExpressionUtils.literal and adds tests validating schema generation for default uuid(7) calls, including id fields, @id, @default with call args, and model metadata. Duplicate test cases verify the same behavior in the test file.

Changes

Cohort / File(s) Summary
TS schema generator: default call args wrapping
packages/sdk/src/ts-schema-generator.ts
Changes default "call" handling to wrap each arg with ExpressionUtils.literal, replacing direct literal nodes in generated code.
CLI tests: default literal function arguments
packages/cli/test/ts-schema-gen.test.ts
Adds two identical tests ensuring generated schemas represent default uuid(7) as a function call with a literal 7 in args and related model metadata.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

A nibble of code, a hop through the trees,
I wrap little literals with elegant ease.
uuid(7) sings, the schema agrees—
Tests multiply like clover and bees.
Thump goes my paw: shipped with breezy peace. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Title Check ✅ Passed The title succinctly indicates that the pull request addresses an issue with literal function arguments in @default() decorators, which aligns directly with the PR’s goal of fixing TypeScript schema generation for default literals passed to functions. It is concise, clear, and focused on the primary change without extraneous details.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/cli/test/ts-schema-gen.test.ts (1)

364-424: Test correctly validates the fix but consider expanding coverage.

The test properly validates that literal arguments passed to default functions (e.g., uuid(7)) are correctly wrapped as expression literals in the generated schema, which addresses the PR objective.

However:

  • The PR description mentions both uuid() and nanoid() functions, but only uuid() is tested.
  • Consider adding test cases for other scenarios: multiple arguments (e.g., uuid(7, 8)), string literals (e.g., uuid('v4')), and the nanoid() function.
  • The AI summary mentions "duplicate test cases," but only one test is visible in the annotated code. If there's a duplicate test elsewhere in the file, consider consolidating them.

To enhance test coverage, consider adding:

it('generates correct default literal function arguments for various functions', async () => {
    const { schema } = await generateTsSchema(`
model User {
    id String @id @default(nanoid(10))
    uuid String @default(uuid(7))
}
    `);

    expect(schema.models.User.fields.id.default).toMatchObject({
        kind: "call",
        function: "nanoid",
        args: [{ kind: "literal", value: 10 }]
    });
    
    expect(schema.models.User.fields.uuid.default).toMatchObject({
        kind: "call",
        function: "uuid",
        args: [{ kind: "literal", value: 7 }]
    });
});
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1a8cef and 7aa8d56.

📒 Files selected for processing (2)
  • packages/cli/test/ts-schema-gen.test.ts (1 hunks)
  • packages/sdk/src/ts-schema-generator.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
{packages,samples,tests}/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place packages only under packages/, samples/, or tests/

Files:

  • packages/sdk/src/ts-schema-generator.ts
  • packages/cli/test/ts-schema-gen.test.ts
🧬 Code graph analysis (1)
packages/cli/test/ts-schema-gen.test.ts (2)
packages/testtools/src/schema.ts (1)
  • generateTsSchema (35-65)
samples/blog/zenstack/schema.ts (1)
  • schema (9-230)
🔇 Additional comments (1)
packages/sdk/src/ts-schema-generator.ts (1)

510-512: LGTM! The fix correctly resolves the TypeScript error.

The change properly wraps each default function argument in ExpressionUtils.literal, ensuring type compatibility with the Expression type. This is consistent with how literal expressions are handled elsewhere in the codebase (e.g., createLiteralExpression at lines 1099-1108).

The fix correctly handles the flow where:

  1. getMappedValue (line 626) extracts literal values from function arguments via getLiteral
  2. Those primitive values (string|number|boolean) are converted to TypeScript AST nodes via createLiteralNode
  3. Each node is now wrapped in ExpressionUtils.literal to match the Expression type signature

This enables default function calls like @default(uuid(7)) to compile successfully.

@sanny-io sanny-io changed the title fix: default literal function arguments fix: literal function arguments with @default() Oct 6, 2025
Copy link
Member

@ymc9 ymc9 left a comment

Choose a reason for hiding this comment

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

Thank you for making the fix @sanny-io ! Looks great to me.

@ymc9 ymc9 merged commit 348abee into zenstackhq:dev Oct 9, 2025
6 of 7 checks passed
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.

2 participants