Skip to content

Commit 7ac9d94

Browse files
committed
error readability
1 parent feb950b commit 7ac9d94

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ No valid results for oneOf {
118118
} at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo
119119
```
120120

121-
For ambiguous schemas defined with `oneOf`/`anyOf` message is indented multi-line.
121+
For ambiguous schemas defined with `oneOf`/`anyOf` message is indented multi-line string.
122122

123-
Processing path is a combination of schema and data pointers. You can use `PointerUtil` to extract schema/data pointer.
123+
Processing path is a combination of schema and data pointers. You can use `InvalidValue->getSchemaPointer()`
124+
and `InvalidValue->getDataPointer()` to extract schema/data pointer.
124125

125-
You can build `Error` tree from exception using `InvalidValue::inspect($exception)`.
126+
You can build error tree using `InvalidValue->inspect()`.
126127

127128
### PHP structured classes with validation
128129

src/InvalidValue.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,29 @@ public function addPath($path)
2424
const NOT_IMPLEMENTED = 2;
2525

2626

27-
public static function inspect(InvalidValue $invalidValue)
27+
public function inspect()
2828
{
2929
$error = new Error();
30-
$error->error = $invalidValue->error;
31-
$error->processingPath = $invalidValue->path;
30+
$error->error = $this->error;
31+
$error->processingPath = $this->path;
3232
$error->dataPointer = PointerUtil::getDataPointer($error->processingPath);
3333
$error->schemaPointers = PointerUtil::getSchemaPointers($error->processingPath);
34-
if ($invalidValue instanceof LogicException) {
35-
foreach ($invalidValue->subErrors as $nestedError) {
36-
$error->subErrors[] = self::inspect($nestedError);
34+
if ($this instanceof LogicException) {
35+
foreach ($this->subErrors as $subError) {
36+
$error->subErrors[] = $subError->inspect();
3737
}
3838
}
3939
return $error;
4040
}
41+
42+
public function getSchemaPointer()
43+
{
44+
return PointerUtil::getSchemaPointer($this->path);
45+
}
46+
47+
public function getDataPointer()
48+
{
49+
return PointerUtil::getDataPointer($this->path);
50+
}
51+
4152
}

tests/src/PHPUnit/Error/ErrorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ public function testErrorMessage()
169169
$this->fail('Exception expected');
170170
} catch (InvalidValue $exception) {
171171
$this->assertSame($expectedException, $exception->getMessage());
172-
$error = InvalidValue::inspect($exception);
172+
$error = $exception->inspect();
173173
$this->assertSame($errorInspected, print_r($error, 1));
174+
$this->assertSame('/properties/root/patternProperties/^[a-zA-Z0-9_]+$', $exception->getSchemaPointer());
175+
$this->assertSame('/root/zoo', $exception->getDataPointer());
174176
}
175177
}
176178

0 commit comments

Comments
 (0)