1.2.0 • Published 7 years ago

jsref v1.2.0

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

jsref

Fast, flexible and Tiny (>1kb min) json reference resolver with support for json pointers, external refernences, custom resolvers and path or key based filters.

Usage

On NodeJS

$ npm install jsref --save

var jsref = require('jsref')

var inp = { k1: 'v1', k2: { $ref: '#k1' } }
var res = jsref(inp, { lazy:true } )

In Browser

<script src="https://cdn.rawgit.com/mattaylor/jsref/master/jsref.min.js"></script>
<script>
  var inp = { k1: 'v1', k2: { $ref: '#k1' } }
  var res = jsref(inp, { lazy:true } )
</script>

NOTE: local references must be prefixed by #. Old style JSON schema references eg { $ref: 'string' } will be resolved as remote url paths

Options

ParamDescrptionDefault
rootUrl host and path prefix to prepend remote referenceshttp://localhost/
refsObject to use to store shared references{}
keysArray of object keys to expand (if defined), otherwise expand all keysnull
deepRecursively de-reference remote referencesfalse
fragJSON Pointer Fragment identifier to extract from external resultsnull
findFunction that takes a remote reference and returns a promise of the resulthttp fetch and extract json
$refProperty name used to identify reference values$ref
lazyReturn immediately using promises only where necessary for external reference, (otherwise return as promise to fully resolved object)false
httpHTTP options to pass to fetch when resolving remote referencesnull
pathRegexp pattern that must against refs paths (if defined) to restric resolutionnull

Examples

No Options

var jsref = require('jsref')

var ob1 = {
  foo: { id: 'foobar', value: 'bar' },
  new: { $ref: '#/foo/id'    },
  dot: { $ref: '#foo.id' },
  fot: { $ref: "http://json-schema.org/address" },
  bar: { $ref: "http://json-schema.org/address#description" }
}

jsref(ob1).catch(console.log).then(res => { /* do something */ })

With Options

var jsref = require('jsref')

var ob2 = {
  a: [ { $ref: 'topic/topic1'}, { $ref: '#c.a' } ], 
  b: { $ref: 'topic/topic1' }, 
  c: { a: 'hello', b: { $ref: '#a.0' }  }
}

var opts = { 
  root: 'http://avowt.com:7511/api/1.0/avowt/', 
  deep: true,
  keys: ['a']
  path: 'topic',
  refs: { 'realm/1': { name: 'realm1' } },
  frag: 'result._source'
}

jsref(ob2, opts).catch(console.log).then(res => { /* do something */ })

Custom Find

var search = require('elasticsearch')
var client = new search.Client({host: 'localhost:9200'})
var jsref  = require('jsref')

var ob2 = {
  a: [ { $ref: 'topic/topic1'}, { $ref: '#c.a' } ], 
  b: { $ref: 'topic/topic1' },
  c: { a: 'hello', b: { $ref: '#a.0' }  }
}

var opts = { 
  deep: true,
  find: (url) => {
    var [type, id] = url.split('/')
    return client.get({index:'myIndex', type: type, id: id})
  }
}

jsref(ob2, opts).catch(console.log).then(res => { /* do something */ })
1.2.0

7 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.9

8 years ago

1.0.888888888

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago