|
1 | 1 | // Copyright (c) Cosmo Tech. |
2 | 2 | // Licensed under the MIT license. |
| 3 | +import DateUtils from '../DateUtils/DateUtils'; |
3 | 4 | import { ValidationUtils } from '../ValidationUtils'; |
4 | 5 | import { Error as PanelError } from '../models'; |
5 | 6 | import CSV from './CSVUtils'; |
@@ -86,38 +87,50 @@ const _getColTypeFromTypeArray = (typeArray) => { |
86 | 87 | }; |
87 | 88 |
|
88 | 89 | const _validateFormat = (rows, hasHeader, cols, options) => { |
89 | | - const colsData = cols.map((col) => ({ ...col, type: _getColTypeFromTypeArray(col.type) })); |
90 | 90 | const errors = []; |
91 | | - const knownColsCount = colsData.length; |
| 91 | + const knownColsCount = cols.length; |
92 | 92 | const startIndex = hasHeader ? 1 : 0; |
| 93 | + |
| 94 | + const colMeta = cols.map((col) => ({ |
| 95 | + field: col.field, |
| 96 | + type: _getColTypeFromTypeArray(col.type), |
| 97 | + acceptsEmptyFields: col.acceptsEmptyFields ?? col.cellEditorParams?.acceptsEmptyFields ?? false, |
| 98 | + colOptions: { |
| 99 | + ...options, |
| 100 | + enumValues: col.enumValues ?? col.cellEditorParams?.enumValues, |
| 101 | + minValue: col.minValue, |
| 102 | + maxValue: col.maxValue, |
| 103 | + }, |
| 104 | + })); |
| 105 | + // For date columns, convert min & max values to do it only once |
| 106 | + colMeta.forEach((col) => { |
| 107 | + if (col.type === 'date' && options?.dateFormat) { |
| 108 | + const colOptions = col.colOptions; |
| 109 | + if (colOptions.minValue != null) colOptions.minDate = DateUtils.parse(colOptions.minValue, options?.dateFormat); |
| 110 | + if (colOptions.maxValue != null) colOptions.maxDate = DateUtils.parse(colOptions.maxValue, options?.dateFormat); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
93 | 114 | for (let rowIndex = startIndex; rowIndex < rows.length; rowIndex++) { |
94 | 115 | const row = rows[rowIndex]; |
95 | 116 | 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 | | - } |
118 | | - } |
| 117 | + |
| 118 | + if (row.length !== knownColsCount || row.includes(undefined)) { |
| 119 | + _forgeColumnsCountError(row, rowIndex + 1, cols, errors); |
| 120 | + } |
| 121 | + |
| 122 | + for (let colIndex = 0; colIndex < knownColsCount; colIndex++) { |
| 123 | + const { type, colOptions, acceptsEmptyFields, field } = colMeta[colIndex]; |
| 124 | + const value = row[colIndex]; |
| 125 | + if (value === undefined) continue; |
| 126 | + |
| 127 | + const validationResult = ValidationUtils.isValid(value, type, colOptions, acceptsEmptyFields); |
| 128 | + if (validationResult !== true) { |
| 129 | + const { summary: errorSummary, context: errorContext } = validationResult; |
| 130 | + const errorLoc = `Line ${rowIndex + 1}, Column ${colIndex + 1} ("${field}")`; |
| 131 | + errors.push(new PanelError(errorSummary, errorLoc, errorContext)); |
119 | 132 | } |
120 | | - }); |
| 133 | + } |
121 | 134 | } |
122 | 135 |
|
123 | 136 | return errors; |
|
0 commit comments