Browse Source

fix(nc-gui): validation logic

pull/4519/head
Wing-Kam Wong 2 years ago
parent
commit
d2bcf4ace2
  1. 8
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts
  2. 16
      packages/nc-gui/utils/parsers/parserHelpers.ts

8
packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

@ -132,15 +132,15 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
if (column.uidt === UITypes.SingleLineText) { if (column.uidt === UITypes.SingleLineText) {
// check for long text // check for long text
if (isMultiLineTextType(rows)) { if (isMultiLineTextType(rows, col)) {
column.uidt = UITypes.LongText column.uidt = UITypes.LongText
} }
if (isEmailType(rows)) { if (isEmailType(rows, col)) {
column.uidt = UITypes.Email column.uidt = UITypes.Email
} }
if (isUrlType(rows)) { if (isUrlType(rows, col)) {
column.uidt = UITypes.URL column.uidt = UITypes.URL
} else { } else {
const vals = rows const vals = rows
@ -148,7 +148,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
.map((r: any) => r[col]) .map((r: any) => r[col])
.filter((v: any) => v !== null && v !== undefined && v.toString().trim() !== '') .filter((v: any) => v !== null && v !== undefined && v.toString().trim() !== '')
const checkboxType = isCheckboxType(vals) const checkboxType = isCheckboxType(vals, col)
if (checkboxType.length === 1) { if (checkboxType.length === 1) {
column.uidt = UITypes.Checkbox column.uidt = UITypes.Checkbox
} else { } else {

16
packages/nc-gui/utils/parsers/parserHelpers.ts

@ -20,11 +20,11 @@ const booleanOptions = [
] ]
const aggBooleanOptions: any = booleanOptions.reduce((obj, o) => ({ ...obj, ...o }), {}) const aggBooleanOptions: any = booleanOptions.reduce((obj, o) => ({ ...obj, ...o }), {})
const getColVal = (row: any, col = null) => { const getColVal = (row: any, col?: number) => {
return row && col ? row[col] : row return row && col ? row[col] : row
} }
export const isCheckboxType: any = (values: [], col = null) => { export const isCheckboxType: any = (values: [], col?: number) => {
let options = booleanOptions let options = booleanOptions
for (let i = 0; i < values.length; i++) { for (let i = 0; i < values.length; i++) {
const val = getColVal(values[i], col) const val = getColVal(values[i], col)
@ -45,7 +45,7 @@ export const getCheckboxValue = (value: any) => {
return value && aggBooleanOptions[value] return value && aggBooleanOptions[value]
} }
export const isMultiLineTextType = (values: [], col = null) => { export const isMultiLineTextType = (values: [], col?: number) => {
return values.some( return values.some(
(r) => (getColVal(r, col) || '').toString().match(/[\r\n]/) || (getColVal(r, col) || '').toString().length > 255, (r) => (getColVal(r, col) || '').toString().match(/[\r\n]/) || (getColVal(r, col) || '').toString().length > 255,
) )
@ -107,13 +107,15 @@ export const isDecimalType = (colData: []) =>
return v && parseInt(v) !== +v return v && parseInt(v) !== +v
}) })
export const isEmailType = (colData: []) => export const isEmailType = (colData: [], col?: number) =>
colData.some((v: any) => { colData.some((r: any) => {
const v = getColVal(r, col)
return v && validateEmail(v) return v && validateEmail(v)
}) })
export const isUrlType = (colData: []) => export const isUrlType = (colData: [], col?: number) =>
colData.some((v: any) => { colData.some((r: any) => {
const v = getColVal(r, col)
return v && isValidURL(v) return v && isValidURL(v)
}) })

Loading…
Cancel
Save