Skip to content

Commit fb02144

Browse files
committed
Validate references via API instead of locally
Validation rules can change at any time, and the SDK must adapt to the API. Local checks could block valid paths if Firebase loosens a rule, so they’ve been removed. Developers can be trusted not to use invalid paths 😅.
1 parent 0967f92 commit fb02144

File tree

6 files changed

+11
-206
lines changed

6 files changed

+11
-206
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ If it saves you or your team time, please consider [sponsoring its development](
55

66
## [Unreleased]
77

8+
### Changed
9+
10+
* Realtime Database references are now validated by the API instead of locally. Validation rules can change at any time,
11+
and the SDK can only adapt to changes in the API. While local checks could prevent obviously invalid paths, they’d
12+
also require an SDK update whenever Firebase loosens a rule. Developers can be trusted not to use invalid paths 😅.
13+
814
## [7.23.0] - 2025-10-13
915

1016
* Require `cuyz/valinor:^2.2.1` for better mapping.

src/Firebase/Database/Reference.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Kreait\Firebase\Database;
66

7-
use Kreait\Firebase\Database\Reference\Validator;
87
use Kreait\Firebase\Exception\DatabaseException;
98
use Kreait\Firebase\Exception\InvalidArgumentException;
109
use Kreait\Firebase\Exception\OutOfRangeException;
@@ -27,21 +26,13 @@
2726
*/
2827
class Reference implements Stringable
2928
{
30-
private readonly UriInterface $uri;
31-
3229
/**
3330
* @internal
34-
*
35-
* @throws InvalidArgumentException if the reference URI is invalid
3631
*/
3732
public function __construct(
38-
UriInterface $uri,
33+
private readonly UriInterface $uri,
3934
private readonly ApiClient $apiClient,
40-
private readonly Validator $validator = new Validator(),
4135
) {
42-
$this->validator->validateUri($uri);
43-
44-
$this->uri = $uri;
4536
}
4637

4738
/**
@@ -89,19 +80,15 @@ public function getParent(): self
8980
throw new OutOfRangeException('Cannot get parent of root reference');
9081
}
9182

92-
return new self(
93-
$this->uri->withPath('/'.ltrim($parentPath, '/')),
94-
$this->apiClient,
95-
$this->validator,
96-
);
83+
return new self($this->uri->withPath('/'.ltrim($parentPath, '/')), $this->apiClient);
9784
}
9885

9986
/**
10087
* The root location of a Reference.
10188
*/
10289
public function getRoot(): self
10390
{
104-
return new self($this->uri->withPath('/'), $this->apiClient, $this->validator);
91+
return new self($this->uri->withPath('/'), $this->apiClient);
10592
}
10693

10794
/**
@@ -117,11 +104,7 @@ public function getChild(string $path): self
117104
$childPath = sprintf('/%s/%s', trim($this->uri->getPath(), '/'), trim($path, '/'));
118105

119106
try {
120-
return new self(
121-
$this->uri->withPath($childPath),
122-
$this->apiClient,
123-
$this->validator,
124-
);
107+
return new self($this->uri->withPath($childPath), $this->apiClient);
125108
} catch (\InvalidArgumentException $e) {
126109
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
127110
}
@@ -323,7 +306,7 @@ public function push($value = null): self
323306
$newKey = $this->apiClient->push($this->uri->getPath(), $value);
324307
$newPath = sprintf('%s/%s', $this->uri->getPath(), $newKey);
325308

326-
return new self($this->uri->withPath($newPath), $this->apiClient, $this->validator);
309+
return new self($this->uri->withPath($newPath), $this->apiClient);
327310
}
328311

329312
/**

src/Firebase/Database/Reference/Validator.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

tests/Unit/Database/Reference/ValidatorTest.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/Unit/Database/ReferenceTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use GuzzleHttp\Psr7\Uri;
88
use Kreait\Firebase\Database\ApiClient;
99
use Kreait\Firebase\Database\Reference;
10-
use Kreait\Firebase\Exception\InvalidArgumentException;
1110
use Kreait\Firebase\Exception\OutOfRangeException;
1211
use Kreait\Firebase\Tests\UnitTestCase;
1312
use PHPUnit\Framework\Attributes\Test;
@@ -77,13 +76,6 @@ public function getChild(): void
7776
$this->assertSame('parent/key/child', $child->getPath());
7877
}
7978

80-
#[Test]
81-
public function getInvalidChild(): void
82-
{
83-
$this->expectException(InvalidArgumentException::class);
84-
$this->reference->getChild('#');
85-
}
86-
8779
#[Test]
8880
public function getChildKeys(): void
8981
{

tests/Unit/DatabaseTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ public function getRootReference(): void
4444
$this->assertSame('/', $this->database->getReference()->getUri()->getPath());
4545
}
4646

47-
#[Test]
48-
public function getReferenceWithInvalidPath(): void
49-
{
50-
$this->expectException(InvalidArgumentException::class);
51-
$this->database->getReference('#');
52-
}
53-
5447
#[Test]
5548
public function getReferenceFromUrl(): void
5649
{

0 commit comments

Comments
 (0)