Skip to content

Commit 551d520

Browse files
committed
Merge pull request #95 from seegno/feature/add-format-option-to-date-assert
Add format option to date assert
2 parents d58a900 + 02feec9 commit 551d520

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Tests if the value is a valid credit card number using the Luhn10 algorithm.
9292
### Date
9393
Tests if the value is a valid date.
9494

95+
#### Arguments
96+
- `format` (optional) - the format in which the date must be in.
97+
9598
### DateDiffGreaterThan
9699
Tests if the difference between two dates is greater than a given threshold.
97100

src/asserts/date-assert.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,43 @@
44
*/
55

66
import { Violation } from 'validator.js';
7+
import { isString } from 'lodash';
78

89
/**
910
* Export `DateAssert`.
1011
*/
1112

12-
export default function dateAssert() {
13+
export default function dateAssert({ format } = {}) {
1314
/**
1415
* Class name.
1516
*/
1617

1718
this.__class__ = 'Date';
1819

20+
/**
21+
* Optional peer dependency.
22+
*/
23+
24+
let moment;
25+
26+
/**
27+
* Validate format.
28+
*/
29+
30+
if (format) {
31+
if (!isString(format)) {
32+
throw new Error(`Unsupported format ${format} given`);
33+
}
34+
35+
moment = require('moment');
36+
}
37+
38+
/**
39+
* Format to match the input.
40+
*/
41+
42+
this.format = format;
43+
1944
/**
2045
* Validation algorithm.
2146
*/
@@ -29,6 +54,14 @@ export default function dateAssert() {
2954
throw new Violation(this, value);
3055
}
3156

57+
if (!this.format) {
58+
return true;
59+
}
60+
61+
if (!moment(value, this.format, true).isValid()) {
62+
throw new Violation(this, value);
63+
}
64+
3265
return true;
3366
};
3467

test/asserts/date-assert_test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,43 @@ describe('DateAssert', () => {
3535
});
3636
});
3737

38+
it('should throw an error if an invalid format is given', () => {
39+
const formats = [[], {}, 123];
40+
41+
formats.forEach(format => {
42+
try {
43+
new Assert().Date({ format }).validate();
44+
45+
should.fail();
46+
} catch (e) {
47+
e.should.be.instanceOf(Error);
48+
e.message.should.equal(`Unsupported format ${format} given`);
49+
}
50+
});
51+
});
52+
53+
it('should throw an error if value is not correctly formatted', () => {
54+
try {
55+
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('20003112');
56+
57+
should.fail();
58+
} catch (e) {
59+
e.should.be.instanceOf(Violation);
60+
e.show().assert.should.equal('Date');
61+
}
62+
});
63+
64+
it('should throw an error if value does not pass strict validation', () => {
65+
try {
66+
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
67+
68+
should.fail();
69+
} catch (e) {
70+
e.should.be.instanceOf(Violation);
71+
e.show().assert.should.equal('Date');
72+
}
73+
});
74+
3875
it('should expose `assert` equal to `Date`', () => {
3976
try {
4077
new Assert().Date().validate('foo');
@@ -49,6 +86,10 @@ describe('DateAssert', () => {
4986
new Assert().Date().validate(new Date());
5087
});
5188

89+
it('should accept a correctly formatted date', () => {
90+
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000-12-30');
91+
});
92+
5293
it('should accept a `string`', () => {
5394
new Assert().Date().validate('2014-10-16');
5495
});

0 commit comments

Comments
 (0)