Fix schema parser

This commit is contained in:
Adria Navarro 2024-08-02 11:51:19 +02:00
parent 02d6458ac8
commit e1ace85248
2 changed files with 34 additions and 13 deletions

View File

@ -51,7 +51,3 @@ export function jsonWithSchema(schema: TableSchema, rows: Row[]) {
export function isFormat(format: any): format is RowExportFormat { export function isFormat(format: any): format is RowExportFormat {
return Object.values(RowExportFormat).includes(format as RowExportFormat) return Object.values(RowExportFormat).includes(format as RowExportFormat)
} }
export function parseCsvExport<T>(value: string) {
return JSON.parse(value) as T
}

View File

@ -8,7 +8,6 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { ValidColumnNameRegex, helpers, utils } from "@budibase/shared-core" import { ValidColumnNameRegex, helpers, utils } from "@budibase/shared-core"
import { db } from "@budibase/backend-core" import { db } from "@budibase/backend-core"
import { parseCsvExport } from "../api/controllers/view/exporters"
type Rows = Array<Row> type Rows = Array<Row>
@ -159,7 +158,7 @@ export function parse(rows: Rows, table: Table): Rows {
const columnSchema = schema[columnName] const columnSchema = schema[columnName]
const { type: columnType } = columnSchema const { type: columnType } = columnSchema
if (columnType === FieldType.NUMBER) { if ([FieldType.NUMBER, FieldType.BIGINT].includes(columnType)) {
// If provided must be a valid number // If provided must be a valid number
parsedRow[columnName] = columnData ? Number(columnData) : columnData parsedRow[columnName] = columnData ? Number(columnData) : columnData
} else if ( } else if (
@ -171,16 +170,23 @@ export function parse(rows: Rows, table: Table): Rows {
parsedRow[columnName] = columnData parsedRow[columnName] = columnData
? new Date(columnData).toISOString() ? new Date(columnData).toISOString()
: columnData : columnData
} else if (
columnType === FieldType.JSON &&
typeof columnData === "string"
) {
parsedRow[columnName] = parseJsonExport(columnData)
} else if (columnType === FieldType.BB_REFERENCE) { } else if (columnType === FieldType.BB_REFERENCE) {
let parsedValues: { _id: string }[] = columnData || [] let parsedValues: { _id: string }[] = columnData || []
if (columnData) { if (columnData && typeof columnData === "string") {
parsedValues = parseCsvExport<{ _id: string }[]>(columnData) parsedValues = parseJsonExport<{ _id: string }[]>(columnData)
} }
parsedRow[columnName] = parsedValues?.map(u => u._id) parsedRow[columnName] = parsedValues?.map(u => u._id)
} else if (columnType === FieldType.BB_REFERENCE_SINGLE) { } else if (columnType === FieldType.BB_REFERENCE_SINGLE) {
const parsedValue = let parsedValue = columnData
columnData && parseCsvExport<{ _id: string }>(columnData) if (columnData && typeof columnData === "string") {
parsedValue = parseJsonExport<{ _id: string }>(columnData)
}
parsedRow[columnName] = parsedValue?._id parsedRow[columnName] = parsedValue?._id
} else if ( } else if (
(columnType === FieldType.ATTACHMENTS || (columnType === FieldType.ATTACHMENTS ||
@ -188,7 +194,7 @@ export function parse(rows: Rows, table: Table): Rows {
columnType === FieldType.SIGNATURE_SINGLE) && columnType === FieldType.SIGNATURE_SINGLE) &&
typeof columnData === "string" typeof columnData === "string"
) { ) {
parsedRow[columnName] = parseCsvExport(columnData) parsedRow[columnName] = parseJsonExport(columnData)
} else { } else {
parsedRow[columnName] = columnData parsedRow[columnName] = columnData
} }
@ -212,14 +218,14 @@ function isValidBBReference(
if (!data) { if (!data) {
return !isRequired return !isRequired
} }
const user = parseCsvExport<{ _id: string }>(data) const user = parseJsonExport<{ _id: string }>(data)
return db.isGlobalUserID(user._id) return db.isGlobalUserID(user._id)
} }
switch (subtype) { switch (subtype) {
case BBReferenceFieldSubType.USER: case BBReferenceFieldSubType.USER:
case BBReferenceFieldSubType.USERS: { case BBReferenceFieldSubType.USERS: {
const userArray = parseCsvExport<{ _id: string }[]>(data) const userArray = parseJsonExport<{ _id: string }[]>(data)
if (!Array.isArray(userArray)) { if (!Array.isArray(userArray)) {
return false return false
} }
@ -233,3 +239,22 @@ function isValidBBReference(
throw utils.unreachable(subtype) throw utils.unreachable(subtype)
} }
} }
function parseJsonExport<T>(value: string) {
try {
const parsed = JSON.parse(value)
return parsed as T
} catch (e: any) {
if (
e.message.startsWith("Expected property name or '}' in JSON at position ")
) {
// This was probably converted as CSV and it has single quotes instead of double ones
const parsed = JSON.parse(value.replace(/'/g, '"'))
return parsed as T
}
// It is no a valid JSON
throw e
}
}