fix: prevent crash when date props are passed as strings#6166
Merged
martijnrusschen merged 1 commit intomainfrom Dec 18, 2025
Merged
fix: prevent crash when date props are passed as strings#6166martijnrusschen merged 1 commit intomainfrom
martijnrusschen merged 1 commit intomainfrom
Conversation
Added safeToDate() helper function that validates date values at runtime, returning null for invalid inputs (strings, numbers, invalid Date objects). This allows graceful fallback instead of crashing with "getTime/getFullYear is not a function" errors. Fixed locations: - time.tsx: isSelectedTime(), renderTimes() - validate selected/openToDate - index.tsx: handleTimeOnlyArrowKey(), handleTimeOnlyInputKeyDown() - validate selected - index.tsx: handleInputChange() - validate startDate/endDate before .getTime() Added tests: - 11 unit tests for safeToDate() function - 5 integration tests for string date prop handling in TimePicker Fixes #5964 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6166 +/- ##
=======================================
Coverage 99.26% 99.26%
=======================================
Files 30 30
Lines 3792 3801 +9
Branches 1631 1635 +4
=======================================
+ Hits 3764 3773 +9
Misses 27 27
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
safeToDate()helper function indate_utils.tsthat validates date values at runtimenullfor invalid inputs (strings, numbers, invalid Date objects), allowing graceful fallbackFixed Locations
time.tsx:isSelectedTime(),renderTimes()- validateselected/openToDatepropsindex.tsx:handleTimeOnlyArrowKey(),handleTimeOnlyInputKeyDown()- validateselectedpropindex.tsx:handleInputChange()- validatestartDate/endDatebefore calling.getTime()Root Cause
TypeScript types
selected,startDate,endDate, etc. asDate | null, but at runtime JavaScript consumers or incorrect usage could pass strings. The code assumed these were always validDateobjects and called methods like.getTime()directly, causing crashes.Solution
The
safeToDate()function checks:nullorundefined→ returnsnullDateobject (usingisDate()andisValid()from date-fns) → returns the datenullThis allows the existing fallback chains (e.g.,
selected || openToDate || newDate()) to work correctly.Test Plan
safeToDate()function covering valid dates, null, undefined, strings, numbers, invalid Date objectsFixes #5964
🤖 Generated with Claude Code