Skip to content

Commit

Permalink
0317/date validation adjustment (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeerickson authored Sep 4, 2019
1 parent 249b15c commit f8a1e70
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 63 deletions.
15 changes: 8 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 5 additions & 22 deletions dist/validator.js
Original file line number Diff line number Diff line change
@@ -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<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";

Expand Down Expand Up @@ -17318,22 +17318,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);
}
Expand All @@ -17344,12 +17328,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();
Expand All @@ -17373,7 +17356,7 @@ function isValidDate2(inDate) {
return false;
}

return valid;
return true;
}

var rules = {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "validatorjs",
"description": "Validation library inspired by Laravel's Validator",
"version": "3.16.1",
"version": "3.17.0",
"author": "David <[email protected]>",
"contributors": [
{
Expand Down Expand Up @@ -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"
}
}
49 changes: 43 additions & 6 deletions spec/date-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
});

Expand All @@ -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;
}
});
Expand Down
25 changes: 4 additions & 21 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -60,7 +43,7 @@ function isValidDate2(inDate) {
return false;
}

return valid;
return true;
}

var rules = {
Expand Down

0 comments on commit f8a1e70

Please sign in to comment.