Validate single reference not being an array
This commit is contained in:
parent
6999758105
commit
35ba5b93b5
|
@ -39,6 +39,8 @@ export async function processInputBBReferences<
|
||||||
type: T,
|
type: T,
|
||||||
subtype?: TS
|
subtype?: TS
|
||||||
): Promise<string | string[] | null> {
|
): Promise<string | string[] | null> {
|
||||||
|
switch (type) {
|
||||||
|
case FieldType.BB_REFERENCE:
|
||||||
let referenceIds: string[] = []
|
let referenceIds: string[] = []
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
|
@ -69,8 +71,6 @@ export async function processInputBBReferences<
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case FieldType.BB_REFERENCE:
|
|
||||||
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:
|
||||||
|
|
Loading…
Reference in New Issue