diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index 42dec55d..8843dd61 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -173,6 +173,7 @@ protected function validatePhone($phone) protected function validateHostname($host) { - return preg_match('/^[_a-z]+\.([_a-z]+\.?)+$/i', $host); + $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'; + return preg_match($hostnameRegex, $host); } } diff --git a/tests/Constraints/FormatTest.php b/tests/Constraints/FormatTest.php index 3ffe5b19..a71a6ae8 100644 --- a/tests/Constraints/FormatTest.php +++ b/tests/Constraints/FormatTest.php @@ -123,6 +123,9 @@ public function getValidFormats() array('::ff', 'ipv6'), array('www.example.com', 'host-name'), + array('3v4l.org', 'host-name'), + array('a-valid-host.com', 'host-name'), + array('localhost', 'host-name'), array('anything', '*'), array('unknown', '*'), @@ -165,7 +168,8 @@ public function getInvalidFormats() array(':::ff', 'ipv6'), - array('localhost', 'host-name'), + array('@localhost', 'host-name'), + array('..nohost', 'host-name'), ); }