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
);

src/Keyword/Applicator/NotKeyword.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/OfKeywordTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function evaluateStaticOf(mixed $keywordValue, StaticKeywordInterface $ke
1919
{
2020
if (!is_array($keywordValue) || !$keywordValue) {
2121
throw new InvalidKeywordValueException(
22-
'The value of "%s" must be a non-empty array',
22+
'The value of \'%s\' must be a non-empty array.',
2323
$keyword,
2424
$context
2525
);
@@ -30,7 +30,7 @@ public function evaluateStaticOf(mixed $keywordValue, StaticKeywordInterface $ke
3030

3131
if (!is_object($schema) && !is_bool($schema)) {
3232
throw new InvalidKeywordValueException(
33-
'The array elements of "%s" must be valid JSON Schemas',
33+
'The array elements of \'%s\' must be valid JSON Schemas.',
3434
$keyword,
3535
$context
3636
);

src/Keyword/Applicator/OneOfKeyword.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 !== 1) {
3838
$result->invalidate(
3939
'Value must match exactly one schema, but matches '
40-
. $numMatches
40+
. $numMatches,
41+
$numMatches
4142
);
4243
}
4344

src/Keyword/Applicator/PatternPropertiesKeyword.php

Lines changed: 2 additions & 2 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,7 +38,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
3838

3939
if (!is_object($patternPropertySchema) && !is_bool($patternPropertySchema)) {
4040
throw new InvalidKeywordValueException(
41-
'The property values of "%s" must valid JSON Schemas',
41+
'The property values of \'%s\' must valid JSON Schemas.',
4242
$this,
4343
$context
4444
);

src/Keyword/Applicator/PrefixItemsKeyword.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2727
{
2828
if (!is_array($keywordValue) || !$keywordValue) {
2929
throw new InvalidKeywordValueException(
30-
'The "%s" must be a non-empty array',
30+
'The \'%s\' must be a non-empty array.',
3131
$this,
3232
$context
3333
);
@@ -38,7 +38,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
3838

3939
if (!is_object($prefixItemSchema) && !is_bool($prefixItemSchema)) {
4040
throw new InvalidKeywordValueException(
41-
'The array elements of "%s" must be valid JSON Schemas',
41+
'The array elements of \'%s\' must be valid JSON Schemas.',
4242
$this,
4343
$context
4444
);

src/Keyword/Applicator/PropertiesKeyword.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($propertySchema) && !is_bool($propertySchema)) {
4040
throw new InvalidKeywordValueException(
41-
'Property "'
41+
'Property \''
4242
. $propertyName
43-
. '" of "%s" object must be a valid JSON Schema',
43+
. '\' of \'%s\' object must be a valid JSON Schema.',
4444
$this,
4545
$context
4646
);

src/Keyword/Applicator/PropertyNamesKeyword.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/RefKeyword.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
3333
{
3434
if (!is_string($keywordValue)) {
3535
throw new InvalidKeywordValueException(
36-
'The value of "%s" must be a string',
36+
'The value of \'%s\' must be a string.',
3737
$this,
3838
$context
3939
);
@@ -42,7 +42,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
4242
$uri = $context->draft->createUri($keywordValue);
4343
if (!$uri) {
4444
throw new InvalidKeywordValueException(
45-
'The value of "%s" must be a valid URI reference',
45+
'The value of \'%s\' must be a valid URI reference.',
4646
$this,
4747
$context
4848
);
@@ -91,7 +91,7 @@ public function evaluate(mixed $keywordValue, RuntimeEvaluationContext $context)
9191
$referencedSchema = $context->draft->dereferenceJsonPointer($referencedSchema, $fragment);
9292
if ($referencedSchema === null) {
9393
throw new KeywordRuntimeEvaluationException(
94-
'Can not dereference JSON pointer "' . $fragment . '"',
94+
'Can not dereference JSON pointer \'' . $fragment . '\'.',
9595
$this,
9696
$context
9797
);
@@ -103,7 +103,7 @@ public function evaluate(mixed $keywordValue, RuntimeEvaluationContext $context)
103103
$schemaInstanceLocationHash = spl_object_hash($context->getCurrentSchema()) . ':' . $context->getCurrentInstanceLocation();
104104
if (isset($this->processedSchemaInstanceLocations[$schemaInstanceLocationHash])) {
105105
throw new KeywordRuntimeEvaluationException(
106-
'"%s" causes an infinite recursion',
106+
'\'%s\' causes an infinite recursion.',
107107
$this,
108108
$context
109109
);
@@ -136,7 +136,7 @@ protected function dereferenceSchema(string $schemaUri, RuntimeEvaluationContext
136136
$referencedSchema = $context->staticEvaluationContext->getSchemaByUri($schemaUri);
137137
if (!$referencedSchema) {
138138
throw new KeywordRuntimeEvaluationException(
139-
'Failed to dereference schema URI "' . $schemaUri . '"',
139+
'Failed to dereference schema URI \'' . $schemaUri . '\'.',
140140
$this,
141141
$context
142142
);

src/Keyword/Applicator/ThenKeyword.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/UnevaluatedItemsKeyword.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/UnevaluatedPropertiesKeyword.php

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

src/Keyword/Identifier/AnchorKeyword.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2929
{
3030
if (!is_string($keywordValue)) {
3131
throw new InvalidKeywordValueException(
32-
'The value of "%s" must be a string',
32+
'The value of \'%s\' must be a string.',
3333
$this,
3434
$context
3535
);
3636
}
3737

3838
if (preg_match(static::PATTERN_XML_NC_NAME_US_ASCII, $keywordValue) !== 1) {
3939
throw new InvalidKeywordValueException(
40-
'The value of "%s" must start with a letter ([A-Za-z]) or underscore ("_"), followed by any number of'
41-
. ' letters, digits ([0-9]), hyphens ("-"), underscores ("_"), and periods (".")',
40+
'The value of \'%s\' must start with a letter (a-z) or underscore (_), followed by any number of'
41+
. ' letters, digits (0-9), hyphens (-), underscores (_), and periods (.).',
4242
$this,
4343
$context
4444
);
@@ -48,7 +48,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
4848

4949
if ($context->hasSchema((string) $anchorUri)) {
5050
throw new StaticKeywordAnalysisException(
51-
'The value "' . $keywordValue . '" of "%s" is defined twice, but must be unique in each JSON Schema',
51+
'The value \'' . $keywordValue . '\' of \'%s\' is defined twice, but must be unique in each JSON Schema.',
5252
$this,
5353
$context
5454
);

src/Keyword/Identifier/IdKeyword.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
2626
{
2727
if (!is_string($keywordValue)) {
2828
throw new InvalidKeywordValueException(
29-
'The value of "%s" must be a string',
29+
'The value of \'%s\' must be a string.',
3030
$this,
3131
$context
3232
);
@@ -35,15 +35,15 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
3535
$uri = $context->draft->createUri($keywordValue);
3636
if (!$uri) {
3737
throw new InvalidKeywordValueException(
38-
'The value of "%s" must be a valid URI reference',
38+
'The value of \'%s\' must be a valid URI reference.',
3939
$this,
4040
$context
4141
);
4242
}
4343

4444
if ($uri->getFragment()) {
4545
throw new InvalidKeywordValueException(
46-
'The URI reference of "%s" must not contain a non-empty fragment',
46+
'The URI reference of \'%s\' must not contain a non-empty fragment.',
4747
$this,
4848
$context
4949
);
@@ -54,7 +54,7 @@ public function evaluateStatic(mixed &$keywordValue, StaticEvaluationContext $co
5454

5555
if ($context->hasSchema((string) $normalizedUri)) {
5656
throw new StaticKeywordAnalysisException(
57-
'The URI reference "' . $keywordValue . '" is defined twice',
57+
'The URI reference \'' . $keywordValue . '\' is defined twice.',
5858
$this,
5959
$context
6060
);

0 commit comments

Comments
 (0)