nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Json::validate as an equivalent of json_validate #317

Open Marian-Kechlibar opened 1 week ago

Marian-Kechlibar commented 1 week ago

PHP 8.3 comes with a new function json_validate which only tests if a string is valid JSON without actually parsing it into objects. I believe it would be nice to have Nette\Utils\Json::validate as well, even if it was just a simple wrapper around json_validate, mostly for aesthetic purposes.

Currently, you must do a try-catch around Json::decode in order to validate a JSON. This sort-of breaks the visual flow of the code, plus is somewhat more expensive than Json::validate would be, if your only need is the validation, and you don't really need the fully parsed result.

Possibly, Json::validate could act more like a pure function and return a list of encountered problems instead of modifying the global state, which is what json_validate does.