refactor: simplify relationship validation code

This commit is contained in:
Prem Palanisamy 2026-01-27 06:59:53 +00:00
parent 63e6a51af1
commit 00d091513d
4 changed files with 7 additions and 21 deletions

View file

@ -251,7 +251,8 @@ abstract class Action extends DatabasesAction
}
/**
* Validate a relationship value and its document ID.
* Validate relationship values.
* Handles Document objects, ID strings, and associative arrays.
*/
protected function validateRelationship(mixed $relation): void
{
@ -263,8 +264,6 @@ abstract class Action extends DatabasesAction
$relationId = $relation;
} elseif (\is_array($relation) && \array_values($relation) !== $relation) {
$relationId = $relation['$id'] ?? null;
} else {
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, 'Relationship value must be an object or document ID string, not ' . \gettype($relation));
}
if ($relationId !== null) {

View file

@ -309,22 +309,17 @@ class Create extends Action
);
foreach ($relations as &$relation) {
// Generate unique ID for new relation without $id
if (
\is_array($relation)
&& \array_values($relation) !== $relation
&& !isset($relation['$id'])
) {
$relation['$id'] = ID::unique();
$relation = new Document($relation);
}
$this->validateRelationship($relation);
// If the relation is an array it can be either update or create a child document.
if (\is_array($relation) && \array_values($relation) !== $relation) {
$relation = new Document($relation);
}
if ($relation instanceof Document) {
$relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser);

View file

@ -201,22 +201,18 @@ class Update extends Action
);
foreach ($relations as &$relation) {
// Generate unique ID for new relation without $id
// If the relation is an array it can be either update or create a child document.
if (
\is_array($relation)
&& \array_values($relation) !== $relation
&& !isset($relation['$id'])
) {
$relation['$id'] = ID::unique();
$relation = new Document($relation);
}
$this->validateRelationship($relation);
// If the relation is an array it can be either update or create a child document.
if (\is_array($relation) && \array_values($relation) !== $relation) {
$relation = new Document($relation);
}
if ($relation instanceof Document) {
$relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser);

View file

@ -211,22 +211,18 @@ class Upsert extends Action
);
foreach ($relations as &$relation) {
// Generate unique ID for new relation without $id
// If the relation is an array it can be either update or create a child document.
if (
\is_array($relation)
&& \array_values($relation) !== $relation
&& !isset($relation['$id'])
) {
$relation['$id'] = ID::unique();
$relation = new Document($relation);
}
$this->validateRelationship($relation);
// If the relation is an array it can be either update or create a child document.
if (\is_array($relation) && \array_values($relation) !== $relation) {
$relation = new Document($relation);
}
if ($relation instanceof Document) {
$relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser);