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,6 +39,8 @@ export async function processInputBBReferences<
type: T,
subtype?: TS
): Promise<string | string[] | null> {
switch (type) {
case FieldType.BB_REFERENCE:
let referenceIds: string[] = []
if (Array.isArray(value)) {
@ -69,8 +71,6 @@ export async function processInputBBReferences<
}
})
switch (type) {
case FieldType.BB_REFERENCE:
switch (subtype) {
case undefined:
throw "Subtype must be defined"
@ -93,13 +93,19 @@ export async function processInputBBReferences<
}
case FieldType.BB_REFERENCE_SINGLE: {
const user = await cache.user.getUser(referenceIds[0])
if (!user) {
throw new InvalidBBRefError(referenceIds[0], FieldSubtype.USER)
if (value && Array.isArray(value)) {
throw "BB_REFERENCE_SINGLE cannot be an array"
}
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: