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"> <script setup lang="ts">
import { import { ActiveCellInj, ColumnInj, IsFormInj, ReadonlyInj, getMdiIcon, inject, useSelectedCellKeyupListener } from '#imports'
ActiveCellInj,
ColumnInj,
IsFormInj,
ReadonlyInj,
getMdiIcon,
inject,
useSelectedCellKeyupListener,
} from '#imports'
interface Props { interface Props {
// If the previous cell value was a text, the initial checkbox value is a string type // 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' 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 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 return value
} }
const dateFormat = isMysql ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'
switch (to) { switch (to) {
case UITypes.Number: case UITypes.Number: {
return Number(value) const parsedNumber = Number(value)
if (isNaN(parsedNumber)) {
throw new TypeError(`Cannot convert '${value}' to number`)
}
return parsedNumber
}
case UITypes.Checkbox: case UITypes.Checkbox:
return Boolean(value) return Boolean(value)
case UITypes.Date: case UITypes.Date: {
return new Date(value) 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: { case UITypes.Attachment: {
let parsedVal let parsedVal
try { try {

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

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

Loading…
Cancel
Save