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

ShapeType and CoercionException #208

Closed
vaclavvanik opened this issue Jun 11, 2021 · 3 comments
Closed

ShapeType and CoercionException #208

vaclavvanik opened this issue Jun 11, 2021 · 3 comments
Assignees
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Milestone

Comments

@vaclavvanik
Copy link
Contributor

I love coercing ShapeType. I can easily check if given payload is in expected format. However debugging complex ShapeType CoercionException is pain in the ass.

Let's see simple (complex) example:

<?php

use Psl\Type;

require_once __DIR__ . '/vendor/autoload.php';

$type = Type\shape([
    'response' => Type\shape([
        'object1' => Type\shape([
            'code' => Type\non_empty_string(),
        ]),
        'object2' => Type\shape([
            'code' => Type\non_empty_string(),
        ]),
    ]),
]);

$payload = [
    'response' => [
        'object1' => [
            'code' => '',
        ],
        'object2' => [
            'code' => 5,
        ],
    ]
];

$coerce = $type->coerce($payload);

This will throw CoercionException with message: Could not coerce "string" to type "non-empty-string".

It is really confusing I do not know which node is invalid.

I would suggest message like: Could not coerce (response->object1->code) "string" to type "non-empty-string".

@vaclavvanik vaclavvanik added the Type: Enhancement Most issues will probably ask for additions or changes. label Jun 11, 2021
@azjezz
Copy link
Owner

azjezz commented Jun 11, 2021

type exceptions contain a type trace, however, the type trace for shapes needs a bit of improvement.

example: https://phpsandbox.io/n/broad-sunset-wkem-c9oad

@azjezz
Copy link
Owner

azjezz commented Jun 11, 2021

another example: https://phpsandbox.io/n/broad-sunset-wkem-c9oad

one this that i think would be useful is to flip the frames by default.

@azjezz azjezz added this to the 1.9.0 milestone Oct 16, 2021
@azjezz azjezz modified the milestones: 1.9.0, 2.0.0 Oct 29, 2021
@azjezz azjezz added this to To do in v2.0.0 Nov 4, 2021
@azjezz azjezz removed this from To do in v2.0.0 May 7, 2022
@azjezz azjezz modified the milestones: 2.0.0, 2.1.0 May 7, 2022
@veewee
Copy link
Collaborator

veewee commented Apr 4, 2024

Fixed in #453

@veewee veewee closed this as completed Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

No branches or pull requests

3 participants