Skip to content

Commit bd39123

Browse files
committed
enhance hostname validation
1 parent 18b7ac3 commit bd39123

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Schema/StringSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function endsWith(string $endsWith): static
229229
public function hostname(): static
230230
{
231231
return $this->postParse(static function (string $string) {
232-
if (filter_var($string, FILTER_VALIDATE_DOMAIN)) {
232+
if (filter_var($string, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
233233
return $string;
234234
}
235235

tests/Unit/Schema/StringSchemaTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,32 @@ public function testParseWithInvalidHostname(): void
466466
}
467467
}
468468

469+
public function testParseWithInvalidHostnameWithInvalidLabelCharacters(): void
470+
{
471+
$input = 'exa_mple.com';
472+
473+
$schema = (new StringSchema())->hostname();
474+
475+
try {
476+
$schema->parse($input);
477+
478+
throw new \Exception('code should not be reached');
479+
} catch (ErrorsException $errorsException) {
480+
self::assertSame([
481+
[
482+
'path' => '',
483+
'error' => [
484+
'code' => 'string.hostname',
485+
'template' => 'Invalid hostname {{given}}',
486+
'variables' => [
487+
'given' => $input,
488+
],
489+
],
490+
],
491+
], $errorsException->errors->jsonSerialize());
492+
}
493+
}
494+
469495
public function testParseWithValidDomain(): void
470496
{
471497
$input = 'example.com';

0 commit comments

Comments
 (0)