fix(js/ts): reject JS-permissive date strings that .NET TryParse rejects#4588
Draft
fable-repo-assist[bot] wants to merge 2 commits intomainfrom
Draft
Conversation
V8's Date constructor accepts strings like "ABC 6" by treating the letter sequence as a timezone abbreviation. .NET DateTime.TryParse rejects such strings as invalid. Add a pre-validation check in parseRaw() that requires the input to start with a digit or a recognised month-name prefix (Jan–Dec). This matches .NET behaviour while preserving all existing valid formats. Closes #3858 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Fixes
DateTime.TryParseaccepting strings in JavaScript that .NET correctly rejects.Closes #3858
Root Cause
V8's
Dateconstructor is overly permissive — it accepts strings like"ABC 6"by treating"ABC"as a timezone abbreviation and"6"as a year/month. .NET'sDateTime.TryParserejects such strings because they don't match any recognised date format pattern.In
parseRaw(), the code callsnew Date(input)first and only falls back to a strict regex whenisNaN(date.getTime()). Since V8 successfully parses"ABC 6", the guard never fires.Fix
Add a pre-validation check at the top of
parseRaw()that requires the string to start with either a digit or a recognised month-name prefix (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec — case-insensitive):This lets valid formats through:
"9/10/2014","2024-01-15","2024-01-15T12:00:00Z""January 15, 2024","Dec 25 2024""12:00:00"And rejects V8-specific quirks:
"ABC 6"→ fails (starts withA, not a digit or month name) ✓"XYZ 2024"→ fails ✓"foo"→ still fails (already tested) ✓Changes
src/fable-library-ts/Date.ts: add pre-validation inparseRaw()tests/Js/Main/DateTimeTests.fs: add regression test for"ABC 6"and"XYZ 2024"Trade-offs
The regex is simple and only checks the start of the string. It does not attempt to fully validate the date format (that remains the job of
new Date()and the fallback regex). This minimises the risk of breaking existing valid inputs while fixing the specific V8 over-acceptance bug.