Skip to content

Commit 3c247f3

Browse files
Joao MadeirasRui Marinho
authored andcommitted
Add support for numeric timestamps on DateAssert
1 parent d58a900 commit 3c247f3

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/asserts/date-assert.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ export default function dateAssert() {
2121
*/
2222

2323
this.validate = value => {
24-
if (typeof value !== 'string' && Object.prototype.toString.call(value) !== '[object Date]') {
25-
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string' });
24+
if (typeof value === 'string') {
25+
if (isNaN(Date.parse(value))) {
26+
throw new Violation(this, value);
27+
}
28+
29+
return true;
30+
}
31+
32+
if (typeof value === 'number') {
33+
if (isNaN(new Date(value)) || new Date(value).getTime() < 0) {
34+
throw new Violation(this, value);
35+
}
36+
37+
return true;
2638
}
2739

28-
if (isNaN(Date.parse(value)) === true) {
29-
throw new Violation(this, value);
40+
if (!(value instanceof Date) && Object.prototype.toString.call(value) !== '[object Date]') {
41+
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string_or_a_number' });
3042
}
3143

3244
return true;

test/asserts/date-assert_test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const Assert = BaseAssert.extend({
2020
*/
2121

2222
describe('DateAssert', () => {
23-
it('should throw an error if the input value is not a string or a date', () => {
24-
const choices = [[], {}, 123];
23+
it('should throw an error if the input value is not a date or a string or a timestamp', () => {
24+
const choices = [[], {}];
2525

2626
choices.forEach(choice => {
2727
try {
@@ -30,11 +30,21 @@ describe('DateAssert', () => {
3030
should.fail();
3131
} catch (e) {
3232
e.should.be.instanceOf(Violation);
33-
e.violation.value.should.equal('must_be_a_date_or_a_string');
33+
e.violation.value.should.equal('must_be_a_date_or_a_string_or_a_number');
3434
}
3535
});
3636
});
3737

38+
it('should throw an error if the input value is an invalid timestamp', () => {
39+
try {
40+
new Assert().Date().validate(-Number.MAX_VALUE);
41+
42+
should.fail();
43+
} catch (e) {
44+
e.should.be.instanceOf(Violation);
45+
}
46+
});
47+
3848
it('should expose `assert` equal to `Date`', () => {
3949
try {
4050
new Assert().Date().validate('foo');
@@ -45,11 +55,19 @@ describe('DateAssert', () => {
4555
}
4656
});
4757

48-
it('should accept a `Date`', () => {
58+
it('should accept an instance of `Date`', () => {
4959
new Assert().Date().validate(new Date());
5060
});
5161

52-
it('should accept a `string`', () => {
53-
new Assert().Date().validate('2014-10-16');
62+
it('should accept a string date', () => {
63+
new Assert().Date().validate('2016-04-23');
64+
});
65+
66+
it('should accept an ISO-8601 string date', () => {
67+
new Assert().Date().validate('2016-04-23T00:51:18.570Z');
68+
});
69+
70+
it('should accept a numeric timestamp', () => {
71+
new Assert().Date().validate(Date.now());
5472
});
5573
});

0 commit comments

Comments
 (0)