Skip to content

Commit

Permalink
make Date validator non-parsing by default
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Jun 10, 2015
1 parent c10fa45 commit 0b36592
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
36 changes: 30 additions & 6 deletions test-src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const s = require("../");

describe("Date validator", function() {
let old = new Date("1979");
let oldString = "1979";
let older = new Date("1970");
let now = new Date();
let notADate = { dinosaur: "rawwwr" };
Expand Down Expand Up @@ -43,11 +42,6 @@ describe("Date validator", function() {
}, Error);
});

it("should accept a valid Date as a string", function() {
let schema = s.Date({ opt: false });
assert.equal(schema.validate(oldString).getTime(), old.getTime());
});

it("should reject a Date less than min", function() {
let schema = s.Date({ min: old });
assert.throws(function() {
Expand All @@ -61,4 +55,34 @@ describe("Date validator", function() {
schema.validate(now);
}, Error);
});

it("should reject non-date if parse if not true", function() {

let schema = s.Date();

assert.throws(function() {
schema.validate("Wed Jun 10 2015 20:36:48 GMT+1000 (AEST)");
});

});

it("should accept non-date if parse is true", function() {

let schema = s.Date({ parse: true });
let date = schema.validate("Wed Jun 10 2015 20:36:48 GMT+1000 (AEST)");

assert.equal(date.getMinutes(), 36);

});

it("should reject non-date compatible string if parse is true", function() {

let schema = s.Date({ parse: true });

assert.throws(function() {
schema.validate("hello");
});

});

});
5 changes: 0 additions & 5 deletions test-src/fastDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ describe("FastDate validator", function() {
}, Error);
});

it("should accept a valid Date as a string", function() {
let schema = s.FastDate();
assert.equal(schema.validate(oldString).getTime(), old.getTime());
});

});
12 changes: 10 additions & 2 deletions validators-src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DELETEKEY = require("../lib/deleteKey");

function dateValidator(args, childValidators, data) {

args = merge(args, { min: null, max: null });
args = merge(args, { min: null, max: null, parse: false });

if(data === undefined) {
if(args.opt) {
Expand All @@ -15,18 +15,26 @@ function dateValidator(args, childValidators, data) {
throw new Error("required Date");
}

if(Object.prototype.toString.call(data) !== '[object Date]' && args.parse !== true) {
throw new Error("required date, received (" + typeof data + ") " + data.toString());
}

let d = new Date(data);

if(!(d instanceof Date) || isNaN(d.getTime())) {
if(isNaN(d.getTime())) {
throw new Error("required date or Date compatible string, received (" + typeof data + ") " + data.toString());
}

if(args.min && d.getTime() < new Date(args.min).getTime()) {
throw new Error("must be greater than "+new Date(args.min));
}

if(args.max && d.getTime() > new Date(args.max).getTime()) {
throw new Error("must be less than or equal to "+new Date(args.max));
}

return d;

}

export default {
Expand Down
8 changes: 3 additions & 5 deletions validators-src/fastDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

function fastDateValidator(args, childValidators, data) {

let d = new Date(data);

if(!(d instanceof Date) || isNaN(d.getTime())) {
throw new Error("required date or Date compatible string, received (" + typeof data + ") " + data.toString());
if(Object.prototype.toString.call(data) !== '[object Date]') {
throw new Error("required date, received (" + typeof data + ") " + data.toString());
}

return d;
return data;

}

Expand Down

0 comments on commit 0b36592

Please sign in to comment.