The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started




Json Schema allows references so you don’t need to repeat yourself. For example:

  "$schema": "",
  "definitions": {
    "pet": {
      "type": "object",
      "properties": {
        "name":  { "type": "string" },
        "breed": { "type": "string" },
        "age":  { "type": "string" }
      "required": ["name", "breed", "age"]
  "type": "object",
  "properties": {
    "cat": { "$ref": "#/definitions/pet" },
    "dog": { "$ref": "#/definitions/pet" }

References contain a URI, and use JSON Pointer. References need to be resolved before the validator can be used.


To dereference your schema, create a new Dereferencer instance.


$deref  = new League\JsonGuard\Dereferencer();

Now call the dereference method with your schema. The schema should be the result from a json_decode call.


$schema = json_decode('{"properties": { "username": {"type": "string"}, "login": {"$ref": "#/properties/username"} } }');
$schema = $deref->dereference($schema);

The resulting object is identical, but references have been replaced with Reference objects.


Alternatively, you can provide the dereferencer with a path to load the schema from.


$schema = $deref->dereference('');

By default http://, https://, and file:// paths are supported.

Custom Loaders

You can make your own loaders by implementing the Loader Interface. Imagine you may want to load schemas from a CouchDb database, and your references look like this:

{ "$ref":"couchdb://00a271787f89c0ef2e10e88a0c0001f4" }

Once you have written your custom loader, you can register it with the dereferencer’s LoaderManager. The first argument should be the loader instance, and the second argument should be the prefix you would like to load references for.


use My\App\CouchDbLoader;

$couchLoader = new CouchDbLoader();
$deref  = new League\JsonGuard\Dereferencer();

$deref->getLoaderManager()->registerLoader($couchLoader, 'couchdb');