File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -39,5 +39,17 @@ describe('MinDate', () => {
39
39
control = new FormControl ( '2016-09-10' ) ;
40
40
expect ( minDate ( '2016-09-09' ) ( control ) ) . toBeNull ( ) ;
41
41
} ) ;
42
+
43
+ it ( 'Date object year-month-day should equal to "null"' , ( ) => {
44
+ const obj = { year : 2018 , month : 10 , day : 11 } ;
45
+ control = new FormControl ( obj ) ;
46
+ expect ( minDate ( '2017-09-01' ) ( control ) ) . toBeNull ( ) ;
47
+ } ) ;
48
+
49
+ it ( 'Date object year-month-day should equal to "{minDate: true}"' , ( ) => {
50
+ const obj = { year : 2017 , month : 10 , day : 11 } ;
51
+ control = new FormControl ( obj ) ;
52
+ expect ( minDate ( '2017-11-01' ) ( control ) ) . toEqual ( { minDate : true } ) ;
53
+ } ) ;
42
54
} ) ;
43
55
Original file line number Diff line number Diff line change 1
1
import { AbstractControl , Validators , ValidatorFn } from '@angular/forms' ;
2
2
3
- import { isPresent , isDate } from '../util/lang' ;
3
+ import { isPresent , isDate , parseDate } from '../util/lang' ;
4
4
5
5
export const minDate = ( minDate : any ) : ValidatorFn => {
6
6
7
+ minDate = parseDate ( minDate ) ;
8
+
7
9
if ( ! isDate ( minDate ) && ! ( minDate instanceof Function ) ) {
8
10
throw Error ( 'minDate value must be or return a formatted date' ) ;
9
11
}
Original file line number Diff line number Diff line change @@ -4,4 +4,11 @@ export function isPresent(obj: any): boolean {
4
4
5
5
export function isDate ( obj : any ) : boolean {
6
6
return ! / I n v a l i d | N a N / . test ( new Date ( obj ) . toString ( ) ) ;
7
+ }
8
+
9
+ export function parseDate ( obj : any ) : string {
10
+ if ( typeof obj === 'object' ) {
11
+ return obj . year + '-' + obj . month + '-' + obj . day ;
12
+ }
13
+ return obj ;
7
14
}
You can’t perform that action at this time.
0 commit comments