Skip to content

Commit 4f24aca

Browse files
committed
fix: add handling of uri-template format; fail on backslash in uri reference
1 parent 286da5b commit 4f24aca

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/JsonSchema/ConstraintError.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ConstraintError extends Enum
3333
public const FORMAT_REGEX= 'regexFormat';
3434
public const FORMAT_STYLE = 'styleFormat';
3535
public const FORMAT_TIME = 'timeFormat';
36+
public const FORMAT_URI_TEMPLATE = 'uriTemplateFormat';
3637
public const FORMAT_URL = 'urlFormat';
3738
public const FORMAT_URL_REF = 'urlRefFormat';
3839
public const INVALID_SCHEMA = 'invalidSchema';
@@ -90,6 +91,7 @@ public function getMessage()
9091
self::FORMAT_REGEX=> 'Invalid regex format %s',
9192
self::FORMAT_STYLE => 'Invalid style',
9293
self::FORMAT_TIME => 'Invalid time %s, expected format hh:mm:ss',
94+
self::FORMAT_URI_TEMPLATE => 'Invalid URI template format',
9395
self::FORMAT_URL => 'Invalid URL format',
9496
self::FORMAT_URL_REF => 'Invalid URL reference format',
9597
self::LENGTH_MAX => 'Must be at most %d characters long',

src/JsonSchema/Constraints/Drafts/Draft06/FormatConstraint.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
9696
$this->addError(ConstraintError::FORMAT_URL(), $path, ['format' => $schema->format]);
9797
}
9898
break;
99+
case 'uri-template':
100+
if (!$this->validateUriTemplate($value)) {
101+
$this->addError(ConstraintError::FORMAT_URI_TEMPLATE(), $path, ['format' => $schema->format]);
102+
}
103+
break;
99104

100105
case 'email':
101106
if (filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE) === null) {
@@ -113,7 +118,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
113118
$this->addError(ConstraintError::FORMAT_JSON_POINTER(), $path, ['format' => $schema->format]);
114119
}
115120
break;
116-
break;
117121
default:
118122
break;
119123
}
@@ -205,4 +209,12 @@ private function validateRfc3339DateTime(string $value): bool
205209
// Compare value and date result to be equal
206210
return true;
207211
}
212+
213+
private function validateUriTemplate(string $value): bool
214+
{
215+
return preg_match(
216+
'/^(?:[^\{\}]*|\{[a-zA-Z0-9_:%\/\.~\-\+\*]+\})*$/',
217+
$value
218+
) === 1;
219+
}
208220
}

src/JsonSchema/Tool/Validator/RelativeReferenceValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public static function isValid(string $ref): bool
1616
}
1717

1818
// Additional checks for invalid cases
19+
if (strpos($ref, '\\') !== false) {
20+
return false; // Backslashes are not allowed in URI references
21+
}
22+
1923
if (preg_match('/^(http|https):\/\//', $ref)) {
2024
return false; // Absolute URI
2125
}

0 commit comments

Comments
 (0)