Skip to content
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

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

zerkms
Copy link
Contributor

@zerkms zerkms commented Sep 25, 2024

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

@coveralls
Copy link

coveralls commented Sep 25, 2024

Pull Request Test Coverage Report for Build 11711748423

Details

  • 47 of 47 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 98.696%

Totals Coverage Status
Change from base Build 11369981368: 0.005%
Covered Lines: 5375
Relevant Lines: 5446

💛 - Coveralls

a) the value at the path is of the wrong type
b) the path does not exist

.
@zerkms zerkms force-pushed the 494-lack-of-key-error-message branch from 40639a4 to 93af1e7 Compare September 25, 2024 02:44
@@ -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;
Copy link
Collaborator

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)

Copy link
Contributor Author

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?

Copy link
Collaborator

@veewee veewee Nov 6, 2024

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'.

Copy link
Contributor Author

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.

Copy link
Collaborator

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?

src/Psl/Type/Exception/AssertException.php Outdated Show resolved Hide resolved
in AssertException and CoercionException
@zerkms zerkms force-pushed the 494-lack-of-key-error-message branch from 8efa40c to 6b02ca3 Compare October 28, 2024 21:03
@zerkms zerkms force-pushed the 494-lack-of-key-error-message branch from df72221 to 8915096 Compare November 6, 2024 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New v3 path error message should distinguish between "lack of key" and "key equals null"
3 participants