From c759b2b69014f69b08cd28bf480685d05afb4f1d Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Tue, 26 Jul 2022 12:32:55 +0800 Subject: [PATCH] feat(gui-v2): excel / csv validators --- .../nc-gui-v2/components/dlg/QuickImport.vue | 11 +++++++---- packages/nc-gui-v2/utils/validation.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/nc-gui-v2/components/dlg/QuickImport.vue b/packages/nc-gui-v2/components/dlg/QuickImport.vue index 1a442f65ed..0be010743a 100644 --- a/packages/nc-gui-v2/components/dlg/QuickImport.vue +++ b/packages/nc-gui-v2/components/dlg/QuickImport.vue @@ -8,7 +8,7 @@ import MdiFileIcon from '~icons/mdi/file-plus-outline' import MdiFileUploadOutlineIcon from '~icons/mdi/file-upload-outline' import MdiLinkVariantIcon from '~icons/mdi/link-variant' import MdiCodeJSONIcon from '~icons/mdi/code-json' -import { fieldRequiredValidator, importUrlValidator } from '~/utils/validation' +import { fieldRequiredValidator, importCsvUrlValidator, importExcelUrlValidator, importUrlValidator } from '~/utils/validation' import { extractSdkResponseErrorMsg } from '~/utils/errorUtils' import { ExcelTemplateAdapter, ExcelUrlTemplateAdapter, JSONTemplateAdapter, JSONUrlTemplateAdapter } from '~/utils/parsers' import { useProject } from '#imports' @@ -47,7 +47,7 @@ const importState = reactive({ const validators = computed(() => { return { - url: [fieldRequiredValidator, importUrlValidator], + url: [fieldRequiredValidator, importUrlValidator, importType === 'csv' ? importCsvUrlValidator : importExcelUrlValidator], maxRowsToParse: [fieldRequiredValidator], } }) @@ -122,7 +122,10 @@ async function parseAndExtractData(val: any, name: string) { templateData.value = null importData.value = null const templateGenerator: any = getAdapter(name, val) - + if (!templateGenerator) { + toast.error('Template Generator cannot be found!') + return + } await templateGenerator.init() templateGenerator.parse() templateData.value = templateGenerator.getTemplate() @@ -281,4 +284,4 @@ function getAdapter(name: string, val: any) { overflow: auto; height: 300px; } - \ No newline at end of file + diff --git a/packages/nc-gui-v2/utils/validation.ts b/packages/nc-gui-v2/utils/validation.ts index 4b83ce57aa..ec3f9bcc92 100644 --- a/packages/nc-gui-v2/utils/validation.ts +++ b/packages/nc-gui-v2/utils/validation.ts @@ -101,3 +101,21 @@ export const importUrlValidator = { callback() }, } + +export const importCsvUrlValidator = { + validator: (rule: any, value: any, callback: (errMsg?: string) => void) => { + if (value && !/.*\.(csv)/.test(value)) { + callback('Target file is not an accepted file type. The accepted file type is .csv!') + } + callback() + }, +} + +export const importExcelUrlValidator = { + validator: (rule: any, value: any, callback: (errMsg?: string) => void) => { + if (value && !/.*\.(xls|xlsx|xlsm|ods|ots)/.test(value)) { + callback('Target file is not an accepted file type. The accepted file types are .xls, .xlsx, .xlsm, .ods, .ots!') + } + callback() + }, +}