jsonref

2019-08-15

Javascript References ($ref) and Pointers library

jsonref

A simple Javascript library implementing the JSON Referenceand the JSON Pointerspecifications.

travis buildCoverage Statusnpm version

Install

$ npm install jsonref

parse(dataOrUri _[, options]_)

  • dataOrUri, the data to parse or a fully qualified URI to pass to retrieverto download the data
  • options(optional), parsing options, the following optional properties are supported:
    • scope, the current resolution scope (base href) of URLs and paths.
    • store, an object to use to cache resolved idand $refvalues. If no store is passed, one is automatically created. Pass a storeif you are going to parse several objects or URIs referencing the same idand $refvalues.
    • retriever, a function accepting a URL in input and returning a promise resolved to an object representing the data downloaded for the URI. Whenever a $refto a new URI is found, if the URI is not already cached in the store in use, it'll be fetched using this retriever. If not retrieveris passed and a URI needs to be downloaded, a no_retrieverexception is thrown.

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

Sample browser-friendly retrieverusing fetch

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

Sample node.js retrieverusing 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]_)

  • data, the object to transverse using JSON Pointer.
  • path, either a string (#/prop1/prop2) or an array of path components ([ "#", "prop1", "prop2" ]or [ "prop1", "prop2" ]).
  • value, optional, value to set at path. All missing intermediate path levels are created as well.

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 retrieveris passed, the returned value is a rejected Promise, with a no_retrieverexception.

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"
  }
}

HomePage

https://github.com/vivocha/jsonref#readme

Repository

https://github.com/vivocha/jsonref.git


上一篇:New CSS Logical Properties! - The Next Step of CSS Evolution
下一篇:jsonpolice
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章
暂无相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部