Skip to content

Commit 9a96110

Browse files
added object validator test
1 parent f5c0cfd commit 9a96110

File tree

7 files changed

+118
-11
lines changed

7 files changed

+118
-11
lines changed

src/Database/Validator/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464
*/
6565
public function getType(): string
6666
{
67-
return self::VAR_OBJECT;
67+
return self::TYPE_OBJECT;
6868
}
6969

7070
/**

src/Database/Validator/IndexDependency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ public function isArray(): bool
8080
*/
8181
public function getType(): string
8282
{
83-
return self::VAR_OBJECT;
83+
return self::TYPE_OBJECT;
8484
}
8585
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Utopia\Database\Validator;
4+
5+
use Utopia\Validator;
6+
7+
class ObjectValidator extends Validator
8+
{
9+
/**
10+
* Get Description
11+
*/
12+
public function getDescription(): string
13+
{
14+
return 'Value must be a valid object';
15+
}
16+
17+
/**
18+
* Is Valid
19+
*
20+
* @param mixed $value
21+
*/
22+
public function isValid(mixed $value): bool
23+
{
24+
return empty($value) || is_array($value) && !array_is_list($value);
25+
}
26+
27+
/**
28+
* Is Array
29+
*/
30+
public function isArray(): bool
31+
{
32+
return false;
33+
}
34+
35+
/**
36+
* Get Type
37+
*/
38+
public function getType(): string
39+
{
40+
return self::TYPE_OBJECT;
41+
}
42+
}

src/Database/Validator/Queries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ public function isArray(): bool
168168
*/
169169
public function getType(): string
170170
{
171-
return self::VAR_OBJECT;
171+
return self::TYPE_OBJECT;
172172
}
173173
}

src/Database/Validator/Query/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function isArray(): bool
4848
*/
4949
public function getType(): string
5050
{
51-
return self::VAR_OBJECT;
51+
return self::TYPE_OBJECT;
5252
}
5353

5454
/**

src/Database/Validator/Structure.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,8 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys)
359359
break;
360360

361361
case Database::VAR_OBJECT:
362-
// For JSONB/object types, just validate it's an array (associative or list)
363-
if (!is_array($value)) {
364-
$this->message = 'Attribute "'.$key.'" has invalid type. Value must be an array for object type';
365-
return false;
366-
}
367-
// No additional validators needed - JSONB accepts any valid array structure
368-
continue 2; // Skip to next attribute
362+
$validators[] = new ObjectValidator();
363+
break;
369364

370365
case Database::VAR_POINT:
371366
case Database::VAR_LINESTRING:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Tests\Unit\Validator;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Utopia\Database\Validator\ObjectValidator;
7+
8+
class ObjectTest extends TestCase
9+
{
10+
public function testValidAssociativeObjects(): void
11+
{
12+
$validator = new ObjectValidator();
13+
14+
$this->assertTrue($validator->isValid(['key' => 'value']));
15+
$this->assertTrue($validator->isValid([
16+
'a' => [
17+
'b' => [
18+
'c' => 123
19+
]
20+
]
21+
]));
22+
23+
$this->assertTrue($validator->isValid([
24+
'author' => 'Arnab',
25+
'metadata' => [
26+
'rating' => 4.5,
27+
'info' => [
28+
'category' => 'science'
29+
]
30+
]
31+
]));
32+
33+
$this->assertTrue($validator->isValid([
34+
'key1' => null,
35+
'key2' => ['nested' => null]
36+
]));
37+
38+
$this->assertTrue($validator->isValid([
39+
'meta' => (object)['x' => 1]
40+
]));
41+
42+
$this->assertTrue($validator->isValid([
43+
'a' => 1,
44+
2 => 'b'
45+
]));
46+
47+
}
48+
49+
public function testInvalidStructures(): void
50+
{
51+
$validator = new ObjectValidator();
52+
53+
$this->assertFalse($validator->isValid(['a', 'b', 'c']));
54+
55+
$this->assertFalse($validator->isValid('not an array'));
56+
57+
$this->assertFalse($validator->isValid([
58+
0 => 'value'
59+
]));
60+
}
61+
62+
public function testEmptyCases(): void
63+
{
64+
$validator = new ObjectValidator();
65+
66+
$this->assertTrue($validator->isValid([]));
67+
68+
$this->assertFalse($validator->isValid('sldfjsdlfj'));
69+
}
70+
}

0 commit comments

Comments
 (0)