diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 149622a0d..0d9256e55 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -1,11 +1,23 @@ import toDate from './toDate'; +function resolveComparisonDate(options) { + if (options instanceof Date) { + return options; + } + if (typeof options === 'object' && options !== null) { + return options.comparisonDate; + } + return options; +} + export default function isAfter(date, options) { // For backwards compatibility: // isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date` - const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); + const comparisonDate = resolveComparisonDate(options) || Date().toString(); - const comparison = toDate(comparisonDate); + const comparison = comparisonDate instanceof Date + ? comparisonDate + : toDate(comparisonDate); const original = toDate(date); return !!(original && comparison && original > comparison); diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 977d0ad1a..aaa134abf 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,11 +1,23 @@ import toDate from './toDate'; +function resolveComparisonDate(options) { + if (options instanceof Date) { + return options; + } + if (typeof options === 'object' && options !== null) { + return options.comparisonDate; + } + return options; +} + export default function isBefore(date, options) { // For backwards compatibility: // isBefore(str [, date]), i.e. `options` could be used as argument for the legacy `date` - const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); + const comparisonDate = resolveComparisonDate(options) || Date().toString(); - const comparison = toDate(comparisonDate); + const comparison = comparisonDate instanceof Date + ? comparisonDate + : toDate(comparisonDate); const original = toDate(date); return !!(original && comparison && original < comparison); diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js index f0daf8a17..94d074bad 100644 --- a/test/validators/isAfter.test.js +++ b/test/validators/isAfter.test.js @@ -74,3 +74,22 @@ describe('isAfter', () => { }); }); }); +describe('(legacy syntax with Date object)', () => { + it('should accept a Date object as the second argument and use it as the comparison date', () => { + // Regression: previously `typeof options === 'object' && options.comparisonDate === undefined` + // meant the Date argument was silently ignored and the comparison fell back to "now". + test({ + validator: 'isAfter', + args: [new Date('2010-01-01T00:00:00Z')], + valid: ['2010-01-02', '2011-08-04', '2030-01-01', new Date(2020, 0, 1).toString()], + invalid: ['2009-12-31', '2010-01-01', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: [new Date('2030-01-01T00:00:00Z')], + valid: ['2030-01-02', '2050-06-15'], + invalid: ['2025-06-26', '2029-12-31', new Date(2025, 0, 1).toString()], + }); + }); +}); diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js index 298e5b410..1fcb88d36 100644 --- a/test/validators/isBefore.test.js +++ b/test/validators/isBefore.test.js @@ -117,3 +117,22 @@ describe('isBefore', () => { }); }); }); +describe('legacy syntax with Date object', () => { + it('should accept a Date object as the second argument and use it as the comparison date', () => { + // Regression: previously `typeof options === 'object' && options.comparisonDate === undefined` + // meant the Date argument was silently ignored and the comparison fell back to "now". + test({ + validator: 'isBefore', + args: [new Date('2010-01-01T00:00:00Z')], + valid: ['2009-12-31', '1999-12-31', new Date(0).toString()], + invalid: ['2010-01-02', '2011-08-04', '2030-01-01'], + }); + + test({ + validator: 'isBefore', + args: [new Date('2030-01-01T00:00:00Z')], + valid: ['2025-06-26', '2029-12-31', new Date(2025, 0, 1).toString()], + invalid: ['2030-01-02', '2050-06-15'], + }); + }); +});