Validate single reference not being an array
This commit is contained in:
parent
6999758105
commit
35ba5b93b5
|
@ -39,38 +39,38 @@ export async function processInputBBReferences<
|
|||
type: T,
|
||||
subtype?: TS
|
||||
): 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) {
|
||||
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) {
|
||||
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:
|
||||
|
|
Loading…
Reference in New Issue