From c64d76eb845e43650195fa4067ec1f217e7b3d41 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 19 Jul 2024 15:07:58 +0100 Subject: [PATCH] Respond to PR comments. --- packages/backend-core/src/sql/utils.ts | 5 +++++ .../server/src/utilities/rowProcessor/index.ts | 16 +++++++--------- .../server/src/utilities/rowProcessor/map.ts | 5 ++--- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/backend-core/src/sql/utils.ts b/packages/backend-core/src/sql/utils.ts index e73c6ac445..67b5d2081b 100644 --- a/packages/backend-core/src/sql/utils.ts +++ b/packages/backend-core/src/sql/utils.ts @@ -8,6 +8,7 @@ const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const ROW_ID_REGEX = /^\[.*]$/g const ENCODED_SPACE = encodeURIComponent(" ") const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/ +const TIME_REGEX = /^(?:\d{2}:)?(?:\d{2}:)(?:\d{2})$/ export function isExternalTableID(tableId: string) { return tableId.startsWith(DocumentType.DATASOURCE + SEPARATOR) @@ -147,6 +148,10 @@ export function isValidFilter(value: any) { return value != null && value !== "" } +export function isValidTime(value: string) { + return TIME_REGEX.test(value) +} + export function sqlLog(client: string, query: string, values?: any[]) { if (!environment.SQL_LOGGING_ENABLE) { return diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 54d86d55ff..71de056814 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -99,18 +99,16 @@ export async function processAutoColumn( } } -async function processDeafultValues(table: Table, row: Row) { +async function processDefaultValues(table: Table, row: Row) { const ctx: { ["Current User"]?: User; user?: User } = {} const identity = context.getIdentity() - if (identity) { - if (identity._id && identity.type === IdentityType.USER) { - const user = await cache.user.getUser(identity._id) - delete user.password + if (identity?._id && identity.type === IdentityType.USER) { + const user = await cache.user.getUser(identity._id) + delete user.password - ctx["Current User"] = user - ctx.user = user - } + ctx["Current User"] = user + ctx.user = user } for (let [key, schema] of Object.entries(table.schema)) { @@ -221,7 +219,7 @@ export async function inputProcessing( } await processAutoColumn(userId, table, clonedRow, opts) - await processDeafultValues(table, clonedRow) + await processDefaultValues(table, clonedRow) return { table, row: clonedRow } } diff --git a/packages/server/src/utilities/rowProcessor/map.ts b/packages/server/src/utilities/rowProcessor/map.ts index d86e0fe4d2..db52c4718c 100644 --- a/packages/server/src/utilities/rowProcessor/map.ts +++ b/packages/server/src/utilities/rowProcessor/map.ts @@ -1,7 +1,6 @@ +import { sql } from "@budibase/backend-core" import { FieldType } from "@budibase/types" -const TIME_REGEX = /^(?:\d{2}:)?(?:\d{2}:)(?:\d{2})$/ - const parseArrayString = (value: any) => { if (typeof value === "string") { if (value === "") { @@ -117,7 +116,7 @@ export const TYPE_TRANSFORM_MAP: any = { parse: (date: any) => { if (date instanceof Date) { return date.toISOString() - } else if (typeof date === "string" && TIME_REGEX.test(date)) { + } else if (typeof date === "string" && sql.utils.isValidTime(date)) { return date } else { const parsed = new Date(date)