Skip to content

Commit dbc66c6

Browse files
committed
fix: add custom validation prop for intrinsic validation
1 parent 9ca2a51 commit dbc66c6

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/Shared/Components/KeyValueTable/KeyValueTable.component.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,24 @@ export const KeyValueTable = <K extends string>({
107107
[updatedRows],
108108
)
109109

110-
const validationSchema: typeof parentValidationSchema = (value, key, rowId) => {
111-
if (validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency[value] > 1) {
112-
return false
113-
}
110+
const validationSchema = (
111+
value: Parameters<typeof parentValidationSchema>[0],
112+
key: Parameters<typeof parentValidationSchema>[1],
113+
rowId: Parameters<typeof parentValidationSchema>[2],
114+
shouldTriggerCustomValidation: boolean = true,
115+
) => {
116+
if (shouldTriggerCustomValidation) {
117+
if (validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency[value] > 1) {
118+
return false
119+
}
114120

115-
if (validateEmptyKeys && key === firstHeaderKey && !value) {
116-
const isValuePresentAtRow = updatedRows.some(({ id, data }) => id === rowId && data[secondHeaderKey].value)
117-
if (isValuePresentAtRow) {
118-
return true
121+
if (validateEmptyKeys && key === firstHeaderKey && !value) {
122+
const isValuePresentAtRow = updatedRows.some(
123+
({ id, data }) => id === rowId && data[secondHeaderKey].value,
124+
)
125+
if (isValuePresentAtRow) {
126+
return true
127+
}
119128
}
120129
}
121130

@@ -159,10 +168,11 @@ export const KeyValueTable = <K extends string>({
159168
}
160169
}
161170

171+
// Sending custom validation as false since already checked above
162172
const isValid = editedRows.every(
163173
({ data: _data, id }) =>
164-
validationSchema(_data[firstHeaderKey].value, firstHeaderKey, id) &&
165-
validationSchema(_data[secondHeaderKey].value, secondHeaderKey, id),
174+
validationSchema(_data[firstHeaderKey].value, firstHeaderKey, id, false) &&
175+
validationSchema(_data[secondHeaderKey].value, secondHeaderKey, id, false),
166176
)
167177

168178
return isValid

0 commit comments

Comments
 (0)