Fix: BigInt validator coercing non-validator attributes (#423)#427
Fix: BigInt validator coercing non-validator attributes (#423)#427ymc9 merged 1 commit intozenstackhq:devfrom
Conversation
* stop coercing mapped attribute values into BigInt inside addBigIntValidation, limiting conversion to real validator attributes * add regression test proving @map-decorated BigInt fields create and update successfully
WalkthroughAddresses issue Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. 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. Comment |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where the BigInt validator was attempting to coerce all field attributes to BigInt values, including non-validation attributes like @map, causing errors when those attributes contained string values.
Key Changes:
- Modified
addBigIntValidationto defer BigInt coercion until inside validation attribute matchers (@gt, @gte, @lt, @LTe) - Added regression test validating that BigInt fields with
@mapdecorator work correctly for create and update operations
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
packages/orm/src/client/crud/validator/utils.ts |
Fixed premature BigInt coercion by moving conversion inside validation attribute matchers |
tests/regression/test/issue-423.test.ts |
Added regression test for BigInt fields with @Map decorator |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/orm/src/client/crud/validator/utils.ts (1)
117-144: LGTM! Fix correctly prevents BigInt coercion of non-validator attributes.The change successfully addresses issue #423 by moving the
BigInt(val)conversion from an unconditional position (previously at line 128, before the match statement) into each validation branch. This ensures that only validator attributes (@gt,@gte,@lt,@lte) trigger BigInt conversion, while non-validator attributes like@mapare safely ignored by the match statement.The fix prevents the
SyntaxError: Failed to parse String to BigIntthat occurred whenaddBigIntValidationattempted to coerce@map("account_id")string values into BigInt.Optional refinement for efficiency:
Currently,
getArgValueis called for ALL attributes at line 124, even though only validation attributes need it. Consider checkingattr.namefirst to avoid unnecessary value extraction for non-validator attributes:for (const attr of attributes) { + if (!['"@gt', '@gte', '@lt', '@lte'].includes(attr.name)) { + continue; + } const val = getArgValue<number>(attr.args?.[0]?.value); if (val === undefined) { continue; }However, the current approach is consistent with
addNumberValidationandaddStringValidation, so this optimization is optional.tests/regression/test/issue-423.test.ts (1)
4-16: Good regression test, but consider expanding coverage.The test successfully verifies that BigInt fields with
@mapattributes no longer cause "Failed to parse String to BigInt" errors during create and update operations.Enhance test coverage for validation attributes:
To ensure the fix doesn't break existing validation functionality, consider adding a test case that verifies BigInt validation attributes (
@gt,@gte,@lt,@lte) still work correctly:it('verifies BigInt validation attributes still work', async () => { const db = await createTestClient( ` model SampleBigInt { id BigInt @id @map("sample_id") count BigInt @gt(0) @lte(1000) }` ); // Should succeed await expect( db.SampleBigInt.create({ data: { id: BigInt(1), count: BigInt(50) } }) ).resolves.toMatchObject({ id: BigInt(1), count: BigInt(50) }); // Should fail validation await expect( db.SampleBigInt.create({ data: { id: BigInt(2), count: BigInt(-1) } }) ).rejects.toThrow(); });Additionally, the original issue #423 mentioned
@default(autoincrement()). Consider testing that scenario as well to ensure comprehensive coverage.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/orm/src/client/crud/validator/utils.ts(1 hunks)tests/regression/test/issue-423.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/regression/test/issue-423.test.ts (1)
packages/testtools/src/client.ts (1)
createTestClient(95-244)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Upload results
ymc9
left a comment
There was a problem hiding this comment.
LGTM! Thanks for making this PR. It took me a while to understand why it interferes with @map 😂
Fixes #423
The BigInt validator was attempting to coerce any value regardless if it was a validation attribute to a BigInt, this caused issues for things like
@map(column_name)from working because it would throw an error attempting to coerce "column_name" into a BigInt value.Changes:
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests