fix(db-mongodb): add validation to relationship ids (#8395)

fixes https://github.com/payloadcms/payload/issues/8652
This commit is contained in:
Patrik
2024-10-11 13:49:07 -04:00
committed by GitHub
parent 7a0b419c10
commit 21606ded08
2 changed files with 30 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
import type { CollectionConfig, Field, SanitizedConfig, TraverseFieldsCallback } from 'payload'
import mongoose from 'mongoose'
import { traverseFields } from 'payload'
import { APIError, traverseFields } from 'payload'
import { fieldAffectsData } from 'payload/shared'
type Args = {
@@ -31,7 +31,14 @@ const convertValue = ({
)
if (!customIDField) {
return new mongoose.Types.ObjectId(value)
try {
return new mongoose.Types.ObjectId(value)
} catch (error) {
throw new APIError(
`Failed to create ObjectId from value: ${value}. Error: ${error.message}`,
400,
)
}
}
return value