1
+ import { isObject } from 'tsfun' ;
1
2
import { parseDate } from '../../tools/parse-date' ;
2
3
import { formatDate } from '../../tools/format-date' ;
4
+ import { Field } from '../configuration/field' ;
5
+ import { DateConfiguration } from '../configuration/date-configuration' ;
3
6
4
7
5
8
/**
@@ -14,13 +17,33 @@ export interface DateSpecification {
14
17
15
18
export module DateSpecification {
16
19
20
+ export function validate ( date : DateSpecification , field : Field ) : boolean {
21
+
22
+ if ( ! isObject ( date ) || ! validateDateValue ( date . value , field ) ) return false ;
23
+
24
+ if ( field . dateConfiguration ?. inputMode === DateConfiguration . InputMode . SINGLE && date . endValue !== undefined ) {
25
+ return false ;
26
+ }
27
+
28
+ if ( field . dateConfiguration ?. inputMode === DateConfiguration . InputMode . RANGE
29
+ && ! validateDateValue ( date . endValue , field ) ) {
30
+ return false ;
31
+ }
32
+
33
+ return true ;
34
+ }
35
+
36
+
17
37
export function generateLabel ( date : DateSpecification , timezone : string , timeSuffix : string ,
18
38
locale : string ) : string {
19
39
20
- let result : string = getValueLabel ( date . value , timezone , timeSuffix , locale , ! date . endValue ) ;
21
- if ( date . endValue ) result += ' - ' + getValueLabel ( date . endValue , timezone , timeSuffix , locale , true ) ;
22
-
23
- return result ;
40
+ try {
41
+ let result : string = getValueLabel ( date . value , timezone , timeSuffix , locale , ! date . endValue ) ;
42
+ if ( date . endValue ) result += ' - ' + getValueLabel ( date . endValue , timezone , timeSuffix , locale , true ) ;
43
+ return result ;
44
+ } catch ( _ ) {
45
+ return null ;
46
+ }
24
47
}
25
48
26
49
@@ -37,4 +60,20 @@ export module DateSpecification {
37
60
38
61
return formattedDate ;
39
62
}
63
+
64
+
65
+ function validateDateValue ( dateValue : string , field : Field ) {
66
+
67
+ if ( ! dateValue || isNaN ( parseDate ( dateValue ) ?. getTime ( ) ) ) return false ;
68
+
69
+ if ( field . dateConfiguration ?. dataType === DateConfiguration . DataType . DATE_TIME && ! dateValue . includes ( ':' ) ) {
70
+ return false ;
71
+ }
72
+
73
+ if ( field . dateConfiguration ?. dataType === DateConfiguration . DataType . DATE && dateValue . includes ( ':' ) ) {
74
+ return false ;
75
+ }
76
+
77
+ return true ;
78
+ }
40
79
}
0 commit comments