vivocha/jsonref

Name: jsonref

Owner: Vivocha

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

Created: 2016-03-14 14:44:02.0

Updated: 2017-08-29 08:54:36.0

Pushed: 2017-10-12 14:47:56.0

Homepage:

Size: 71

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

jsonref

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

travis build coveralls coverage npm version

Install
m 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
tion retriever(url) {
r opts = {
method: 'GET',
credentials: 'include'

turn fetch(url, opts).then(function(response) {
return response.json();
;

Sample node.js retriever using request
request = require('request');

tion retriever(url) {
turn 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
jsonref = require('jsonref');

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



ref.parse(schema).then(function(result) {
nsole.log(JSON.stringify(result, null, 2));

The output is:


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


Parsing an object with external references
jsonref = require('jsonref');

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



ref.parse(schema, {
triever: retriever
hen(function(result) {
nsole.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
jsonref = require('jsonref');

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



ref.parse(schema, {
ore: store
hen(function(result) {
nsole.log(JSON.stringify(result, null, 2));

After parsing, the contents of the store are:


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

ttp://my.site/schema1#": {
"id": "schema1",
"type": "integer"



This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.