vivocha / jsonref

A simple Javascript library implementing the JSON Reference and the JSON Pointer specifications.
MIT License
13 stars 3 forks source link

jsonref

A simple Javascript library implementing the JSON Reference and the JSON Pointer specifications.

travis build Coverage Status npm version

Install

$ npm install jsonref

parse(dataOrUri [, options])

The function returns a Promise resolving to the parsed data, with all $ref instances resolved.

Sample browser-friendly retriever using fetch

function retriever(url) {
  var opts = {
    method: 'GET',
    credentials: 'include'
  };
  return fetch(url, opts).then(function(response) {
    return response.json();
  });
}

Sample node.js retriever using request

var request = require('request');

function retriever(url) {
  return new Promise(function(resolve, reject) {
    request({
      url: url,
      method: 'GET',
      json: true
    }, function(err, response, data) {
      if (err) {
        reject(err);
      } else if (response.statusCode !== 200) {
        reject(response.statusCode);
      } else {
        resolve(data);
      }
    });
  });
}

pointer(data, path [, value])

Returns the data requested or sets a new value at the specified path

Examples

Parsing an object with no external references

var jsonref = require('jsonref');

var schema = {
  "id": "http://my.site/myschema#",
  "definitions": {
    "schema1": {
      "id": "schema1",
      "type": "integer"
    },
    "schema2": {
      "type": "array",
      "items": { "$ref": "schema1" }
    }
  }
}

jsonref.parse(schema).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
});

The output is:

{
  "id": "http://my.site/myschema#",
  "definitions": {
    "schema1": {
      "id": "schema1",
      "type": "integer"
    },
    "schema2": {
      "type": "array",
      "items": {
        "id": "schema1",
        "type": "integer"
      }
    }
  }
}

Parsing an object with external references

var jsonref = require('jsonref');

var schema = {
  "allOf": [
    { "$ref": "http://json-schema.org/draft-04/schema#" },
    {
      "type": "object",
      "properties": {
        "documentation": {
          "type": "string"
        }
      }
    }
  ]
}

jsonref.parse(schema, {
  retriever: retriever
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
});

The library will call retriever("http://json-schema.org/draft-04/schema#") to download the external reference. If no retriever is passed, the returned value is a rejected Promise, with a no_retriever exception.

Parsing an object using a custom store

var jsonref = require('jsonref');

var store = {};
var schema = {
  "id": "http://my.site/myschema#",
  "definitions": {
    "schema1": {
      "id": "schema1",
      "type": "integer"
    },
    "schema2": {
      "type": "array",
      "items": { "$ref": "schema1" }
    }
  }
}

jsonref.parse(schema, {
  store: store
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
});

After parsing, the contents of the store are:

{
  "http://my.site/myschema#": {
    "id": "http://my.site/myschema#",
    "definitions": {
      "schema1": {
        "id": "schema1",
        "type": "integer"
      },
      "schema2": {
        "type": "array",
        "items": {
          "id": "schema1",
          "type": "integer"
        }
      }
    }
  },
  "http://my.site/schema1#": {
    "id": "schema1",
    "type": "integer"
  }
}