Browse Source

fix(gui): add validation for paste data

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4514/head
Pranav C 2 years ago
parent
commit
4283f48ee8
  1. 10
      packages/nc-gui/components/cell/Checkbox.vue
  2. 55
      packages/nc-gui/composables/useMultiSelect/convertCellData.ts
  3. 29
      packages/nc-gui/composables/useMultiSelect/index.ts

10
packages/nc-gui/components/cell/Checkbox.vue

@ -1,13 +1,5 @@
<script setup lang="ts">
import {
ActiveCellInj,
ColumnInj,
IsFormInj,
ReadonlyInj,
getMdiIcon,
inject,
useSelectedCellKeyupListener,
} from '#imports'
import { ActiveCellInj, ColumnInj, IsFormInj, ReadonlyInj, getMdiIcon, inject, useSelectedCellKeyupListener } from '#imports'
interface Props {
// If the previous cell value was a text, the initial checkbox value is a string type

55
packages/nc-gui/composables/useMultiSelect/convertCellData.ts

@ -1,18 +1,61 @@
import dayjs from 'dayjs'
import { UITypes } from 'nocodb-sdk'
export default function convertCellData(args: { from: UITypes; to: UITypes; value: any }) {
export default function convertCellData(args: { from: UITypes; to: UITypes; value: any }, isMysql = false) {
const { from, to, value } = args
if (from === to && from !== UITypes.Attachment) {
if (from === to && ![UITypes.Attachment, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year].includes(to)) {
return value
}
const dateFormat = isMysql ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'
switch (to) {
case UITypes.Number:
return Number(value)
case UITypes.Number: {
const parsedNumber = Number(value)
if (isNaN(parsedNumber)) {
throw new TypeError(`Cannot convert '${value}' to number`)
}
return parsedNumber
}
case UITypes.Checkbox:
return Boolean(value)
case UITypes.Date:
return new Date(value)
case UITypes.Date: {
const parsedDate = dayjs(value)
if (!parsedDate.isValid()) throw new Error('Not a valid date')
return parsedDate.format('YYYY-MM-DD')
}
case UITypes.DateTime: {
const parsedDateTime = dayjs(value)
if (!parsedDateTime.isValid()) {
throw new Error('Not a valid datetime value')
}
return parsedDateTime.format(dateFormat)
}
case UITypes.Time: {
let parsedTime = dayjs(value)
if (!parsedTime.isValid()) {
parsedTime = dayjs(value, 'HH:mm:ss')
}
if (!parsedTime.isValid()) {
parsedTime = dayjs(`1999-01-01 ${value}`)
}
if (!parsedTime.isValid()) {
throw new Error('Not a valid time value')
}
return parsedTime.format(dateFormat)
}
case UITypes.Year: {
const parsedDate = dayjs(value)
if (!parsedDate.isValid()) {
return parsedDate.format('YYYY')
}
if (/^\d+$/.test(value)) {
throw new Error('Not a valid year value')
}
return value
}
case UITypes.Attachment: {
let parsedVal
try {

29
packages/nc-gui/composables/useMultiSelect/index.ts

@ -16,6 +16,7 @@ import {
useEventListener,
useI18n,
useMetas,
useProject,
} from '#imports'
import type { Row } from '~/lib'
@ -42,6 +43,8 @@ export function useMultiSelect(
const { getMeta } = useMetas()
const { isMysql } = useProject()
let clipboardContext = $ref<{ value: any; uidt: UITypes } | null>(null)
const editEnabled = ref(_editEnabled)
@ -264,11 +267,14 @@ export function useMultiSelect(
if (!clipboardContext.value || typeof clipboardContext.value !== 'object') {
return message.info('Invalid data')
}
rowObj.row[columnObj.title!] = convertCellData({
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
})
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
},
isMysql.value,
)
e.preventDefault()
const foreignKeyColumn: ColumnType = meta.value?.columns.find(
@ -291,11 +297,14 @@ export function useMultiSelect(
}
if (clipboardContext) {
rowObj.row[columnObj.title!] = convertCellData({
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
})
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
},
isMysql.value,
)
e.preventDefault()
syncCellData?.(selectedCell)
} else {

Loading…
Cancel
Save