Skip to content

Commit

Permalink
fixed #1 IDs in refs without root id
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jun 24, 2015
1 parent e7b0ee7 commit 1cbcade
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/ajv.js
Expand Up @@ -158,9 +158,9 @@ function Ajv(opts) {
else throw new Error(message);
}

resolve.ids.call(self, schema);
var localRefs = resolve.ids.call(self, schema);

var validate = compileSchema.call(self, schema);
var validate = compileSchema.call(self, schema, undefined, localRefs);
if (id[0] != '#') self._refs[id] = validate;
self._cache.put(str, validate);

Expand Down
8 changes: 7 additions & 1 deletion lib/compile/index.js
Expand Up @@ -12,7 +12,7 @@ var RULES = require('./rules')
module.exports = compile;


function compile(schema, root) {
function compile(schema, root, localRefs) {
var self = this
, refVal = [ undefined ]
, refs = {};
Expand Down Expand Up @@ -75,6 +75,12 @@ function compile(schema, root) {
}
var v = resolve.call(self, compile, root, ref);
if (v) return addLocalRef(ref, v);

var localSchema = localRefs[ref];
if (localSchema) {
var v = compile.call(self, localSchema, root, localRefs);
if (v) return addLocalRef(ref, v);
}
}

function addLocalRef(ref, v) {
Expand Down
51 changes: 29 additions & 22 deletions lib/compile/resolve.js
Expand Up @@ -117,29 +117,36 @@ function resolveUrl(baseId, id) {

function resolveIds(schema) {
var id = normalizeId(schema.id);
var localRefs = {};
_resolveIds.call(this, schema, getFullPath(id, false), id);
}


function _resolveIds(schema, fullPath, baseId) {
if (Array.isArray(schema))
for (var i=0; i<schema.length; i++)
_resolveIds.call(this, schema[i], fullPath+'/'+i, baseId);
else if (schema && typeof schema == 'object') {
if (typeof schema.id == 'string') {
var id = baseId = baseId
? url.resolve(baseId, schema.id)
: getFullPath(schema.id);

var refVal = this._refs[id];
if (typeof refVal == 'string') refVal = this._refs[refVal];
if (refVal && refVal.schema) {
if (!equal(schema, refVal.schema))
throw new Error('id "' + id + '" resolves to more than one schema');
} else if (id != normalizeId(fullPath))
this._refs[id] = fullPath;
return localRefs;

function _resolveIds(schema, fullPath, baseId) {
if (Array.isArray(schema))
for (var i=0; i<schema.length; i++)
_resolveIds.call(this, schema[i], fullPath+'/'+i, baseId);
else if (schema && typeof schema == 'object') {
if (typeof schema.id == 'string') {
var id = baseId = baseId
? url.resolve(baseId, schema.id)
: normalizeId(schema.id);

var refVal = this._refs[id];
if (typeof refVal == 'string') refVal = this._refs[refVal];
if (refVal && refVal.schema) {
if (!equal(schema, refVal.schema))
throw new Error('id "' + id + '" resolves to more than one schema');
} else if (id != normalizeId(fullPath)) {
if (id[0] == '#') {
if (localRefs[id] && !equal(schema, localRefs[id]))
throw new Error('id "' + id + '" resolves to more than one schema');
localRefs[id] = schema;
} else
this._refs[id] = fullPath;
}
}
for (var key in schema)
_resolveIds.call(this, schema[key], fullPath+'/'+escapeFragment(key), baseId);
}
for (var key in schema)
_resolveIds.call(this, schema[key], fullPath+'/'+escapeFragment(key), baseId);
}
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.5.12",
"version": "0.6.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions spec/json-schema.spec.js
Expand Up @@ -42,8 +42,7 @@ jsonSchemaTest([ ajv, fullAjv ], {
// 'schemas/complex', 'schemas/basic', 'schemas/advanced',
],
skip: [
'optional/zeroTerminatedFloats',
'schemas/complex'
'optional/zeroTerminatedFloats'
],
cwd: __dirname,
hideFolder: 'draft4/'
Expand Down
1 change: 0 additions & 1 deletion spec/tests/issues/1_ids_in_refs.json
@@ -1,6 +1,5 @@
[
{
"skip": true,
"description": "IDs in refs without root id (#1)",
"schema": {
"definitions": {
Expand Down

0 comments on commit 1cbcade

Please sign in to comment.