@@ -85,41 +85,59 @@ const _getColTypeFromTypeArray = (typeArray) => {
8585 return 'string' ; // Fall back to default type
8686} ;
8787
88+ const fastIsDate = ( value ) => typeof value === 'string' && / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( value ) ;
89+
8890const _validateFormat = ( rows , hasHeader , cols , options ) => {
89- const colsData = cols . map ( ( col ) => ( { ...col , type : _getColTypeFromTypeArray ( col . type ) } ) ) ;
9091 const errors = [ ] ;
91- const knownColsCount = colsData . length ;
92+ const knownColsCount = cols . length ;
9293 const startIndex = hasHeader ? 1 : 0 ;
94+
95+ const colMeta = cols . map ( ( col ) => ( {
96+ field : col . field ,
97+ type : _getColTypeFromTypeArray ( col . type ) ,
98+ acceptsEmptyFields : col . acceptsEmptyFields ?? col . cellEditorParams ?. acceptsEmptyFields ?? false ,
99+ colOptions : {
100+ ...options ,
101+ enumValues : col . enumValues ?? col . cellEditorParams ?. enumValues ,
102+ minValue : col . minValue ,
103+ maxValue : col . maxValue ,
104+ } ,
105+ } ) ) ;
106+
93107 for ( let rowIndex = startIndex ; rowIndex < rows . length ; rowIndex ++ ) {
94108 const row = rows [ rowIndex ] ;
95109 while ( row [ row . length - 1 ] === undefined && row . length > knownColsCount ) row . pop ( ) ;
96- if ( row . length !== knownColsCount || row . includes ( undefined ) )
97- _forgeColumnsCountError ( row , rowIndex + 1 , colsData , errors ) ;
98- row . forEach ( ( rowCell , colIndex ) => {
99- if ( colIndex < knownColsCount ) {
100- const colType = colsData [ colIndex ] . type ;
101- if ( colType && rowCell !== undefined ) {
102- // use of cellEditorParams is deprecated
103- const colOptions = {
104- ...options ,
105- enumValues : colsData [ colIndex ] ?. enumValues ?? colsData [ colIndex ] ?. cellEditorParams ?. enumValues ,
106- minValue : colsData [ colIndex ] ?. minValue ,
107- maxValue : colsData [ colIndex ] ?. maxValue ,
108- } ;
109- const acceptsEmptyFields =
110- // use of cellEditorParams is deprecated
111- colsData [ colIndex ] . acceptsEmptyFields ?? colsData [ colIndex ] . cellEditorParams ?. acceptsEmptyFields ?? false ;
112- const validationResult = ValidationUtils . isValid ( rowCell , colType , colOptions , acceptsEmptyFields ) ;
113- if ( validationResult !== true ) {
114- const { summary : errorSummary , context : errorContext } = validationResult ;
115- const errorLoc = `Line ${ rowIndex + 1 } , Column ${ colIndex + 1 } ("${ colsData [ colIndex ] . field } ")` ;
116- errors . push ( new PanelError ( errorSummary , errorLoc , errorContext ) ) ;
117- }
110+ if ( row . length !== knownColsCount || row . includes ( undefined ) ) {
111+ _forgeColumnsCountError ( row , rowIndex + 1 , cols , errors ) ;
112+ continue ;
113+ }
114+
115+ for ( let colIndex = 0 ; colIndex < knownColsCount ; colIndex ++ ) {
116+ const { type, colOptions, acceptsEmptyFields, field } = colMeta [ colIndex ] ;
117+ const value = row [ colIndex ] ;
118+ if ( value === undefined ) continue ;
119+
120+ if ( type === 'date' && value !== undefined ) {
121+ if ( ! fastIsDate ( value ) ) {
122+ errors . push (
123+ new PanelError (
124+ 'Invalid date format' ,
125+ `Line ${ rowIndex + 1 } , Column ${ colIndex + 1 } ("${ field } ")` ,
126+ `Value "${ value } " is not a valid date (expected format yyyy-MM-dd)`
127+ )
128+ ) ;
118129 }
130+ return ;
119131 }
120- } ) ;
121- }
122132
133+ const validationResult = ValidationUtils . isValid ( value , type , colOptions , acceptsEmptyFields ) ;
134+ if ( validationResult !== true ) {
135+ const { summary : errorSummary , context : errorContext } = validationResult ;
136+ const errorLoc = `Line ${ rowIndex + 1 } , Column ${ colIndex + 1 } ("${ field } ")` ;
137+ errors . push ( new PanelError ( errorSummary , errorLoc , errorContext ) ) ;
138+ }
139+ }
140+ }
123141 return errors ;
124142} ;
125143
0 commit comments