File tree Expand file tree Collapse file tree 2 files changed +24
-8
lines changed Expand file tree Collapse file tree 2 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -21,12 +21,24 @@ export default function dateAssert() {
21
21
*/
22
22
23
23
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 ) ) === true ) {
26
+ throw new Violation ( this , value ) ;
27
+ }
28
+
29
+ return true ;
30
+ }
31
+
32
+ if ( typeof value === 'number' ) {
33
+ if ( new Date ( value ) . getTime ( ) < 0 ) {
34
+ throw new Violation ( this , value ) ;
35
+ }
36
+
37
+ return true ;
26
38
}
27
39
28
- if ( isNaN ( Date . parse ( value ) ) === true ) {
29
- throw new Violation ( this , value ) ;
40
+ if ( Object . prototype . toString . call ( value ) !== '[object Date]' ) {
41
+ throw new Violation ( this , value , { value : 'must_be_a_date_or_a_string_or_a_timestamp' } ) ;
30
42
}
31
43
32
44
return true ;
Original file line number Diff line number Diff line change @@ -19,9 +19,9 @@ const Assert = BaseAssert.extend({
19
19
* Test `DateAssert`.
20
20
*/
21
21
22
- describe ( 'DateAssert' , ( ) => {
23
- it ( 'should throw an error if the input value is not a string or a date ' , ( ) => {
24
- const choices = [ [ ] , { } , 123 ] ;
22
+ describe . only ( 'DateAssert' , ( ) => {
23
+ it ( 'should throw an error if the input value is not a date or a string or a timestamp ' , ( ) => {
24
+ const choices = [ [ ] , { } ] ;
25
25
26
26
choices . forEach ( choice => {
27
27
try {
@@ -30,7 +30,7 @@ describe('DateAssert', () => {
30
30
should . fail ( ) ;
31
31
} catch ( e ) {
32
32
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_timestamp ' ) ;
34
34
}
35
35
} ) ;
36
36
} ) ;
@@ -52,4 +52,8 @@ describe('DateAssert', () => {
52
52
it ( 'should accept a `string`' , ( ) => {
53
53
new Assert ( ) . Date ( ) . validate ( '2014-10-16' ) ;
54
54
} ) ;
55
+
56
+ it ( 'should accept a `timestamp`' , ( ) => {
57
+ new Assert ( ) . Date ( ) . validate ( Date . now ( ) ) ;
58
+ } ) ;
55
59
} ) ;
You can’t perform that action at this time.
0 commit comments