Skip to content

Commit

Permalink
Date validations
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Dec 12, 2016
1 parent 02547f1 commit a5e9833
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ var year='[1-9][0-9]{3}';
var mon='[01]?[0-9]';
var day='((30)|(31)|([0-2]?[0-9]))';
function isDate(values) {
var sep='[/-]';
var dateRegExp = new RegExp('^(('+year+sep+mon+sep+day+')|('+day+sep+mon+sep+year+')|('+mon+sep+day+sep+year+'))$');
var sep='([/-])';
var dateRegExp = new RegExp('^(('+year+sep+mon+'\\3'+day+')|('+day+sep+mon+'\\13'+year+')|('+mon+sep+day+'\\15'+year+'))$');
return values.every(function(val) {
//var matches = dateRegExp.exec(val); console.log("matching", val, matches);
return val.match(dateRegExp);
});
}
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,16 @@ describe("datatype validation", function(){
expect(ts('2009-05-06 00:10:00.100 4:00')).to.not.be.ok();
expect(ts('2009-05-06 00:00:00 +13:60')).to.not.be.ok();
});
it("dates", function(){
var d = txtToSql.typeValidations['date'].checkOne;
// good
expect(d('2016-11-21')).to.be.ok();
expect(d('2016/11/21')).to.be.ok();
expect(d('1/29/1969')).to.be.ok();
expect(d('29/1/1969')).to.be.ok();
// bad
expect(d('2016/11-21')).to.not.be.ok();
expect(d('3-29/1969')).to.not.be.ok();
expect(d('29/3-1969')).to.not.be.ok();
});
});
5 changes: 3 additions & 2 deletions web/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ var year='[1-9][0-9]{3}';
var mon='[01]?[0-9]';
var day='((30)|(31)|([0-2]?[0-9]))';
function isDate(values) {
var sep='[/-]';
var dateRegExp = new RegExp('^(('+year+sep+mon+sep+day+')|('+day+sep+mon+sep+year+')|('+mon+sep+day+sep+year+'))$');
var sep='([/-])';
var dateRegExp = new RegExp('^(('+year+sep+mon+'\\3'+day+')|('+day+sep+mon+'\\13'+year+')|('+mon+sep+day+'\\15'+year+'))$');
return values.every(function(val) {
//var matches = dateRegExp.exec(val); console.log("matching", val, matches);
return val.match(dateRegExp);
});
}
Expand Down

0 comments on commit a5e9833

Please sign in to comment.