getdkan/rooted-json-data

Access and modify JSON-based data objects while enforcing JSON Schema

0.2.1 2024-01-18 22:18 UTC

README

Build status Maintainability Test Coverage

Access and modify JSON-based data objects while enforcing JSON Schema.

This library primarily wires together JsonPath-PHP and Opis JSON Schema, providing a JSON Object class that functions like a small internal service. Data can be added, retrieved and modified through a simple API, and any changes will immediately provoke a re-validation. Validation errors through exceptions with actionable messages.

Example:

$json = '{"number":3}';
$schema = '{"type": "object","properties": {"number":{ "type": "number" }}}';
$data = new RootedJsonData($json, $schema);
echo $data->{"$.number"}; // 3
echo $data->{"$[number]"}; // 3
echo "{$data}"; // {"number":3}
$data->{"$.number"} = "three"; // EXCEPTION