evgeny, jason: fixed incorrect root replacing and failure to resolve ref to another file root from inside ref, closes #12

master
Evgeny Poberezkin 2015-06-16 17:10:04 +01:00
parent 082284d9cc
commit e0d6afe7dc
4 changed files with 76 additions and 15 deletions

View File

@ -1,7 +1,8 @@
'use strict';
var url = require('url')
, equal = require('./equal');
, equal = require('./equal')
, util = require('./util');
module.exports = resolve;
@ -22,6 +23,7 @@ function resolve(compile, root, ref) {
var refVal = this._schemas[ref];
if (typeof refVal == 'function') return refVal;
var schema = _resolve.call(this, root, ref);
if (typeof schema == 'function') return this._refs[ref] = schema;
if (schema) return this._refs[ref] = compile.call(this, schema, root);
};
@ -38,10 +40,14 @@ function _resolve(root, ref) {
// if (this._refs[refVal]) refVal = this._refs[refVal];
// else refVal = resolve.call(this, rootSchema, refVal);
// }
if (typeof refVal == 'function') replaceRoot(root, refVal);
if (typeof refVal == 'function') root = refVal;
else {
var refVal = this._schemas[normalizeId(refPath)];
if (typeof refVal == 'function') replaceRoot(root, refVal);
var id = normalizeId(refPath);
var refVal = this._schemas[id];
if (typeof refVal == 'function') {
if (id == normalizeId(ref)) return refVal;
root = refVal;
}
}
if (!root.schema) return;
baseId = getFullPath(root.schema.id);
@ -68,13 +74,6 @@ function _resolve(root, ref) {
}
function replaceRoot(root, withRoot) {
root.schema = withRoot.schema;
root.refVal = withRoot.refVal;
root.refs = withRoot.refs;
}
function unescapeFragment(str) {
return decodeURIComponent(str)
.replace(/~1/g, '/')

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.4.10",
"version": "0.4.11",
"description": "Another JSON schema Validator",
"main": "lib/ajv.js",
"scripts": {

View File

@ -19,7 +19,7 @@ var ONLY_FILES, SKIP_FILES;
// 'schemas/complex',
// 'schemas/basic'
// 'schemas/advanced'
// 'issues/2_root_ref_in_ref'
'issues/12_restoring_root_after_resolve'
// ];
SKIP_FILES = [
@ -64,7 +64,7 @@ function addTests(description, testsPath) {
(skip ? describe.skip : describe) (file.name, function() {
var testSets = require(file.path);
testSets.forEach(function (testSet) {
// if (testSet.description != 'remote ref, containing refs itself') return;
// if (testSet.description != 'restoring root after ref resolution (#12)') return;
(testSet.skip ? describe.skip : describe)(testSet.description, function() {
var validate, fullValidate;
// it(testSet.description, function() {
@ -75,7 +75,7 @@ function addTests(description, testsPath) {
});
testSet.tests.forEach(function (test) {
// if (test.description != 'valid') return;
// if (test.description != 'valid number') return;
(test.skip ? it.skip : it)(test.description, function() {
var valid = validate(test.data);
// console.log('result', valid, test.valid, validate.errors);

View File

@ -0,0 +1,62 @@
[
{
"description": "restoring root after ref resolution (#12)",
"schema": {
"definitions": {
"int": { "$ref": "http://localhost:1234/integer.json" },
"str": { "type": "string" }
},
"anyOf": [
{ "$ref": "#/definitions/int" },
{ "$ref": "#/definitions/str" }
]
},
"tests": [
{
"description": "valid string",
"data": "foo",
"valid": true
},
{
"description": "valid number",
"data": 1,
"valid": true
},
{
"description": "invalid object",
"data": {},
"valid": false
}
]
},
{
"description": "all refs are in the same place",
"schema": {
"definitions": {
"int": { "type": "integer" },
"str": { "type": "string" }
},
"anyOf": [
{ "$ref": "#/definitions/int" },
{ "$ref": "#/definitions/str" }
]
},
"tests": [
{
"description": "valid string",
"data": "foo",
"valid": true
},
{
"description": "valid number",
"data": 1,
"valid": true
},
{
"description": "invalid object",
"data": {},
"valid": false
}
]
}
]