-
Notifications
You must be signed in to change notification settings - Fork 306
Add runtime validation for columnMapper to catch common mistakes #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add runtime validation to catch the common mistake of passing factory functions (like `snakeCamelMapper`) instead of calling them to get the ColumnMapper object (`snakeCamelMapper()`). Changes: - Add branded type to ColumnMapper interface using a Symbol to help TypeScript distinguish ColumnMapper objects from functions - Add `isValidColumnMapper()` helper function to validate at runtime - Add `InvalidColumnMapperError` with descriptive error messages that mention the function name and correct usage - Add comprehensive tests for validation and branding This fixes an issue where TypeScript may not catch this mistake if strict settings aren't enabled or when using JavaScript. https://claude.ai/code/session_01NxuoHM9wcmxBvhN5EbARdM
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
Comment |
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3774 +/- ##
==========================================
+ Coverage 87.36% 87.65% +0.28%
==========================================
Files 23 23
Lines 2011 2025 +14
Branches 528 539 +11
==========================================
+ Hits 1757 1775 +18
+ Misses 252 248 -4
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
…rove docs - Add clarifying comment explaining why COLUMN_MAPPER_BRAND is not checked at runtime (backwards compatibility with custom mappers) - Fix misleading JSDoc that claimed isValidColumnMapper throws - Remove redundant function type check (covered by typeof !== 'object') - Remove unnecessary comments and trailing return in validateOptions - Add test for anonymous function error message fallback - Enhance test to verify helpful error guidance for invalid objects Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…e inference Extract function via array access to prevent JavaScript from inferring the name from the property assignment. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
samwillis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Runtime validation looks good, but the COLUMN_MAPPER_BRAND looks entirely redundant. Suspect Claude tried to use it but could not for backwards compatibility?
The brand symbol was added but never used for validation (to maintain backwards compatibility with custom mappers). Remove it entirely since it adds complexity without benefit - runtime validation catches the common mistake of passing factory functions without calling them. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Good catch! Removed the |
|
@samwillis ready for a re-review when you're ready |
Summary
Adds runtime validation for the
columnMapperoption inShapeStreamto catch a common developer mistake: passing the factory function (snakeCamelMapper) instead of calling it (snakeCamelMapper()).Root Cause
TypeScript's structural typing means that both
snakeCamelMapper(the function) andsnakeCamelMapper()(the result) type-check as validcolumnMappervalues at compile time. When users make this mistake, they get confusing runtime errors deep in the column mapping logic rather than a clear error at construction time.Approach
COLUMN_MAPPER_BRAND) added toColumnMapperinterface to distinguish objects from functions at the type levelvalidateOptions()checks:encode/decodemethodsKey Invariants
snakeCamelMapper(),createColumnMapper()) always return branded objectsNon-goals
{ encode, decode }objects should continue workingVerification
Files Changed
src/error.tsInvalidColumnMapperErrorclasssrc/column-mapper.tsCOLUMN_MAPPER_BRAND,isValidColumnMapper(), update factoriessrc/client.tsvalidateOptions()src/index.tstest/column-mapper.test.tstest/stream.test.ts🤖 Generated with Claude Code