Comparing version 0.1.2 to 0.2.1
@@ -5,3 +5,3 @@ 'use strict'; | ||
$.cloneSchema = require('./util/clone-schema'); | ||
$.resolveSchema = require('./util/resolve-schema'); | ||
$.normalizeSchema = require('./util/normalize-schema'); | ||
@@ -24,17 +24,13 @@ | ||
function pushReference(from) { | ||
$.normalizeSchema(fakeroot, from); | ||
var fixed = $.normalizeSchema(fakeroot, from); | ||
var base = $.getDocumentURI(from.id); | ||
var base = $.getDocumentURI(fixed.id); | ||
if (!$ref.refs[base]) { | ||
$ref.refs[base] = from; | ||
$ref.refs[base] = fixed; | ||
} | ||
} | ||
if (Array.isArray(refs)) { | ||
refs.forEach(pushReference); | ||
} else { | ||
for (var key in refs) { | ||
pushReference(refs[key]); | ||
} | ||
for (var key in refs) { | ||
pushReference(refs[key]); | ||
} | ||
@@ -44,3 +40,3 @@ | ||
return $.cloneSchema(schema, $ref.refs, ex); | ||
return $.resolveSchema($.normalizeSchema(fakeroot, schema), $ref.refs, ex); | ||
} | ||
@@ -47,0 +43,0 @@ |
@@ -5,40 +5,69 @@ 'use strict'; | ||
var SCHEMA_URI = 'http://json-schema.org/schema'; | ||
var SCHEMA_URI = 'http://json-schema.org/schema#'; | ||
var normalizeSchema = module.exports = function(fakeroot, schema) { | ||
if (!schema.id) { | ||
throw new Error('Missing id for schema "' + JSON.stringify(schema) + '"'); | ||
function expand(obj, parent) { | ||
if (obj) { | ||
if (obj.$ref && !$.isURL(obj.$ref)) { | ||
obj.$ref = $.resolveURL(parent, obj.$ref); | ||
} | ||
if (typeof obj.id === 'string' && !$.isURL(obj.id)) { | ||
obj.id = $.resolveURL(parent === obj.id ? SCHEMA_URI : parent, obj.id); | ||
} | ||
} | ||
if (!schema.$schema) { | ||
schema.$schema = fakeroot || SCHEMA_URI; | ||
} else { | ||
schema.$schema = fakeroot || schema.$schema; | ||
for (var key in obj) { | ||
var value = obj[key]; | ||
if (typeof value === 'object') { | ||
expand(value, typeof obj.id === 'string' ? obj.id : parent); | ||
} | ||
} | ||
} | ||
if (!$.isURL(schema.id)) { | ||
schema.id = $.resolveURL(schema.$schema, schema.id); | ||
function clone(obj, seen) { | ||
var target = {}; | ||
seen = seen || []; | ||
if (seen.indexOf(obj) !== -1) { | ||
return target; | ||
} | ||
function expand(obj, parent) { | ||
if (parent) { | ||
if (obj.id) { | ||
normalizeSchema($.resolveURL(parent || SCHEMA_URI, obj.id), obj); | ||
} | ||
seen.push(obj); | ||
if (obj.$ref && !$.isURL(obj.$ref)) { | ||
obj.$ref = $.resolveURL(obj.id || parent || SCHEMA_URI, obj.$ref); | ||
} | ||
if (Array.isArray(obj)) { | ||
target = []; | ||
} | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
target[key] = typeof obj[key] === 'object' ? clone(obj[key], seen) : obj[key]; | ||
} | ||
} | ||
for (var key in obj) { | ||
var value = obj[key]; | ||
return target; | ||
} | ||
if (typeof value === 'object' && !(key === 'enum' || key === 'required')) { | ||
expand(value, obj.id || parent); | ||
} | ||
} | ||
module.exports = function(fakeroot, schema) { | ||
if (typeof fakeroot === 'object') { | ||
schema = fakeroot; | ||
fakeroot = null; | ||
} | ||
expand(schema); | ||
var copy = clone(schema); | ||
if (!copy.id) { | ||
copy.id = '#'; | ||
} | ||
if (!copy.$schema) { | ||
copy.$schema = fakeroot || SCHEMA_URI; | ||
} else { | ||
copy.$schema = $.resolveURL(copy.$schema, fakeroot || SCHEMA_URI); | ||
} | ||
expand(copy, $.resolveURL(copy.$schema, copy.id)); | ||
return copy; | ||
}; |
{ | ||
"name": "deref", | ||
"version": "0.1.2", | ||
"version": "0.2.1", | ||
"description": "JSON-Schema $ref resolution", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pateketrueke/deref" | ||
}, | ||
"devDependencies": { | ||
"clone": "^0.1.18", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-watch": "^0.6.1", | ||
"grunt-eslint": "^1.1.0", | ||
"grunt-jasmine-node": "^0.2.1", | ||
"tv4": "^1.1.4" | ||
"grunt-parts": "^0.3.0", | ||
"jayschema": "^0.3.1", | ||
"tv4": "^1.1.4", | ||
"z-schema": "^3.1.3" | ||
}, | ||
@@ -13,0 +18,0 @@ "dependencies": { |
@@ -8,5 +8,44 @@ Do you have $ref's ? | ||
var deref = require('deref'); | ||
``` | ||
var $ = deref(); | ||
Schema dereferencing | ||
-------------------- | ||
```javascript | ||
$ = deref(); | ||
var a = { | ||
id: 'a', | ||
type: 'object', | ||
properties: { | ||
b: { | ||
$ref: 'b' | ||
} | ||
} | ||
}; | ||
var b = { | ||
id: 'b', | ||
type: 'string' | ||
}; | ||
var c = { | ||
id: 'c', | ||
type: 'array', | ||
items: { | ||
$ref: 'a' | ||
} | ||
}; | ||
console.log($(c, [b, a]).id); | ||
// output: http://json-schema.org/c# | ||
console.log($(c, [b, a], true).items.properties.b.type); | ||
// output: string | ||
``` | ||
Schema normalization | ||
-------------------- | ||
```javascript | ||
var schema = { | ||
@@ -31,10 +70,11 @@ id: 'http://x.y.z/rootschema.json#', | ||
console.log($(schema).schema2.nested.id); | ||
console.log(deref.util.normalizeSchema(schema).schema2.nested.id); | ||
// output: http://x.y.z/otherschema.json#bar | ||
``` | ||
Basic usage | ||
=========== | ||
The resulting function of calling `deref()` can accept four arguments: | ||
The resulting function of calling `deref()` can accept three arguments: | ||
@@ -56,3 +96,3 @@ - **fakeroot** (string) | ||
- **refs** (array) | ||
- **refs** (array|object) | ||
@@ -91,3 +131,3 @@ Any additional schemas used while dereferencing. | ||
- `getDocumentURI(path)` | ||
- `cloneSchema(schema, refs, id)` | ||
- `resolveSchema(schema, refs)` | ||
- `normalizeSchema(fakeroot, schema)` | ||
@@ -97,2 +137,4 @@ | ||
[![Build Status](https://travis-ci.org/pateketrueke/deref.png?branch=master)](https://travis-ci.org/pateketrueke/deref) | ||
Since `0.2.1` the `$schema` and `id` are no longer required for schema normalization. | ||
[![Build Status](https://travis-ci.org/pateketrueke/deref.png?branch=master)](https://travis-ci.org/pateketrueke/deref) [![NPM version](https://badge.fury.io/js/deref.png)](http://badge.fury.io/js/deref) [![Coverage Status](https://coveralls.io/repos/pateketrueke/deref/badge.png?branch=master)](https://coveralls.io/r/pateketrueke/deref?branch=master) |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9465
228
136
0
6