@@ -85,6 +85,21 @@ const _getColTypeFromTypeArray = (typeArray) => {
8585 return 'string' ; // Fall back to default type
8686} ;
8787
88+ const validateDateFormat = ( value , options = { } ) => {
89+ if ( typeof value !== 'string' ) return false ;
90+ const { dateFormat = 'yyyy-MM-dd' } = options ;
91+
92+ const formatRegex = dateFormat
93+ . replace ( / y y y y / , '\\d{4}' )
94+ . replace ( / M M / , '(0[1-9]|1[0-2])' )
95+ . replace ( / d d / , '(0[1-9]|[12]\\d|3[01])' )
96+ . replace ( / \/ / g, '\\/' )
97+ . replace ( / - / g, '-' ) ;
98+
99+ const regex = new RegExp ( `^${ formatRegex } $` ) ;
100+ return regex . test ( value ) ;
101+ } ;
102+
88103const _validateFormat = ( rows , hasHeader , cols , options ) => {
89104 const colsData = cols . map ( ( col ) => ( { ...col , type : _getColTypeFromTypeArray ( col . type ) } ) ) ;
90105 const errors = [ ] ;
@@ -116,6 +131,15 @@ const _validateFormat = (rows, hasHeader, cols, options) => {
116131 const value = row [ colIndex ] ;
117132 if ( value === undefined ) continue ;
118133
134+ if ( type === 'date' && ! validateDateFormat ( value , colOptions ) ) {
135+ const expectedFormat = colOptions . dateFormat || 'dd/MM/yyyy' ;
136+ const errorSummary = 'Incorrect date value' ;
137+ const errorContext = `Incorrect value: "${ value } " for type date\nExpected format: ${ expectedFormat } ` ;
138+ const errorLoc = `Line ${ rowIndex + 1 } , Column ${ colIndex + 1 } ("${ field } ")` ;
139+ errors . push ( new PanelError ( errorSummary , errorLoc , errorContext ) ) ;
140+ continue ;
141+ }
142+
119143 const validationResult = ValidationUtils . isValid ( value , type , colOptions , acceptsEmptyFields ) ;
120144 if ( validationResult !== true ) {
121145 const { summary : errorSummary , context : errorContext } = validationResult ;
0 commit comments