mirror of
https://github.com/appwrite/appwrite
synced 2026-05-23 08:58:35 +00:00
refactor: simplify relationship validation code
This commit is contained in:
parent
63e6a51af1
commit
00d091513d
4 changed files with 7 additions and 21 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue