Validate single reference not being an array

This commit is contained in:
Adria Navarro 2024-04-24 12:04:43 +02:00
parent 6999758105
commit 35ba5b93b5
1 changed files with 41 additions and 35 deletions

View File

@ -39,38 +39,38 @@ export async function processInputBBReferences<
type: T, type: T,
subtype?: TS subtype?: TS
): Promise<string | string[] | null> { ): Promise<string | string[] | null> {
let referenceIds: string[] = []
if (Array.isArray(value)) {
referenceIds.push(
...value.map(idOrDoc =>
typeof idOrDoc === "string" ? idOrDoc : idOrDoc._id
)
)
} else if (typeof value !== "string") {
referenceIds.push(value._id)
} else {
referenceIds.push(
...value
.split(",")
.filter(x => x)
.map((id: string) => id.trim())
)
}
// make sure all reference IDs are correct global user IDs
// they may be user metadata references (start with row prefix)
// and these need to be converted to global IDs
referenceIds = referenceIds.map(id => {
if (id?.startsWith(ROW_PREFIX)) {
return dbCore.getGlobalIDFromUserMetadataID(id)
} else {
return id
}
})
switch (type) { switch (type) {
case FieldType.BB_REFERENCE: case FieldType.BB_REFERENCE:
let referenceIds: string[] = []
if (Array.isArray(value)) {
referenceIds.push(
...value.map(idOrDoc =>
typeof idOrDoc === "string" ? idOrDoc : idOrDoc._id
)
)
} else if (typeof value !== "string") {
referenceIds.push(value._id)
} else {
referenceIds.push(
...value
.split(",")
.filter(x => x)
.map((id: string) => id.trim())
)
}
// make sure all reference IDs are correct global user IDs
// they may be user metadata references (start with row prefix)
// and these need to be converted to global IDs
referenceIds = referenceIds.map(id => {
if (id?.startsWith(ROW_PREFIX)) {
return dbCore.getGlobalIDFromUserMetadataID(id)
} else {
return id
}
})
switch (subtype) { switch (subtype) {
case undefined: case undefined:
throw "Subtype must be defined" throw "Subtype must be defined"
@ -93,13 +93,19 @@ export async function processInputBBReferences<
} }
case FieldType.BB_REFERENCE_SINGLE: { case FieldType.BB_REFERENCE_SINGLE: {
const user = await cache.user.getUser(referenceIds[0]) if (value && Array.isArray(value)) {
throw "BB_REFERENCE_SINGLE cannot be an array"
if (!user) {
throw new InvalidBBRefError(referenceIds[0], FieldSubtype.USER)
} }
return referenceIds[0] || null const id = typeof value === "string" ? value : value._id
const user = await cache.user.getUser(id)
if (!user) {
throw new InvalidBBRefError(id, FieldSubtype.USER)
}
return user._id || null
} }
default: default: