-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added different message to distinguish between two cases: #495
base: next
Are you sure you want to change the base?
Conversation
Pull Request Test Coverage Report for Build 11711748423Details
💛 - Coveralls |
a) the value at the path is of the wrong type b) the path does not exist .
40639a4
to
93af1e7
Compare
@@ -17,18 +17,28 @@ final class AssertException extends Exception | |||
/** | |||
* @param list<string> $paths | |||
*/ | |||
private function __construct(string $actual, string $expected, array $paths = [], ?Throwable $previous = null) | |||
private function __construct(?string $actual, string $expected, array $paths = [], ?Throwable $previous = null) | |||
{ | |||
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be "null"
on nested shapes where one of the underlying keys does not exist? (given that you pass null as a string to the parent exception?
(same comment for CoercionException)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what it means, could you expand? If the "outer" shape does not match - no "nested" shapes will be checked, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example:
shape([
'a' => shape([
'b' => mixed()
]),
])->coerce(['a' => []]);
This will result in:
Could not coerce "null" to type "array{'a': array{'b': mixed}}" at path "a.b"
-> Previous: Could not coerce to type "array{'b': mixed}" at path "b" as the value was not passed.
If the "outer" shape does not match - no "nested" shapes will be checked, right?
The types will coerce top-down indeed. But if one of the nested children results in a failure, the exception gets wrapped and enriched with the "path" information. It's that $first
logic inside the exception constructor which you default to ?? 'null'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, now I see that CoercionException
and AssertException
should be monadic. How about now?
Only for CoercionException
yet, as a proof-of-concept.
And a mandatory reminder: this is still under heavy WIP, I understand it's not a trivial change and I will clean things up during the final git squash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think wrap()
-ing it is the way:
vec(
shape([
'a' => shape(['b' => mixed()])
])
)->coerce([['a' => []]]);
Could not coerce "null" to type "vec<array{'a': array{'b': mixed}}>" at path "0.a.b"
The parent's exception construction should be more aware of those details from within the child's type exception.
Maybe an alternate suggestion would to make the $actual
and $first
property nullable for those situations where the value is not available and let that decide the message instead?
in AssertException and CoercionException
8efa40c
to
6b02ca3
Compare
df72221
to
8915096
Compare
a) the value at the path is of the wrong type
b) the path does not exist
Fixes #494
IMPORTANT: this is still "work-in-progress" and merely a conversation opener with the aim to find out better wording (not sure I like it like that, but it's the best I could think of at the moment).