Skip to content

Commit 5fc0476

Browse files
Merge pull request #81 from utopia-php/fix-query-validator-constructor
Construct query validator from array of attribute documents
2 parents 1f0be55 + dd3d5fa commit 5fc0476

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/Database/Validator/QueryValidator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Database\Validator;
44

55
use Utopia\Validator;
6+
use Utopia\Database\Document;
67
use Utopia\Database\Query;
78

89
class QueryValidator extends Validator
@@ -34,11 +35,13 @@ class QueryValidator extends Validator
3435
/**
3536
* Expression constructor
3637
*
37-
* @param array $schema
38+
* @param Document[] $attributes
3839
*/
39-
public function __construct($schema)
40+
public function __construct(array $attributes)
4041
{
41-
$this->schema = $schema;
42+
foreach ($attributes as $attribute) {
43+
$this->schema[] = $attribute->getArrayCopy();
44+
}
4245
}
4346

4447
/**

tests/Database/Validator/QueriesTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ class QueriesTest extends TestCase
9696

9797
public function setUp(): void
9898
{
99-
$this->queryValidator = new QueryValidator($this->collection['attributes']);
99+
// Query validator expects Document[]
100+
$attributes = []; /** @var Document[] $attributes */
101+
foreach ($this->collection['attributes'] as $attribute) {
102+
$attributes[] = new Document($attribute);
103+
}
104+
105+
$this->queryValidator = new QueryValidator($attributes);
100106

101107
$query1 = Query::parse('title.notEqual("Iron Man", "Ant Man")');
102108
$query2 = Query::parse('description.equal("Best movie ever")');

tests/Database/Validator/QueryValidatorTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
use Utopia\Database\Validator\QueryValidator;
66
use PHPUnit\Framework\TestCase;
77
use Utopia\Database\Database;
8+
use Utopia\Database\Document;
89
use Utopia\Database\Query;
910

1011
class QueryValidatorTest extends TestCase
1112
{
1213
/**
13-
* @var array
14+
* @var Document[]
1415
*/
15-
protected $schema = [
16+
protected $schema;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $attributes = [
1622
[
1723
'$id' => 'title',
1824
'key' => 'title',
@@ -77,6 +83,10 @@ class QueryValidatorTest extends TestCase
7783

7884
public function setUp(): void
7985
{
86+
// Query validator expects Document[]
87+
foreach ($this->attributes as $attribute) {
88+
$this->schema[] = new Document($attribute);
89+
}
8090
}
8191

8292
public function tearDown(): void

0 commit comments

Comments
 (0)