From 5e412f15a5cc498b14a0808a926b23d3dffbe6c1 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Fri, 21 Oct 2022 13:54:57 +0800 Subject: [PATCH] feat(nc-gui): validate each table after pre-import --- .../nc-gui/components/template/Editor.vue | 97 +++++++++---------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/packages/nc-gui/components/template/Editor.vue b/packages/nc-gui/components/template/Editor.vue index f59b758dd6..3b5dafcb54 100644 --- a/packages/nc-gui/components/template/Editor.vue +++ b/packages/nc-gui/components/template/Editor.vue @@ -290,8 +290,6 @@ function fieldsValidation(record: Record) { return true } - const tableName = meta.value?.title || '' - if (!record.destCn) { message.error(`${t('msg.error.columnDescriptionNotFound')} ${record.srcCn}`) return false @@ -304,63 +302,62 @@ function fieldsValidation(record: Record) { const v = columns.value.find((c) => c.title === record.destCn) as Record - // check if the input contains null value for a required column - if (v.pk ? !v.ai && !v.cdf : !v.cdf && v.rqd) { - if ( - importData[tableName] - .slice(0, maxRowsToParse) - .some((r: Record) => r[record.srcCn] === null || r[record.srcCn] === undefined || r[record.srcCn] === '') - ) { - message.error(t('msg.error.nullValueViolatesNotNull')) - } - } - - switch (v.uidt) { - case UITypes.Number: + for (const tableName of Object.keys(importData)) { + // check if the input contains null value for a required column + if (v.pk ? !v.ai && !v.cdf : !v.cdf && v.rqd) { if ( importData[tableName] .slice(0, maxRowsToParse) - .some( - (r: Record) => r[record.sourceCn] !== null && r[record.srcCn] !== undefined && isNaN(+r[record.srcCn]), - ) + .some((r: Record) => r[record.srcCn] === null || r[record.srcCn] === undefined || r[record.srcCn] === '') ) { - message.error(t('msg.error.sourceHasInvalidNumbers')) - return false + message.error(t('msg.error.nullValueViolatesNotNull')) } + } - break - case UITypes.Checkbox: - if ( - importData[tableName].slice(0, maxRowsToParse).some((r: Record) => { - if (r[record.srcCn] !== null && r[record.srcCn] !== undefined) { - let input = r[record.srcCn] - if (typeof input === 'string') { - input = input.replace(/["']/g, '').toLowerCase().trim() - return !( - input === 'false' || - input === 'no' || - input === 'n' || - input === '0' || - input === 'true' || - input === 'yes' || - input === 'y' || - input === '1' - ) - } + switch (v.uidt) { + case UITypes.Number: + if ( + importData[tableName] + .slice(0, maxRowsToParse) + .some( + (r: Record) => r[record.sourceCn] !== null && r[record.srcCn] !== undefined && isNaN(+r[record.srcCn]), + ) + ) { + message.error(t('msg.error.sourceHasInvalidNumbers')) + return false + } - return input !== 1 && input !== 0 && input !== true && input !== false - } + break + case UITypes.Checkbox: + if ( + importData[tableName].slice(0, maxRowsToParse).some((r: Record) => { + if (r[record.srcCn] !== null && r[record.srcCn] !== undefined) { + let input = r[record.srcCn] + if (typeof input === 'string') { + input = input.replace(/["']/g, '').toLowerCase().trim() + return !( + input === 'false' || + input === 'no' || + input === 'n' || + input === '0' || + input === 'true' || + input === 'yes' || + input === 'y' || + input === '1' + ) + } + return input !== 1 && input !== 0 && input !== true && input !== false + } + return false + }) + ) { + message.error(t('msg.error.sourceHasInvalidBoolean')) return false - }) - ) { - message.error(t('msg.error.sourceHasInvalidBoolean')) - - return false - } - break + } + break + } } - return true } @@ -541,7 +538,7 @@ function mapDefaultColumns() { for (const col of importColumns[0]) { const o = { srcCn: col.column_name, destCn: '', enabled: true } if (columns.value) { - const tableColumn = columns.value.find((c: Record) => c.title === col.column_name) + const tableColumn = columns.value.find((c) => c.column_name === col.column_name) if (tableColumn) { o.destCn = tableColumn.title as string } else {