Skip to content

Commit 93cd59a

Browse files
committed
error handling optimization
1 parent 59d236b commit 93cd59a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+205
-217
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class Md5HashKeyword extends \Ropi\JsonSchemaEvaluator\Keyword\AbstractKeyword i
254254
}
255255

256256
$draft = new \Ropi\JsonSchemaEvaluator\Draft\Draft202012();
257-
$draft->registerKeyword(new Md5HashKeyword());
257+
$draft->registerKeyword(new Md5HashKeyword(), 'https://example.tld/draft/2022-03/vocab/md5'); // Register keyword with custom vocabulary
258258

259259
$evaluator = new \Ropi\JsonSchemaEvaluator\JsonSchemaEvaluator();
260260

src/Draft/AbstractDraft.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public function vocabularyEnabled(string $vocabulary): bool
9494
{
9595
if (!$this->supportsVocabulary($vocabulary)) {
9696
throw new UnsupportedVocabularyException(
97-
'Can not enable vocabulary "'
97+
'Can not enable vocabulary \''
9898
. $vocabulary
99-
. '", because vocabulary is not supported',
99+
. '\', because vocabulary is not supported.',
100100
1647637917
101101
);
102102
}
@@ -108,9 +108,9 @@ public function enableVocabulary(string $vocabulary): void
108108
{
109109
if (!$this->supportsVocabulary($vocabulary)) {
110110
throw new UnsupportedVocabularyException(
111-
'Can not enable vocabulary "'
111+
'Can not enable vocabulary \''
112112
. $vocabulary
113-
. '", because vocabulary is not supported',
113+
. '\', because vocabulary is not supported.',
114114
1647637758
115115
);
116116
}
@@ -127,9 +127,9 @@ public function disableVocabulary(string $vocabulary): void
127127
{
128128
if (!$this->supportsVocabulary($vocabulary)) {
129129
throw new UnsupportedVocabularyException(
130-
'Can not disable vocabulary "'
130+
'Can not disable vocabulary \''
131131
. $vocabulary
132-
. '", because vocabulary is not supported',
132+
. '\', because vocabulary is not supported.',
133133
1647637759
134134
);
135135
}
@@ -222,6 +222,13 @@ public function evaluate(RuntimeEvaluationContext $context, bool $mutationsOnly
222222
$schema = $context->getCurrentSchema();
223223

224224
if (is_bool($schema)) {
225+
if ($schema === false) {
226+
$lastResult = $context->getLastResult();
227+
if ($lastResult) {
228+
$context->createResultForKeyword($lastResult->keyword)->invalidate('Not allowed');
229+
}
230+
}
231+
225232
return $schema;
226233
}
227234

src/EvaluationContext/RuntimeEvaluationContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ public function getLastResultByKeywordLocation(string $keywordLocation): ?Runtim
139139
return null;
140140
}
141141

142+
public function getLastResult(): ?RuntimeEvaluationResult
143+
{
144+
return end($this->results) ?: null;
145+
}
146+
142147
/**
143148
* @return RuntimeEvaluationResult[]
144149
*/

src/Keyword/Applicator/AdditionalPropertiesKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue) && !is_bool($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be a valid JSON Schema',
30+
'The value of \'%s\' must be a valid JSON Schema.',
3131
$this,
3232
$context
3333
);

src/Keyword/Applicator/AllOfKeyword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function evaluate(mixed $keywordValue, RuntimeEvaluationContext $context)
3737
if ($numMatches !== count($keywordValue)) {
3838
$result->invalidate(
3939
'Value must match all schemas, but matches only '
40-
. $numMatches
40+
. $numMatches,
41+
$numMatches
4142
);
4243
}
4344

src/Keyword/Applicator/ContainsKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue) && !is_bool($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be a valid JSON Schema',
30+
'The value of \'%s\' must be a valid JSON Schema.',
3131
$this,
3232
$context
3333
);

src/Keyword/Applicator/DependentSchemasKeyword.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be an object',
30+
'The value of \'%s\' must be an object.',
3131
$this,
3232
$context
3333
);
@@ -38,9 +38,9 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
3838

3939
if (!is_object($dependentSchema) && !is_bool($dependentSchema)) {
4040
throw new InvalidKeywordValueException(
41-
'The property "'
41+
'The property \''
4242
. $dependencyPropertyName
43-
. '" in "%s" object must be a valid JSON Schema',
43+
. '\' in \'%s\' object must be a valid JSON Schema.',
4444
$this,
4545
$context
4646
);

src/Keyword/Applicator/ElseKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue) && !is_bool($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be a valid JSON Schema',
30+
'The value of \'%s\' must be a valid JSON Schema.',
3131
$this,
3232
$context
3333
);

src/Keyword/Applicator/IfKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue) && !is_bool($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be a valid JSON Schema',
30+
'The value of \'%s\' must be a valid JSON Schema.',
3131
$this,
3232
$context
3333
);

src/Keyword/Applicator/ItemsKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_object($keywordValue) && !is_bool($keywordValue)) {
2929
throw new InvalidKeywordValueException(
30-
'The value of "%s" must be a valid JSON Schema',
30+
'The value of \'%s\' must be a valid JSON Schema.',
3131
$this,
3232
$context
3333
);

0 commit comments

Comments
 (0)