Skip to content

Commit f8a1e70

Browse files
authored
0317/date validation adjustment (#344)
1 parent 249b15c commit f8a1e70

7 files changed

Lines changed: 67 additions & 63 deletions

File tree

.gitignore

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
.DS_Store
2+
.idea/
23
.npm-debug.log
3-
node_modules
4-
spec/checklist.md
5-
coverage
4+
.vscode/
5+
coverage/
6+
node_modules/
7+
NOTES.md
68
npm-debug.log
7-
.idea
8-
.vscode
9-
sandbox
10-
notes.md
9+
RELEASE.md
10+
sandbox/
11+
spec/checklist.md

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2012-2015 David Tang
3+
Copyright (c) 2012-2019 David Tang
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

dist/validator.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! validatorjs - v3.16.1 - - 2019-09-04 */
1+
/*! validatorjs - v3.17.0 - - 2019-09-04 */
22
(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<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
33
"use strict";
44

@@ -17318,22 +17318,6 @@ function leapYear(year) {
1731817318
}
1731917319

1732017320
function isValidDate(inDate) {
17321-
let test = inDate;
17322-
if (typeof inDate === 'string') {
17323-
let dateParsed = dateTools.parseISO(inDate);
17324-
if (typeof dateParsed === 'object') {
17325-
if (dateParsed.toString() === 'Invalid Date') {
17326-
return isValidDate2(inDate);
17327-
}
17328-
}
17329-
return dateTools.isValid(dateParsed);
17330-
}
17331-
return isValidDate2(inDate);
17332-
}
17333-
17334-
function isValidDate2(inDate) {
17335-
var valid = true;
17336-
1733717321
if (inDate instanceof Date) {
1733817322
return !isNaN(inDate);
1733917323
}
@@ -17344,12 +17328,11 @@ function isValidDate2(inDate) {
1734417328
if (pos > 0 && pos <= 6) {
1734517329
inDate = inDate.replace(/\./g, '-');
1734617330
}
17331+
if (inDate.length === 10) {
17332+
return dateTools.isValid(dateTools.parseISO(inDate));
17333+
}
1734717334
}
1734817335

17349-
// TODO: This approach is not going to work as Date conversion will always be accurate
17350-
// For example: 2019-02-31 will return as 2019-03-03 thus calculations will always be correct
17351-
// Need to devise another method of check validity
17352-
1735317336
var testDate = new Date(inDate);
1735417337
var yr = testDate.getFullYear();
1735517338
var mo = testDate.getMonth();
@@ -17373,7 +17356,7 @@ function isValidDate2(inDate) {
1737317356
return false;
1737417357
}
1737517358

17376-
return valid;
17359+
return true;
1737717360
}
1737817361

1737917362
var rules = {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "validatorjs",
33
"description": "Validation library inspired by Laravel's Validator",
4-
"version": "3.16.1",
4+
"version": "3.17.0",
55
"author": "David <[email protected]>",
66
"contributors": [
77
{
@@ -70,9 +70,9 @@
7070
"lint": "eslint \"./src/**/*.js\"",
7171
"lint:spec": "eslint \"./spec/**/*.js\" --rule 'no-redeclare: off'",
7272
"lint:fix": "eslint \"./src/**/*.js\" --fix",
73-
"test-node": "node node_modules/mocha/bin/mocha spec --slow 500 --reporter mocha-better-spec-reporter",
74-
"test-node:watch": "node node_modules/mocha/bin/mocha --watch spec --slow 500 --reporter mocha-better-spec-reporter",
75-
"test-browser": "grunt dist && node node_modules/karma/bin/karma start --single-run",
76-
"test": "npm run test-node && npm run test-browser"
73+
"test:node": "node node_modules/mocha/bin/mocha spec --slow 500 --reporter mocha-better-spec-reporter",
74+
"test:node-watch": "node node_modules/mocha/bin/mocha --watch spec --slow 500 --reporter mocha-better-spec-reporter",
75+
"test:browser": "grunt dist && node node_modules/karma/bin/karma start --single-run",
76+
"test": "npm run test:node && npm run test:browser"
7777
}
7878
}

spec/date-rule.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,48 @@ describe('date rule', function() {
6969
expect(validator.fails()).to.be.true;
7070
});
7171

72-
it('should use custom "isValidDate" rule', () => {
73-
/**
74-
* NOTE
75-
* this test case will only be executed via node as we will be extending to use the`date-fns` package
76-
*/
72+
it('should properly check invalid dates', () => {
73+
let invalidDates = [
74+
'2019-01-32',
75+
'2019-02-31',
76+
'2019-03-32',
77+
'2019-04-31',
78+
'2019-05-32',
79+
'2019-06-31',
80+
'2019-07-32',
81+
'2019-08-32',
82+
'2019-09-31',
83+
'2019-10-32',
84+
'2019-11-31',
85+
'2019-12-32'
86+
];
87+
invalidDates.forEach(dateValue => {
88+
validator = new Validator({ failDate: dateValue }, { failDate: 'date' });
89+
expect(validator.passes()).to.be.false;
90+
});
91+
92+
let validDates = [
93+
'2019-01-31',
94+
'2019-02-28',
95+
'2019-03-31',
96+
'2019-04-30',
97+
'2019-05-31',
98+
'2019-06-30',
99+
'2019-07-31',
100+
'2019-08-31',
101+
'2019-09-30',
102+
'2019-10-31',
103+
'2019-11-30',
104+
'2019-12-31'
105+
];
106+
validDates.forEach(dateValue => {
107+
validator = new Validator({ failDate: dateValue }, { failDate: 'date' });
108+
expect(validator.passes()).to.be.true;
109+
});
110+
});
77111

112+
it('should use custom "isValidDate" rule', () => {
113+
// NOTE: This test should only be used when running with node as it using `date-fns` node module
78114
if (typeof require !== 'undefined') {
79115
Validator.register(
80116
'isValidDate',
@@ -101,7 +137,7 @@ describe('date rule', function() {
101137
'2019-12-32'
102138
];
103139
invalidDates.forEach(dateValue => {
104-
validator = new Validator({ failDate: dateValue }, { failDate: 'isValidDate' });
140+
validator = new Validator({ failDate: dateValue }, { failDate: 'date' });
105141
expect(validator.passes()).to.be.false;
106142
});
107143

@@ -124,6 +160,7 @@ describe('date rule', function() {
124160
expect(validator.passes()).to.be.true;
125161
});
126162
} else {
163+
// if running in browser, always pass
127164
expect(true).to.be.true;
128165
}
129166
});

src/rules.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ function leapYear(year) {
55
}
66

77
function isValidDate(inDate) {
8-
let test = inDate;
9-
if (typeof inDate === 'string') {
10-
let dateParsed = dateTools.parseISO(inDate);
11-
if (typeof dateParsed === 'object') {
12-
if (dateParsed.toString() === 'Invalid Date') {
13-
return isValidDate2(inDate);
14-
}
15-
}
16-
return dateTools.isValid(dateParsed);
17-
}
18-
return isValidDate2(inDate);
19-
}
20-
21-
function isValidDate2(inDate) {
22-
var valid = true;
23-
248
if (inDate instanceof Date) {
259
return !isNaN(inDate);
2610
}
@@ -31,12 +15,11 @@ function isValidDate2(inDate) {
3115
if (pos > 0 && pos <= 6) {
3216
inDate = inDate.replace(/\./g, '-');
3317
}
18+
if (inDate.length === 10) {
19+
return dateTools.isValid(dateTools.parseISO(inDate));
20+
}
3421
}
3522

36-
// TODO: This approach is not going to work as Date conversion will always be accurate
37-
// For example: 2019-02-31 will return as 2019-03-03 thus calculations will always be correct
38-
// Need to devise another method of check validity
39-
4023
var testDate = new Date(inDate);
4124
var yr = testDate.getFullYear();
4225
var mo = testDate.getMonth();
@@ -60,7 +43,7 @@ function isValidDate2(inDate) {
6043
return false;
6144
}
6245

63-
return valid;
46+
return true;
6447
}
6548

6649
var rules = {

0 commit comments

Comments
 (0)