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

property path format #462

Open
root-aza opened this issue Jan 5, 2024 · 5 comments
Open

property path format #462

root-aza opened this issue Jan 5, 2024 · 5 comments

Comments

@root-aza
Copy link

root-aza commented Jan 5, 2024

Hi! Is it possible to somehow change the formation of the property path?

Example:

 cities.0.name => cities.[0].name
@root-aza
Copy link
Author

root-aza commented Jan 8, 2024

@romm

@romm
Copy link
Member

romm commented Jan 8, 2024

Hi @root-aza, pinging me wont change the delay for the answer that I write during my sparetime, and is kind of a rude thing to do after such a short time. Please be careful about it 😉

Concerning your question, I don't really understand what is needed there: could you add a more detailed example of what should be changed?

@root-aza
Copy link
Author

root-aza commented Jan 8, 2024

Code example
<?php

declare(strict_types=1);

use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\Mapper\Tree\Message\Messages;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;

include 'vendor/autoload.php';


final readonly class Country
{
    /**
     * @param non-empty-string $name
     * @param list<City>       $cities
     */
    public function __construct(
        public string $name,
        public array $cities,
    ) {}
}

final readonly class City
{
    /**
     * @param non-empty-string       $name
     * @param DateTimeZone $timeZone
     */
    public function __construct(
        public string $name,
        public DateTimeZone $timeZone,
    ) {}
}

$json = <<<JSON
    {
        "cities": [
            {"name": "", "timeZone": "Europe/Paris"},
            {"name": "Lyon", "timeZone": "Europe/Paris"}
        ]
    }
JSON;

try {
    $country = (new \CuyZ\Valinor\MapperBuilder())
        ->mapper()
        ->map(Country::class, Source::json($json));

    echo $country->name; // France
    echo $country->cities[0]->name; // Paris
} catch (MappingError $error) {
    $list = new ConstraintViolationList();

    foreach (Messages::flattenFromNode($error->node())->errors() as $error){
        $node    = $error->node();
        $newPath = $node->path();

        $constraint = new ConstraintViolation(
            $error->toString(),
            messageTemplate: null,
            parameters: [],
            root: null,
            propertyPath: $node->path(),
            invalidValue: $node->sourceFilled() ? $node->sourceValue() : null,
            code: '72fb6435-c397-4dd2-a201-94c6b4973157'
        );


        $list->add($constraint);
    }
}

$node->path() returns a path like cities.0.name

but we want to get cities.[0].name

Why is this necessary?
To comply with the contract

@romm
Copy link
Member

romm commented Jan 8, 2024

If I understand correctly, this is needed by the ConstraintViolation class? Is there any documentation I can read about that?

@root-aza
Copy link
Author

root-aza commented Jan 8, 2024

If I understand correctly, this is needed by the ConstraintViolation class?

not really.

symfony/validator initially generates a path in the format cities.[0].name

And I would like monotony

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

No branches or pull requests

2 participants