Skip to content

Commit

Permalink
wip validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mioe committed May 23, 2024
1 parent 03bf7cc commit 06b8ad8
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const table = reactive({
columns: [],
})
const invalidCsv = reactive({
format: [],
localDuplicate: [],
removeDuplicate: [],
})
const mainCheckboxRef = shallowRef()
const maxColsInRow = computed(() =>
csv.value.reduce((max, row) => Math.max(max, row.length), 0) - HIDDEN_COL,
Expand Down Expand Up @@ -207,35 +213,75 @@ const onValidate = (result) => {
console.log('🦕 onValidate', null)
return
}
const typeField = table.columns[colIdx].type
if (typeField === 'string') {
console.log('🦕 onValidate', typeField)
const field = table.columns[colIdx]
if (field.type === 'string') {
console.log('🦕 onValidate', field.type)
return
}
if (ev === 'select') {
onCheckValidColumn(typeField, colIdx)
onCheckValidColumn(field, colIdx)
} else if (ev === 'submit') {
onCheckValidValue(typeField, colIdx, rowIdx)
onCheckValidValue(field, colIdx, rowIdx)
return
}
}
const onCheckValidColumn = (type, colIdx) => {
const getRegexByType = (type) => {
if (type === 'phone') {
return /^\+?[1-9]\d{1,14}$/
} else if (type === 'date') {
return /^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(\d{4})$/
} else if (type === 'number') {
return /^-?\d{1,3}(,\d{3})*(\.\d+)?$/
}
return null
}
const onCheckValidColumn = (field, colIdx) => {
const { type } = field
const idxWithUuid = colIdx + HIDDEN_COL
console.log('🦕 onCheckValidColumn', type, colIdx, idxWithUuid)
csv.value.forEach((row, rowIdx) => {
if (rowIdx === 0) {
console.log('🦕 row[0]', row[idxWithUuid], row)
}
})
const regex = getRegexByType(type)
if (!regex) {
return
}
if (type === 'phone') {
csv.value.forEach((row, rowIdx) => {
console.log('🦕 msg', regex.test(row[idxWithUuid].replace(/\D/g, '')))
if (!regex.test(row[idxWithUuid].replace(/\D/g, ''))) {
onDetectionInvalid({
field,
invalidType: 'format',
rowIdx,
colIdxWithUuid: idxWithUuid,
})
}
})
}
}
const onCheckValidValue = (type, colIdx, rowIdx) => {
const onCheckValidValue = (field, colIdx, rowIdx) => {
const { type } = field
const idxWithUuid = colIdx + HIDDEN_COL
console.log('🦕 onCheckValidValue', type, colIdx, rowIdx, idxWithUuid)
}
const onDetectionInvalid = (err) => {
console.log('🦕 onDetectionInvalid', err)
const {
field,
invalidType,
rowIdx,
colIdxWithUuid,
} = err
invalidCsv[invalidType].push({ field, rowIdx, colIdxWithUuid })
}
onUpdated(() => {
console.log('🦕 onUpdated')
if (!table.checkboxes.length) {
Expand Down

0 comments on commit 06b8ad8

Please sign in to comment.