-
Notifications
You must be signed in to change notification settings - Fork 145
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
Miss assertJson
like in phpunit
#217
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. |
why assert then?
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;
}
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
why not assert? what else to do there? ignore invalid json? edit: |
In this code you must check the |
what if i prefer to check it before and don't mind the double decode? edit: |
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 ? |
No description provided.
The text was updated successfully, but these errors were encountered: