From 2b61172fe84ce76e44cd4b189e52573135b597fe Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 2 May 2024 12:19:19 +0100 Subject: [PATCH] PR comments --- packages/server/src/utilities/schema.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/server/src/utilities/schema.ts b/packages/server/src/utilities/schema.ts index 4ac1623325..51f67813ed 100644 --- a/packages/server/src/utilities/schema.ts +++ b/packages/server/src/utilities/schema.ts @@ -54,6 +54,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults { type: columnType, subtype: columnSubtype, autocolumn: isAutoColumn, + constraints, } = schema[columnName] || {} // If the column had an invalid value we don't want to override it @@ -61,6 +62,12 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults { return } + const isRequired = + !!constraints && + ((typeof constraints.presence !== "boolean" && + !constraints.presence?.allowEmpty) || + constraints.presence === true) + // 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) @@ -94,7 +101,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults { } else if ( (columnType === FieldType.BB_REFERENCE || columnType === FieldType.BB_REFERENCE_SINGLE) && - !isValidBBReference(columnData, columnType, columnSubtype) + !isValidBBReference(columnData, columnType, columnSubtype, isRequired) ) { results.schemaValidation[columnName] = false } else { @@ -170,7 +177,8 @@ export function parse(rows: Rows, schema: TableSchema): Rows { function isValidBBReference( data: any, type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, - subtype: BBReferenceFieldSubType + subtype: BBReferenceFieldSubType, + isRequired: boolean ): boolean { if (typeof data !== "string") { return false @@ -178,7 +186,7 @@ function isValidBBReference( if (type === FieldType.BB_REFERENCE_SINGLE) { if (!data) { - return true + return !isRequired } const user = parseCsvExport<{ _id: string }>(data) return db.isGlobalUserID(user._id)