webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Miss `assertJson` like in phpunit

c33s opened this issue · comments

What would be the use case?

i don't really understand the question, well to test if a string is json is the use case and to not repeat myself using constructs based upon https://github.com/sebastianbergmann/phpunit/blob/e15dfe6ebddacd7ed093b008332f775a4cc077f3/src/Framework/Constraint/String/IsJson.php

in my case i store json in the database, so its not enough to check for string, it must be json so the database is able to query that field

But why? What you do with that string next? Do you deserialise it?

i make a doctrine database type for value objects which are stored as json in the database. the conversion should of course check if the json is valid.

why is the exact usecase so important? it is simply handy to assert for json it's a common type nowadays.

it would also be handy to assert for valid path, windows path, linux path, protocoll and much more.

the conversion should of course check if the json is valid.

why assert then?

why is the exact usecase so important? it is simply handy to assert for json it's a common type nowadays.

because I personally never use JSON without deserialising it 🤷

it's a generic doctrine type to store value objects in database, so the type must not care about the data inside the json but it must care that it's valid json.

    public function convertToPHPValue($value, AbstractPlatform $platform): ?ArrayInterface
    {
        Assert::nullOrString($value);
        $this->validateConfiguredValueObjectClass();
        if (null === $value) {
            return null;
        }
        Assert::true($this->isJson($value));

        /** @var ArrayInterface $class */
        $class = $this->valueObjectClass;

        return $class::fromArray(json_decode($value, true));
    }

    private function isJson($value)
    {
        if (!is_string($value)) {
            return false;
        }

        \json_decode($value);

        if (\json_last_error()) {
            return false;
        }

        return true;
    }

An empty string is not valid JSON

you took the code from the error description not from the matches method. https://github.com/sebastianbergmann/phpunit/blob/e15dfe6ebddacd7ed093b008332f775a4cc077f3/src/Framework/Constraint/String/IsJson.php#L35

don't know how phpunit handles the error desciption output and if it maybe automatically adds a Not to the build up string but the matches implemenation looks good to me.

why assert then?

why not assert? what else to do there? ignore invalid json?

edit:
simply missing a Assert::nullOrJson($value) method above

        Assert::true($this->isJson($value));

        /** @var ArrayInterface $class */
        $class = $this->valueObjectClass;

        return $class::fromArray(json_decode($value, true));

In this code you must check the json_decode return value. If you do - you'll see the json assertion would become redundant.

what if i prefer to check it before and don't mind the double decode?

edit:
for me this way i more readable

I don't see how it makes any sense, but point taken.

Hey Guys, If I take a JSON string as an input and want to validate it how would I do it through Assert ?

Also, can you guys give some example on extending Assert features ?