@@ -53,22 +53,32 @@ export default function dateDiffLessThanAssert(threshold, options) {
5353 */
5454
5555 this . validate = value => {
56- if ( typeof value !== 'string' && Object . prototype . toString . call ( value ) !== '[object Date]' ) {
57- throw new Violation ( this , value , { value : 'must_be_a_date_or_a_string' } ) ;
56+ let date = value ;
57+
58+ if ( typeof date === 'number' ) {
59+ date = new Date ( date ) ;
60+
61+ if ( date . toString ( ) === 'Invalid Date' ) {
62+ throw new Violation ( this , date ) ;
63+ }
64+ }
65+
66+ if ( typeof date !== 'string' && Object . prototype . toString . call ( date ) !== '[object Date]' ) {
67+ throw new Violation ( this , date , { value : 'must_be_a_date_or_a_string_or_a_number' } ) ;
5868 }
5969
60- if ( isNaN ( Date . parse ( value ) ) === true ) {
61- throw new Violation ( this , value , { absolute : this . absolute , asFloat : this . asFloat , fromDate : this . fromDate , threshold : this . threshold , unit : this . unit } ) ;
70+ if ( isNaN ( Date . parse ( date ) ) === true ) {
71+ throw new Violation ( this , date , { absolute : this . absolute , asFloat : this . asFloat , fromDate : this . fromDate , threshold : this . threshold , unit : this . unit } ) ;
6272 }
6373
64- let diff = moment ( this . fromDate || Date . now ( ) ) . diff ( value , this . unit , this . asFloat ) ;
74+ let diff = moment ( this . fromDate || Date . now ( ) ) . diff ( date , this . unit , this . asFloat ) ;
6575
6676 if ( this . absolute ) {
6777 diff = Math . abs ( diff ) ;
6878 }
6979
7080 if ( diff >= this . threshold ) {
71- throw new Violation ( this , value , { absolute : this . absolute , asFloat : this . asFloat , diff, fromDate : this . fromDate , threshold : this . threshold , unit : this . unit } ) ;
81+ throw new Violation ( this , date , { absolute : this . absolute , asFloat : this . asFloat , diff, fromDate : this . fromDate , threshold : this . threshold , unit : this . unit } ) ;
7282 }
7383
7484 return true ;
0 commit comments