]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix day-of-week validation (issue #4112)
authorMike Paras <mikeparas@gmail.com>
Sat, 19 Aug 2017 01:51:41 +0000 (18:51 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Oct 2017 22:51:35 +0000 (01:51 +0300)
* Check that day-of-week matches parsed date in configFromArray
* Add tests

src/lib/create/from-array.js
src/lib/create/valid.js
src/test/moment/create.js

index 0cdf474cf3ae5359cb79877e8fca1239c37e6ace..7626c455d56b1c325b4d637d0a7955b5cb852410 100644 (file)
@@ -80,6 +80,11 @@ export function configFromArray (config) {
     if (config._nextDay) {
         config._a[HOUR] = 24;
     }
+
+    // check for mismatching day of week
+    if (config._w && typeof config._w.d !== 'undefined' && config._w.d !== config._d.getDay()) {
+        getParsingFlags(config).weekdayMismatch = true;
+    }
 }
 
 function dayOfYearFromWeekInfo(config) {
index 96b1cf615dcad122085251967bea851a4a283818..d13f12f8b896da422fc8a8c043c8afaa7fce2834 100644 (file)
@@ -14,6 +14,7 @@ export function isValid(m) {
             !flags.empty &&
             !flags.invalidMonth &&
             !flags.invalidWeekday &&
+            !flags.weekdayMismatch &&
             !flags.nullInput &&
             !flags.invalidFormat &&
             !flags.userInvalidated &&
index 3be3c3ae328140b58fda248ee156398f6ed6030d..cfd4f57604e88a5ecca7e4cea1f9e58305f5bc3d 100644 (file)
@@ -1224,3 +1224,8 @@ test('k, kk', function (assert) {
     }
 });
 
+test('mismatching day-of-week and date', function (assert) {
+    assert.ok(!moment('Wed 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because the day-of-the-week is incorrect for the date');
+    assert.ok(moment('Thu 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because the day-of-the-week is correct for the date');
+});
+