budibase/packages/server/src/utilities/schema.ts

215 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-10-09 18:02:21 +02:00
import { FieldSubtype } from "@budibase/types"
import { FieldTypes } from "../constants"
2023-10-09 18:02:21 +02:00
import { ValidColumnNameRegex, utils } from "@budibase/shared-core"
import { db } from "@budibase/backend-core"
2023-10-10 10:47:43 +02:00
import { parseCsvExport } from "../api/controllers/view/exporters"
interface SchemaColumn {
readonly name: string
readonly type: FieldTypes
2023-10-09 18:02:21 +02:00
readonly subtype: FieldSubtype
readonly autocolumn?: boolean
2023-05-02 13:48:05 +02:00
readonly constraints?: {
presence: boolean
}
}
interface Schema {
readonly [index: string]: SchemaColumn
}
interface Row {
[index: string]: any
}
type Rows = Array<Row>
interface SchemaValidation {
[index: string]: boolean
}
interface ValidationResults {
schemaValidation: SchemaValidation
allValid: boolean
invalidColumns: Array<string>
errors: Record<string, string>
}
const PARSERS: any = {
[FieldTypes.NUMBER]: (attribute?: string) => {
if (!attribute) {
return attribute
}
return Number(attribute)
},
[FieldTypes.DATETIME]: (attribute?: string) => {
if (!attribute) {
return attribute
}
return new Date(attribute).toISOString()
},
}
export function isSchema(schema: any): schema is Schema {
return (
typeof schema === "object" &&
Object.values(schema).every(rawColumn => {
const column = rawColumn as SchemaColumn
return (
column !== null &&
typeof column === "object" &&
typeof column.type === "string" &&
Object.values(FieldTypes).includes(column.type as FieldTypes)
)
})
)
}
export function isRows(rows: any): rows is Rows {
return Array.isArray(rows) && rows.every(row => typeof row === "object")
}
export function validate(rows: Rows, schema: Schema): ValidationResults {
const results: ValidationResults = {
schemaValidation: {},
allValid: false,
invalidColumns: [],
errors: {},
}
rows.forEach(row => {
Object.entries(row).forEach(([columnName, columnData]) => {
const columnType = schema[columnName]?.type
2023-10-09 18:02:21 +02:00
const columnSubtype = schema[columnName]?.subtype
const isAutoColumn = schema[columnName]?.autocolumn
2023-10-09 18:02:21 +02:00
// If the column had an invalid value we don't want to override it
if (results.schemaValidation[columnName] === false) {
return
}
// If the columnType is not a string, then it's not present in the schema, and should be added to the invalid columns array
if (typeof columnType !== "string") {
results.invalidColumns.push(columnName)
} else if (!columnName.match(ValidColumnNameRegex)) {
// Check for special characters in column names
results.schemaValidation[columnName] = false
results.errors[columnName] =
"Column names can't contain special characters"
2023-05-02 13:48:05 +02:00
} else if (
columnData == null &&
!schema[columnName].constraints?.presence
) {
results.schemaValidation[columnName] = true
} else if (
// If there's no data for this field don't bother with further checks
// If the field is already marked as invalid there's no need for further checks
results.schemaValidation[columnName] === false ||
columnData == null ||
isAutoColumn
) {
return
} else if (
columnType === FieldTypes.NUMBER &&
isNaN(Number(columnData))
) {
// If provided must be a valid number
results.schemaValidation[columnName] = false
} else if (
// If provided must be a valid date
columnType === FieldTypes.DATETIME &&
isNaN(new Date(columnData).getTime())
) {
results.schemaValidation[columnName] = false
2023-10-09 18:02:21 +02:00
} else if (
columnType === FieldTypes.BB_REFERENCE &&
!isValidBBReference(columnData, columnSubtype)
) {
results.schemaValidation[columnName] = false
} else {
results.schemaValidation[columnName] = true
}
})
})
results.allValid =
Object.values(results.schemaValidation).length > 0 &&
Object.values(results.schemaValidation).every(column => column)
// Select unique values
results.invalidColumns = [...new Set(results.invalidColumns)]
return results
}
export function parse(rows: Rows, schema: Schema): Rows {
return rows.map(row => {
const parsedRow: Row = {}
Object.entries(row).forEach(([columnName, columnData]) => {
if (!(columnName in schema) || schema[columnName]?.autocolumn) {
// Objects can be present in the row data but not in the schema, so make sure we don't proceed in such a case
return
}
const columnType = schema[columnName].type
if (columnType === FieldTypes.NUMBER) {
// If provided must be a valid number
parsedRow[columnName] = columnData ? Number(columnData) : columnData
} else if (columnType === FieldTypes.DATETIME) {
// If provided must be a valid date
parsedRow[columnName] = columnData
? new Date(columnData).toISOString()
: columnData
2023-10-10 11:58:45 +02:00
} else if (columnType === FieldTypes.BB_REFERENCE) {
parsedRow[columnName] = columnData && parseCsvExport(columnData)
} else {
parsedRow[columnName] = columnData
}
})
return parsedRow
})
}
2023-10-09 18:02:21 +02:00
function isValidBBReference(
columnData: any,
columnSubtype: FieldSubtype
): boolean {
switch (columnSubtype) {
case FieldSubtype.USER:
case FieldSubtype.USERS:
if (!columnData) {
// Empty columns are valid by default
return true
}
2023-10-10 10:47:43 +02:00
if (typeof columnData !== "string") {
return false
}
const castedData = parseCsvExport(columnData)
if (!Array.isArray(castedData)) {
2023-10-09 18:02:21 +02:00
// It must be an array field
return false
}
2023-10-10 10:47:43 +02:00
if (columnSubtype === FieldSubtype.USER && castedData.length > 1) {
2023-10-09 18:02:21 +02:00
return false
}
2023-10-10 10:47:43 +02:00
for (const d of castedData) {
2023-10-09 18:02:21 +02:00
if (!db.isGlobalUserID(d._id)) {
return false
}
}
return true
default:
throw utils.unreachable(columnSubtype)
}
}