Skip to content

Commit

Permalink
issue #37
Browse files Browse the repository at this point in the history
  • Loading branch information
zaggino committed Jun 15, 2014
1 parent 9699cd9 commit 3fa5052
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/ZSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,17 @@
ZSchema.prototype._compileSchema = function (report, schema) {
var self = this;

// if schema is a string, assume it's a uri
if (typeof schema === 'string') {
// getRemoteSchema is sync when callback is not specified
var cachedSchema = Utils.getRemoteSchema(schema);
if (!cachedSchema) {
report.addError('SCHEMA_NOT_REACHABLE', {uri: schema});
} else {
schema = cachedSchema;
}
}

// reusing of compiled schemas
if (schema.__$compiled) {
return this.options.sync ? schema : Promise.resolve(schema);
Expand Down
28 changes: 28 additions & 0 deletions test/issue_37.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*jshint strict:false, loopfunc:true*/
/*global describe, it*/

var ZSchema = require("../src/ZSchema");
var assert = require("chai").assert;

describe("https://github.com/zaggino/z-schema/issues/37", function () {

var fileContent = "{ \"type\": \"array\" }";
var uri = "http://localhost:1234/array_schema.json";

it("should allow validation by uri", function (done) {
ZSchema.setRemoteReference(uri, fileContent);
ZSchema.validate([], uri, function (err, report) {
assert.isTrue(report.valid);
done();
});
});

it("should allow validation by uri - failcase", function (done) {
ZSchema.setRemoteReference(uri, fileContent);
ZSchema.validate("invalid", uri, function (err) {
assert.ok(err);
done();
});
});

});

0 comments on commit 3fa5052

Please sign in to comment.