honour Date object passed as legacy 2nd arg to isAfter / isBefore#2783
Conversation
…and isBefore
isAfter and isBefore support a legacy two-argument form `isAfter(str, date)` where the
second argument is a date, plus a modern one-argument form that takes an options
object with `comparisonDate`. The legacy/object disambiguation was done with
`typeof options === 'object'`, which is true for any Date object. So when a caller
wrote `isAfter('2010-01-02', new Date('2010-01-01'))` the legacy branch fired, the
code looked up `options.comparisonDate` on the Date (which is `undefined`), fell
back to `Date().toString()` for "now", and silently compared against the current
moment instead of the supplied Date. `isAfter('2010-01-02', new Date('2010-01-01'))`
returned `false` even though 2010-01-02 is after 2010-01-01, and `isBefore` had
the symmetric problem.
Hoist the disambiguation into a small `resolveComparisonDate` helper that
recognises a `Date` instance directly, and short-circuit `toDate()` when the
comparison date is already a `Date`. The `typeof === 'object'` branch is also
guarded against `null` for the same reason the rest of the codebase already
guards null options.
tests/testFunctions.js serialises args via `JSON.stringify` for error messages,
so I exercised the new path with a hand-built assertion rather than through the
shared `test` helper, which round-trips args through JSON and would lose the
Date identity. The new cases fail on master and pass on the fix.
- test/validators/isAfter.test.js: add a '(legacy syntax with Date object)' describe
block that asserts `isAfter('2010-01-02', new Date('2010-01-01'))` is true,
`isAfter('2009-12-31', new Date('2010-01-01'))` is false, and that an invalid
Date falls through to `false`.
- test/validators/isBefore.test.js: mirror the same three cases for isBefore.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2783 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2586 2602 +16
Branches 656 661 +5
=========================================
+ Hits 2586 2602 +16 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I do think that having |
AI-assisted: Zo Computer GPT-5.6 Luna
tux-tn
left a comment
There was a problem hiding this comment.
I agree with @WikiRik. Supporting Date objects here feels inconsistent with the library’s general spirit of validating string inputs and keeping the API focused on string-based validation. This would be an API expansion rather than a backwards-compatibility fix.
AI-assisted: Zo Computer GPT-5.6 Luna
What this PR fixes
isAfterandisBeforesilently ignore aDateobject that callers pass asthe legacy second argument. The disambiguation between "this is the legacy date
arg" and "this is the new options object" was a
typeof options === 'object'check, which is true for any
Dateas well, so the new-options branch alwayswon.
isAfter('2010-01-02', new Date('2010-01-01'))returnedfalseeventhough 2010-01-02 is plainly after 2010-01-01, because the code then did
options.comparisonDate(which isundefinedon a Date), fell back toDate().toString()for "now", and compared against the current moment.Repro
Fix
Hoist the disambiguation into a small
resolveComparisonDatehelper thatrecognises a
Dateinstance directly, and short-circuittoDate()when thecomparison date is already a
Date. Thetypeof === 'object'branch is alsoguarded against
nullfor the same reason the rest of the codebase alreadyguards null options.
Tests
The shared
testhelper intest/testFunctions.jsserialises args viaJSON.stringifyfor error messages, which would strip the Date identity. Thenew cases are written as direct assertions against the validator instead.
test/validators/isAfter.test.js— new '(legacy syntax with Date object)'describe block:
isAfter('2010-01-02', new Date('2010-01-01'))is trueisAfter('2009-12-31', new Date('2010-01-01'))is falseisAfter('2010-01-02', new Date('not a date'))is falsetest/validators/isBefore.test.js— same three cases forisBefore.Both new test blocks fail on master (
Stashed the source changes and ran the suite: 2 failing) and pass on the fix (320 passing).ESLint passes on the modified files.