Skip to content

Commit 321f6af

Browse files
author
Ben Saunders
committed
Updating to use more correct hostname regex. This fixes #129
1 parent 8490e82 commit 321f6af

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ protected function validatePhone($phone)
173173

174174
protected function validateHostname($host)
175175
{
176-
return preg_match('/^[_a-z]+\.([_a-z]+\.?)+$/i', $host);
176+
$hostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/i';
177+
return preg_match($hostnameRegex, $host);
177178
}
178179
}

tests/Constraints/FormatTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public function getValidFormats()
123123
array('::ff', 'ipv6'),
124124

125125
array('www.example.com', 'host-name'),
126+
array('3v4l.org', 'host-name'),
127+
array('a-valid-host.com', 'host-name'),
128+
array('localhost', 'host-name'),
126129

127130
array('anything', '*'),
128131
array('unknown', '*'),
@@ -166,7 +169,8 @@ public function getInvalidFormats()
166169

167170
array(':::ff', 'ipv6'),
168171

169-
array('localhost', 'host-name'),
172+
array('@localhost', 'host-name'),
173+
array('..nohost', 'host-name'),
170174

171175
);
172176
}

0 commit comments

Comments
 (0)