From f8a1e70601c2f35b1143d131d785c00efeb3c5fb Mon Sep 17 00:00:00 2001 From: Mike Erickson Date: Wed, 4 Sep 2019 11:04:44 -0700 Subject: [PATCH] 0317/date validation adjustment (#344) --- .gitignore | 15 ++++++++------- LICENSE | 2 +- dist/validator.js | 27 +++++--------------------- package-lock.json | 2 +- package.json | 10 +++++----- spec/date-rule.js | 49 +++++++++++++++++++++++++++++++++++++++++------ src/rules.js | 25 ++++-------------------- 7 files changed, 67 insertions(+), 63 deletions(-) diff --git a/.gitignore b/.gitignore index ebfca399..030fbe35 100755 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,11 @@ .DS_Store +.idea/ .npm-debug.log -node_modules -spec/checklist.md -coverage +.vscode/ +coverage/ +node_modules/ +NOTES.md npm-debug.log -.idea -.vscode -sandbox -notes.md +RELEASE.md +sandbox/ +spec/checklist.md \ No newline at end of file diff --git a/LICENSE b/LICENSE index b26853c6..d7bd48d3 100755 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2015 David Tang +Copyright (c) 2012-2019 David Tang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/dist/validator.js b/dist/validator.js index 9b855b56..63f1b5e5 100644 --- a/dist/validator.js +++ b/dist/validator.js @@ -1,4 +1,4 @@ -/*! validatorjs - v3.16.1 - - 2019-09-04 */ +/*! validatorjs - v3.17.0 - - 2019-09-04 */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Validator = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i 0 && pos <= 6) { inDate = inDate.replace(/\./g, '-'); } + if (inDate.length === 10) { + return dateTools.isValid(dateTools.parseISO(inDate)); + } } - // TODO: This approach is not going to work as Date conversion will always be accurate - // For example: 2019-02-31 will return as 2019-03-03 thus calculations will always be correct - // Need to devise another method of check validity - var testDate = new Date(inDate); var yr = testDate.getFullYear(); var mo = testDate.getMonth(); @@ -17373,7 +17356,7 @@ function isValidDate2(inDate) { return false; } - return valid; + return true; } var rules = { diff --git a/package-lock.json b/package-lock.json index d082e867..a6c206a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "validatorjs", - "version": "3.16.1", + "version": "3.16.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ce9482e0..fa88c960 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validatorjs", "description": "Validation library inspired by Laravel's Validator", - "version": "3.16.1", + "version": "3.17.0", "author": "David ", "contributors": [ { @@ -70,9 +70,9 @@ "lint": "eslint \"./src/**/*.js\"", "lint:spec": "eslint \"./spec/**/*.js\" --rule 'no-redeclare: off'", "lint:fix": "eslint \"./src/**/*.js\" --fix", - "test-node": "node node_modules/mocha/bin/mocha spec --slow 500 --reporter mocha-better-spec-reporter", - "test-node:watch": "node node_modules/mocha/bin/mocha --watch spec --slow 500 --reporter mocha-better-spec-reporter", - "test-browser": "grunt dist && node node_modules/karma/bin/karma start --single-run", - "test": "npm run test-node && npm run test-browser" + "test:node": "node node_modules/mocha/bin/mocha spec --slow 500 --reporter mocha-better-spec-reporter", + "test:node-watch": "node node_modules/mocha/bin/mocha --watch spec --slow 500 --reporter mocha-better-spec-reporter", + "test:browser": "grunt dist && node node_modules/karma/bin/karma start --single-run", + "test": "npm run test:node && npm run test:browser" } } diff --git a/spec/date-rule.js b/spec/date-rule.js index 02b16d4a..9c4832c9 100755 --- a/spec/date-rule.js +++ b/spec/date-rule.js @@ -69,12 +69,48 @@ describe('date rule', function() { expect(validator.fails()).to.be.true; }); - it('should use custom "isValidDate" rule', () => { - /** - * NOTE - * this test case will only be executed via node as we will be extending to use the`date-fns` package - */ + it('should properly check invalid dates', () => { + let invalidDates = [ + '2019-01-32', + '2019-02-31', + '2019-03-32', + '2019-04-31', + '2019-05-32', + '2019-06-31', + '2019-07-32', + '2019-08-32', + '2019-09-31', + '2019-10-32', + '2019-11-31', + '2019-12-32' + ]; + invalidDates.forEach(dateValue => { + validator = new Validator({ failDate: dateValue }, { failDate: 'date' }); + expect(validator.passes()).to.be.false; + }); + + let validDates = [ + '2019-01-31', + '2019-02-28', + '2019-03-31', + '2019-04-30', + '2019-05-31', + '2019-06-30', + '2019-07-31', + '2019-08-31', + '2019-09-30', + '2019-10-31', + '2019-11-30', + '2019-12-31' + ]; + validDates.forEach(dateValue => { + validator = new Validator({ failDate: dateValue }, { failDate: 'date' }); + expect(validator.passes()).to.be.true; + }); + }); + it('should use custom "isValidDate" rule', () => { + // NOTE: This test should only be used when running with node as it using `date-fns` node module if (typeof require !== 'undefined') { Validator.register( 'isValidDate', @@ -101,7 +137,7 @@ describe('date rule', function() { '2019-12-32' ]; invalidDates.forEach(dateValue => { - validator = new Validator({ failDate: dateValue }, { failDate: 'isValidDate' }); + validator = new Validator({ failDate: dateValue }, { failDate: 'date' }); expect(validator.passes()).to.be.false; }); @@ -124,6 +160,7 @@ describe('date rule', function() { expect(validator.passes()).to.be.true; }); } else { + // if running in browser, always pass expect(true).to.be.true; } }); diff --git a/src/rules.js b/src/rules.js index b7ddf222..0ed897ae 100755 --- a/src/rules.js +++ b/src/rules.js @@ -5,22 +5,6 @@ function leapYear(year) { } function isValidDate(inDate) { - let test = inDate; - if (typeof inDate === 'string') { - let dateParsed = dateTools.parseISO(inDate); - if (typeof dateParsed === 'object') { - if (dateParsed.toString() === 'Invalid Date') { - return isValidDate2(inDate); - } - } - return dateTools.isValid(dateParsed); - } - return isValidDate2(inDate); -} - -function isValidDate2(inDate) { - var valid = true; - if (inDate instanceof Date) { return !isNaN(inDate); } @@ -31,12 +15,11 @@ function isValidDate2(inDate) { if (pos > 0 && pos <= 6) { inDate = inDate.replace(/\./g, '-'); } + if (inDate.length === 10) { + return dateTools.isValid(dateTools.parseISO(inDate)); + } } - // TODO: This approach is not going to work as Date conversion will always be accurate - // For example: 2019-02-31 will return as 2019-03-03 thus calculations will always be correct - // Need to devise another method of check validity - var testDate = new Date(inDate); var yr = testDate.getFullYear(); var mo = testDate.getMonth(); @@ -60,7 +43,7 @@ function isValidDate2(inDate) { return false; } - return valid; + return true; } var rules = {