Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Validator/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Domain extends Validator
*/
public function __construct(
protected array $restrictions = [],
protected bool $hostnames = true
protected bool $hostnames = true,
protected bool $allowEmpty = false
) {
}

Expand Down Expand Up @@ -64,6 +65,10 @@ public function getDescription(): string
*/
public function isValid($value): bool
{
if ($this->allowEmpty && $value === '') {
return true;
}

if (empty($value)) {
return false;
}
Expand Down
25 changes: 24 additions & 1 deletion tests/Validator/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,31 @@
$this->assertSame(true, $permissiveValidator->isValid('example.com/path')); // Path
}

public function testRestrictions()
/**
* Test allowEmpty parameter
*/
public function testAllowEmpty()
{
// By default, empty string is invalid
$this->assertSame(false, $this->domain->isValid(''));

// With allowEmpty=true, empty string is valid
$domainAllowEmpty = new Domain([], true, true);
$this->assertSame(true, $domainAllowEmpty->isValid(''));

// null is still invalid even with allowEmpty=true
$this->assertSame(false, $domainAllowEmpty->isValid(null));

// Valid domains still pass with allowEmpty=true
$this->assertSame(true, $domainAllowEmpty->isValid('example.com'));
$this->assertSame(true, $domainAllowEmpty->isValid('subdomain.example.com'));

// Invalid domains still fail with allowEmpty=true
$this->assertSame(false, $domainAllowEmpty->isValid('invalid..domain'));
$this->assertSame(false, $domainAllowEmpty->isValid(1));
}

{

Check failure on line 147 in tests/Validator/DomainTest.php

View workflow job for this annotation

GitHub Actions / CodeQL

Syntax error, unexpected '{' on line 147
Comment thread
Meldiron marked this conversation as resolved.
Comment thread
Meldiron marked this conversation as resolved.
$validator = new Domain([
Domain::createRestriction('appwrite.network', 3, ['preview-', 'branch-']),
Domain::createRestriction('fra.appwrite.run', 4),
Expand Down Expand Up @@ -175,4 +198,4 @@
$this->assertSame(true, $permissiveValidator->isValid('subdomain.example.com'));
$this->assertSame(true, $permissiveValidator->isValid('subdomain-new.example.com'));
}
}

Check failure on line 201 in tests/Validator/DomainTest.php

View workflow job for this annotation

GitHub Actions / CodeQL

Syntax error, unexpected '}', expecting EOF on line 201
Loading