z-schema icon indicating copy to clipboard operation
z-schema copied to clipboard

`setSchemaReader` not handling references

Open reesew opened this issue 9 years ago • 0 comments

I'm trying to work with cached remote schemas synchronously, but setSchemaReader is having problems figuring out what to do with the references within those. Not sure how else to explain it except with an example.

The following works as expected

var interagentUrl = 'http://interagent.github.io/interagent-hyper-schema';
var interagentSchema = getSchemaFromUrl(interagentUrl); //synchronous, get from cached
var schema = getSchemaFromFile('schema.json');

validator.setRemoteReference(interagentUrl, interagentSchema);
var schemaValid = validator.validateSchema(schema);

But the documentation seems to suggest that I can use setSchemaReader to do something similar. The below hasn't worked because it parses the uri in the setSchemaReader function's argument as something like cachedSchemas/interagent.github.io/#/definitions/ref.

validator.setSchemaReader(function onSchemaReaderSet(uri) {
    var fileName = uri;
    if (uri.indexOf('http') === 0) {
      fileName = "./cachedSchemas/" + uri.substring(uri.indexOf('://') + 3);
    }
    console.log("fileName", fileName);
    return JSON.parse(fs.readFileSync(fileName, "utf8"));
}

How can I get it to try to read definitions/ref from the schema that is already loaded?

reesew avatar Feb 08 '16 19:02 reesew