PR comments

This commit is contained in:
Adria Navarro 2024-05-02 12:19:19 +01:00
parent 34b6581aed
commit 2b61172fe8
1 changed files with 11 additions and 3 deletions

View File

@ -54,6 +54,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults {
type: columnType, type: columnType,
subtype: columnSubtype, subtype: columnSubtype,
autocolumn: isAutoColumn, autocolumn: isAutoColumn,
constraints,
} = schema[columnName] || {} } = schema[columnName] || {}
// If the column had an invalid value we don't want to override it // 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 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 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") { if (typeof columnType !== "string") {
results.invalidColumns.push(columnName) results.invalidColumns.push(columnName)
@ -94,7 +101,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults {
} else if ( } else if (
(columnType === FieldType.BB_REFERENCE || (columnType === FieldType.BB_REFERENCE ||
columnType === FieldType.BB_REFERENCE_SINGLE) && columnType === FieldType.BB_REFERENCE_SINGLE) &&
!isValidBBReference(columnData, columnType, columnSubtype) !isValidBBReference(columnData, columnType, columnSubtype, isRequired)
) { ) {
results.schemaValidation[columnName] = false results.schemaValidation[columnName] = false
} else { } else {
@ -170,7 +177,8 @@ export function parse(rows: Rows, schema: TableSchema): Rows {
function isValidBBReference( function isValidBBReference(
data: any, data: any,
type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType subtype: BBReferenceFieldSubType,
isRequired: boolean
): boolean { ): boolean {
if (typeof data !== "string") { if (typeof data !== "string") {
return false return false
@ -178,7 +186,7 @@ function isValidBBReference(
if (type === FieldType.BB_REFERENCE_SINGLE) { if (type === FieldType.BB_REFERENCE_SINGLE) {
if (!data) { if (!data) {
return true return !isRequired
} }
const user = parseCsvExport<{ _id: string }>(data) const user = parseCsvExport<{ _id: string }>(data)
return db.isGlobalUserID(user._id) return db.isGlobalUserID(user._id)