Skip to content

Commit 066c526

Browse files
Merge pull request #1245 from habx/feature/addPrevValToValidate
APP-26556: add `prevVal` prop for validation
2 parents b5f9833 + 956e728 commit 066c526

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/imex/imex.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface IMEXOptions {
7878
format?: (value: any, row: any[]) => any
7979
parse?: (value: any, row: any[]) => any
8080
width?: number
81-
validate?: (value: any, row: any[]) => string | boolean | null
81+
validate?: (value: any, row: any[], prevVal?: any) => string | boolean | null
8282
dataValidation?: Excel.DataValidation
8383
hidden?: boolean
8484
note?: string | Excel.Comment

src/imex/import/useImportTable.utils.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ export const parseRawData = async <D extends { id?: string | number }>(
176176
errors: {},
177177
isIgnored: false,
178178
}
179-
180179
const importedRowValue: Partial<D> = {}
181180

182181
const rawRowValues = Object.values(row)
182+
183183
for (let index = 0; index < rawRowValues.length; index++) {
184184
const rawCell = rawRowValues[index]
185185
const currentColumn = orderedColumns[index]
@@ -199,6 +199,22 @@ export const parseRawData = async <D extends { id?: string | number }>(
199199

200200
const ignoreEmpty = currentColumn.imex?.ignoreEmpty ?? true
201201

202+
/**
203+
* Previous value
204+
*/
205+
const prevValIdentifier = get(importedRowValue, getPath(identifierColumn))
206+
const prevValIndex = originalData.findIndex((originalRow) => {
207+
if (options.findPrevValPredicate) {
208+
return options.findPrevValPredicate(originalRow, importedRowValue)
209+
}
210+
return (
211+
get(originalRow, getPath(identifierColumn)) ===
212+
prevValIdentifier
213+
)
214+
})
215+
216+
importedRowMeta.prevVal = originalData[prevValIndex]
217+
202218
try {
203219
newCellValue = parseCell(
204220
rawCell,
@@ -216,7 +232,12 @@ export const parseRawData = async <D extends { id?: string | number }>(
216232
}
217233

218234
const validate = currentColumn.imex?.validate ?? (() => true)
219-
const validateResponse = validate(newCellValue, row)
235+
const validateResponse = validate(
236+
newCellValue,
237+
row,
238+
importedRowMeta.prevVal
239+
)
240+
220241
const isValid =
221242
typeof validateResponse === 'string'
222243
? !validateResponse.length
@@ -245,26 +266,6 @@ export const parseRawData = async <D extends { id?: string | number }>(
245266
set(importedRowValue, columnDataPath, newCellValue)
246267
}
247268

248-
/**
249-
* Previous value
250-
*/
251-
const prevValIdentifier = get(
252-
importedRowValue,
253-
identifierColumn.accessor as string
254-
)
255-
const prevValIndex = originalData.findIndex((originalRow) => {
256-
if (options.findPrevValPredicate) {
257-
return options.findPrevValPredicate(originalRow, importedRowValue)
258-
}
259-
return (
260-
get(originalRow, identifierColumn.accessor as string) ===
261-
prevValIdentifier
262-
)
263-
})
264-
265-
importedRowMeta.prevVal = originalData[prevValIndex]
266-
originalData.splice(prevValIndex, 1) // remove from original array
267-
268269
/**
269270
* Diff
270271
*/

0 commit comments

Comments
 (0)