Skip to content

Commit 1117558

Browse files
Joao MadeirasFrancisco Cardoso
authored andcommitted
Add support for numeric timestamps on NullOrDateAssert
1 parent ddf9ab1 commit 1117558

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The following set of extra asserts are provided by this package:
4141
- [Iso3166Country](#iso3166country) (requires `isoc`)
4242
- [Json](#json)
4343
- [NotEmpty](#notempty)
44-
- [NullOrDate](#nullordate)
44+
- [NullOrDate](#nullordate) (requires `moment` for format validation only)
4545
- [NullOrString](#nullorstring)
4646
- [Phone](#phone) (requires `google-libphonenumber`)
4747
- [PlainObject](#plainobject)
@@ -152,6 +152,9 @@ Tests if the value is valid json.
152152
### NotEmpty
153153
Tests if the value is not an empty (empty object, empty array, empty string, etc).
154154

155+
### NullOrDate
156+
Tests if the value is a `null` or `date`.
157+
155158
### NullOrString
156159
Tests if the value is a `null` or `string`, optionally within some boundaries.
157160

src/asserts/null-or-date-assert.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@
33
* Module dependencies.
44
*/
55

6-
import { Violation } from 'validator.js';
6+
import DateAssert from './date-assert';
7+
import { Assert as BaseAssert, Violation } from 'validator.js';
8+
9+
/**
10+
* Extend Assert with `DateAssert`.
11+
*/
12+
13+
const Assert = BaseAssert.extend({ Date: DateAssert });
714

815
/**
916
* Export `NullOrDateAssert`.
1017
*/
1118

12-
export default function nullOrDateAssert() {
19+
export default function nullOrDateAssert({ format } = {}) {
1320
/**
1421
* Class name.
1522
*/
1623

1724
this.__class__ = 'NullOrDate';
1825

26+
/**
27+
* Format to match the input.
28+
*/
29+
30+
this.format = format;
31+
1932
/**
2033
* Validation algorithm.
2134
*/
2235

2336
this.validate = value => {
24-
if (typeof value !== 'string' && value !== null && Object.prototype.toString.call(value) !== '[object Date]') {
25-
throw new Violation(this, value, { value: 'must_be_null_or_a_date' });
26-
}
27-
2837
if (value === null) {
2938
return true;
3039
}
3140

32-
if (isNaN(Date.parse(value)) === true) {
33-
throw new Violation(this, value);
41+
try {
42+
new Assert().Date({ format }).validate(value);
43+
} catch (e) {
44+
throw new Violation(this, value, { value: 'must_be_null_or_a_date_or_a_number' });
3445
}
3546

3647
return true;

test/asserts/null-or-date-assert_test.js

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

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

2626
choices.forEach(choice => {
2727
try {
@@ -30,7 +30,7 @@ describe('NullOrDateAssert', () => {
3030
should.fail();
3131
} catch (e) {
3232
e.should.be.instanceOf(Violation);
33-
e.violation.value.should.equal('must_be_null_or_a_date');
33+
e.violation.value.should.equal('must_be_null_or_a_date_or_a_number');
3434
}
3535
});
3636
});
@@ -46,6 +46,28 @@ describe('NullOrDateAssert', () => {
4646
}
4747
});
4848

49+
it('should throw an error if the input value is an invalid timestamp', () => {
50+
try {
51+
new Assert().NullOrDate().validate(-Number.MAX_VALUE);
52+
53+
should.fail();
54+
} catch (e) {
55+
e.should.be.instanceOf(Violation);
56+
e.show().assert.should.equal('NullOrDate');
57+
}
58+
});
59+
60+
it('should throw an error if value does not pass strict validation', () => {
61+
try {
62+
new Assert().NullOrDate({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
63+
64+
should.fail();
65+
} catch (e) {
66+
e.should.be.instanceOf(Violation);
67+
e.show().assert.should.equal('NullOrDate');
68+
}
69+
});
70+
4971
it('should expose `assert` equal to `NullOrDate`', () => {
5072
try {
5173
new Assert().NullOrDate().validate({});
@@ -64,7 +86,19 @@ describe('NullOrDateAssert', () => {
6486
new Assert().NullOrDate().validate(new Date());
6587
});
6688

89+
it('should accept a correctly formatted date', () => {
90+
new Assert().NullOrDate({ format: 'YYYY' }).validate('2000');
91+
});
92+
6793
it('should accept a string', () => {
6894
new Assert().NullOrDate().validate('2014-10-16');
6995
});
96+
97+
it('should accept an ISO-8601 string date', () => {
98+
new Assert().NullOrDate().validate('2016-04-23T00:51:18.570Z');
99+
});
100+
101+
it('should accept a numeric timestamp', () => {
102+
new Assert().NullOrDate().validate(Date.now());
103+
});
70104
});

0 commit comments

Comments
 (0)