Skip to content

Commit be207fe

Browse files
committed
fix: do not add UNRESOLVABLE_REFERENCE when REMOTE_NOT_VALID is present
1 parent be77f68 commit be207fe

File tree

7 files changed

+79
-6
lines changed

7 files changed

+79
-6
lines changed

Diff for: .jscs.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"requireSpacesInAnonymousFunctionExpression": { "beforeOpeningRoundBrace": true },
1010
"disallowSpacesInNamedFunctionExpression": { "beforeOpeningRoundBrace": true },
1111
"requireBlocksOnNewline": 1,
12-
"disallowEmptyBlocks": true,
1312
"disallowSpacesInsideArrayBrackets": "all",
1413
"disallowSpacesInsideParentheses": true,
1514
"requireSpacesInsideObjectBrackets": "all",

Diff for: .jshintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"latedef": true,
1212
"newcap": false,
1313
"noarg": true,
14-
"noempty": true,
14+
"noempty": false,
1515
"nonbsp": true,
1616
"nonew": true,
1717
"plusplus": false,

Diff for: src/Report.js

+22
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ Report.prototype.getPath = function () {
9797
return path;
9898
};
9999

100+
Report.prototype.hasError = function (errorCode, params) {
101+
var idx = this.errors.length;
102+
while (idx--) {
103+
if (this.errors[idx].code === errorCode) {
104+
// assume match
105+
var match = true;
106+
107+
// check the params too
108+
var idx2 = this.errors[idx].params.length;
109+
while (idx2--) {
110+
if (this.errors[idx].params[idx2] !== params[idx2]) {
111+
match = false;
112+
}
113+
}
114+
115+
// if match, return true
116+
if (match) { return match; }
117+
}
118+
}
119+
return false;
120+
};
121+
100122
Report.prototype.addError = function (errorCode, params, subReports, schemaDescription) {
101123
if (this.errors.length >= this.reportOptions.maxErrors) {
102124
return;

Diff for: src/SchemaCache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ exports.getSchemaByUri = function (report, uri, root) {
137137

138138
if (result && queryPath) {
139139
var parts = queryPath.split("/");
140-
for (var idx = 0, lim = parts.length; idx < lim; idx++) {
140+
for (var idx = 0, lim = parts.length; result && idx < lim; idx++) {
141141
var key = decodeJSONPointer(parts[idx]);
142142
if (idx === 0) { // it's an id
143143
result = findId(result, key);

Diff for: src/SchemaCompilation.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ exports.compileSchema = function (report, schema) {
239239

240240
if (!response) {
241241

242+
var hasNotValid = report.hasError("REMOTE_NOT_VALID", [refObj.ref]);
242243
var isAbsolute = Utils.isAbsoluteUri(refObj.ref);
243244
var isDownloaded = false;
244245
var ignoreUnresolvableRemotes = this.options.ignoreUnresolvableReferences === true;
@@ -249,7 +250,13 @@ exports.compileSchema = function (report, schema) {
249250
isDownloaded = SchemaCache.checkCacheForUri.call(this, refObj.ref);
250251
}
251252

252-
if (!isAbsolute || !isDownloaded && !ignoreUnresolvableRemotes) {
253+
if (hasNotValid) {
254+
// already has REMOTE_NOT_VALID error for this one
255+
} else if (ignoreUnresolvableRemotes && isAbsolute) {
256+
// ignoreUnresolvableRemotes is on and remote isAbsolute
257+
} else if (isDownloaded) {
258+
// remote is downloaded, so no UNRESOLVABLE_REFERENCE
259+
} else {
253260
Array.prototype.push.apply(report.path, refObj.path);
254261
report.addError("UNRESOLVABLE_REFERENCE", [refObj.ref]);
255262
report.path.slice(0, -refObj.path.length);

Diff for: test/ZSchemaTestSuite/Issue126.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
var REF_NAME = "int.json";
4+
5+
module.exports = {
6+
description: "Issue #126 - do not add UNRESOLVABLE_REFERENCE error when set through setRemoteReference",
7+
tests: [
8+
{
9+
description: "should fail compilation with unresolvable reference",
10+
setup: function (validator) {
11+
validator.setRemoteReference(REF_NAME, {
12+
"$schema": "http://json-schema.org/draft-04/schema#",
13+
"id": REF_NAME,
14+
"type": "object",
15+
"required": [
16+
"number"
17+
],
18+
"properties": {
19+
"number": {
20+
"$ref": "data-types.json#/definitions/number"
21+
}
22+
}
23+
});
24+
},
25+
schema: {
26+
"$schema": "http://json-schema.org/draft-04/schema#",
27+
"id": "ints.json",
28+
"type": "array",
29+
"items": {
30+
"$ref": REF_NAME
31+
}
32+
},
33+
validateSchemaOnly: true,
34+
valid: false,
35+
after: function (err) {
36+
err.forEach(function (e) {
37+
if (e.params.indexOf(REF_NAME) !== -1) {
38+
expect(e.code).not.toBe("UNRESOLVABLE_REFERENCE");
39+
}
40+
});
41+
}
42+
}
43+
]
44+
};

Diff for: test/spec/ZSchemaTestSuiteSpec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ var testSuiteFiles = [
6161
require("../ZSchemaTestSuite/Issue103.js"),
6262
require("../ZSchemaTestSuite/Issue106.js"),
6363
require("../ZSchemaTestSuite/Issue107.js"),
64+
require("../ZSchemaTestSuite/Issue126.js"),
6465
undefined
6566
];
6667

@@ -73,8 +74,8 @@ describe("ZSchemaTestSuite", function () {
7374
}
7475
}
7576

76-
it("should contain 55 files", function () {
77-
expect(testSuiteFiles.length).toBe(55);
77+
it("should contain 56 files", function () {
78+
expect(testSuiteFiles.length).toBe(56);
7879
});
7980

8081
testSuiteFiles.forEach(function (testSuite) {

0 commit comments

Comments
 (0)