Skip to content

Commit ddf9ab1

Browse files
author
Joao Madeiras
committed
Add support for numeric timestamps on DateAssert
1 parent b787895 commit ddf9ab1

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/asserts/date-assert.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ export default function dateAssert({ format } = {}) {
4646
*/
4747

4848
this.validate = value => {
49+
if (typeof value === 'number') {
50+
if (new Date(value).toString() === 'Invalid Date') {
51+
throw new Violation(this, value);
52+
}
53+
54+
return true;
55+
}
56+
4957
if (typeof value !== 'string' && Object.prototype.toString.call(value) !== '[object Date]') {
50-
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string' });
58+
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string_or_a_number' });
5159
}
5260

5361
if (isNaN(Date.parse(value)) === true) {

test/asserts/date-assert_test.js

Lines changed: 25 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 number', () => {
24+
const choices = [[], {}];
2525

2626
choices.forEach(choice => {
2727
try {
@@ -30,7 +30,7 @@ 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
});
@@ -61,6 +61,17 @@ describe('DateAssert', () => {
6161
}
6262
});
6363

64+
it('should throw an error if the input value is an invalid timestamp', () => {
65+
try {
66+
new Assert().Date().validate(-Number.MAX_VALUE);
67+
68+
should.fail();
69+
} catch (e) {
70+
e.should.be.instanceOf(Violation);
71+
e.show().assert.should.equal('Date');
72+
}
73+
});
74+
6475
it('should throw an error if value does not pass strict validation', () => {
6576
try {
6677
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
@@ -82,15 +93,23 @@ describe('DateAssert', () => {
8293
}
8394
});
8495

85-
it('should accept a `Date`', () => {
96+
it('should accept an instance of `Date`', () => {
8697
new Assert().Date().validate(new Date());
8798
});
8899

89100
it('should accept a correctly formatted date', () => {
90101
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000-12-30');
91102
});
92103

93-
it('should accept a `string`', () => {
94-
new Assert().Date().validate('2014-10-16');
104+
it('should accept a string date', () => {
105+
new Assert().Date().validate('2016-04-23');
106+
});
107+
108+
it('should accept an ISO-8601 string date', () => {
109+
new Assert().Date().validate('2016-04-23T00:51:18.570Z');
110+
});
111+
112+
it('should accept a numeric timestamp', () => {
113+
new Assert().Date().validate(Date.now());
95114
});
96115
});

0 commit comments

Comments
 (0)