From 7e9b2cedb63303217be7bae95365ba301f82771f Mon Sep 17 00:00:00 2001 From: Erayd Date: Thu, 4 May 2017 09:31:01 +1200 Subject: [PATCH] Split "uri" format into "uri" and "uri-reference" --- src/JsonSchema/ConstraintError.php | 2 ++ .../Constraints/FormatConstraint.php | 9 ++++++++- tests/Constraints/FormatTest.php | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/JsonSchema/ConstraintError.php b/src/JsonSchema/ConstraintError.php index d4dda01c..c309076e 100644 --- a/src/JsonSchema/ConstraintError.php +++ b/src/JsonSchema/ConstraintError.php @@ -28,6 +28,7 @@ class ConstraintError extends Enum const FORMAT_STYLE = 'styleFormat'; const FORMAT_TIME = 'timeFormat'; const FORMAT_URL = 'urlFormat'; + const FORMAT_URL_REF = 'urlRefFormat'; const INVALID_SCHEMA = 'invalidSchema'; const LENGTH_MAX = 'maxLength'; const LENGTH_MIN = 'minLength'; @@ -77,6 +78,7 @@ public function getMessage() self::FORMAT_STYLE => 'Invalid style', self::FORMAT_TIME => 'Invalid time %s, expected format hh:mm:ss', self::FORMAT_URL => 'Invalid URL format', + self::FORMAT_URL_REF => 'Invalid URL reference format', self::LENGTH_MAX => 'Must be at most %d characters long', self::INVALID_SCHEMA => 'Schema is not valid', self::LENGTH_MIN => 'Must be at least %d characters long', diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index f03e6520..7bc059c2 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -99,6 +99,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = break; case 'uri': + if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { + $this->addError(ConstraintError::FORMAT_URL(), $path, array('format' => $schema->format)); + } + break; + + case 'uriref': + case 'uri-reference': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { // FILTER_VALIDATE_URL does not conform to RFC-3986, and cannot handle relative URLs, but // the json-schema spec uses RFC-3986, so need a bit of hackery to properly validate them. @@ -118,7 +125,7 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = $validURL = null; } if ($validURL === null) { - $this->addError(ConstraintError::FORMAT_URL(), $path, array('format' => $schema->format)); + $this->addError(ConstraintError::FORMAT_URL_REF(), $path, array('format' => $schema->format)); } } break; diff --git a/tests/Constraints/FormatTest.php b/tests/Constraints/FormatTest.php index ad7075d9..b035aafe 100644 --- a/tests/Constraints/FormatTest.php +++ b/tests/Constraints/FormatTest.php @@ -143,12 +143,12 @@ public function getValidFormats() array('555 320 1212', 'phone'), array('http://bluebox.org', 'uri'), - array('//bluebox.org', 'uri'), - array('/absolutePathReference/', 'uri'), - array('./relativePathReference/', 'uri'), - array('./relative:PathReference/', 'uri'), - array('relativePathReference/', 'uri'), - array('relative/Path:Reference/', 'uri'), + array('//bluebox.org', 'uri-reference'), + array('/absolutePathReference/', 'uri-reference'), + array('./relativePathReference/', 'uri-reference'), + array('./relative:PathReference/', 'uri-reference'), + array('relativePathReference/', 'uri-reference'), + array('relative/Path:Reference/', 'uri-reference'), array('info@something.edu', 'email'), @@ -200,6 +200,12 @@ public function getInvalidFormats() array('htt:/bluebox.org', 'uri'), array('.relative:path/reference/', 'uri'), array('', 'uri'), + array('//bluebox.org', 'uri'), + array('/absolutePathReference/', 'uri'), + array('./relativePathReference/', 'uri'), + array('./relative:PathReference/', 'uri'), + array('relativePathReference/', 'uri'), + array('relative/Path:Reference/', 'uri'), array('info@somewhere', 'email'),