Skip to content
Snippets Groups Projects
Commit 5a188248 authored by Alvaro Cabrera's avatar Alvaro Cabrera
Browse files

Resolve given references; fix #294

parent 99ca0097
Branches
Tags
No related merge requests found
...@@ -2,12 +2,14 @@ import traverse from './traverse'; ...@@ -2,12 +2,14 @@ import traverse from './traverse';
import random from './random'; import random from './random';
import utils from './utils'; import utils from './utils';
import deref from 'deref';
function isKey(prop: string): boolean { function isKey(prop: string): boolean {
return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions'; return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions';
} }
// TODO provide types // TODO provide types
function run(schema: JsonSchema, container: Container) { function run(refs: any, schema: JsonSchema, container: Container) {
try { try {
return traverse(schema, [], function reduce(sub, maxReduceDepth) { return traverse(schema, [], function reduce(sub, maxReduceDepth) {
if (typeof maxReduceDepth === 'undefined') { if (typeof maxReduceDepth === 'undefined') {
...@@ -26,8 +28,15 @@ function run(schema: JsonSchema, container: Container) { ...@@ -26,8 +28,15 @@ function run(schema: JsonSchema, container: Container) {
if (typeof sub.$ref === 'string') { if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) { if (sub.$ref.indexOf('#/') === -1) {
throw new Error('Reference not found: ' + sub.$ref); var ref = deref.util.findByRef(sub.$ref, refs);
if (!ref) {
throw new Error('Reference not found: ' + sub.$ref);
}
return ref;
} }
// just remove the reference // just remove the reference
delete sub.$ref; delete sub.$ref;
return sub; return sub;
......
...@@ -13,7 +13,7 @@ function getRefs(refs?: any) { ...@@ -13,7 +13,7 @@ function getRefs(refs?: any) {
var $refs = {}; var $refs = {};
if (Array.isArray(refs)) { if (Array.isArray(refs)) {
refs.forEach(function(schema) { refs.map(deref.util.normalizeSchema).forEach(function(schema) {
$refs[schema.id] = schema; $refs[schema.id] = schema;
}); });
} else { } else {
...@@ -28,7 +28,7 @@ var jsf = function(schema: JsonSchema, refs?: any) { ...@@ -28,7 +28,7 @@ var jsf = function(schema: JsonSchema, refs?: any) {
var $refs = getRefs(refs); var $refs = getRefs(refs);
return run($(schema, $refs, true), container); return run($refs, $(schema, $refs, true), container);
}; };
jsf.resolve = <jsfAPI>function(schema: JsonSchema, refs?: any, cwd?: string) { jsf.resolve = <jsfAPI>function(schema: JsonSchema, refs?: any, cwd?: string) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment