Skip to content

Commit

Permalink
fix(core): implement canonical dereferencing of external json schema …
Browse files Browse the repository at this point in the history
…fragments

Fixes playlyfe#3
  • Loading branch information
Johny Jose committed Jan 27, 2015
1 parent d891610 commit 768ebe3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/themis.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,19 @@ var Utils = {
rollback: function () { return; },

dereferencePath: function (path, schema_id, schemas) {
if (path[0] === "#") {
var hash_index, index, ref, parts;
hash_index = path.indexOf('#');
if (hash_index === 0) {
if (path === "#") { return schemas[schema_id]; }
var index, ref = schemas[schema_id];
var parts = path.slice(2).split("/").map(function (part) { return Utils.decodeJSONPointer(part); });
ref = schemas[schema_id];
parts = path.slice(2).split("/").map(function (part) { return Utils.decodeJSONPointer(part); });
for (index = 0; index < parts.length; index++) {
ref = ref[parts[index]];
}
return ref;
} else if (hash_index > 0) {
ref = schemas[path.slice(0, hash_index)];
parts = path.slice(hash_index + 2).split("/").map(function (part) { return Utils.decodeJSONPointer(part); });
for (index = 0; index < parts.length; index++) {
ref = ref[parts[index]];
}
Expand Down Expand Up @@ -1996,6 +2005,7 @@ var buildSchemas = function (schema_data, options) {
buffer[index] = [schemas[index], schema_id];
}


// Build refs
index = buffer.length;
while (buffer.length > 0) {
Expand Down
39 changes: 39 additions & 0 deletions test/pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Themis = require('../src/themis');

describe('Issue #3: Crashing when pattern is incorrect in ref(erenced) schema', function() {

it('should be able to dereference fragments of external schemas', function() {
var schemas = [
{
"id": "types",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"uuid": {
"type": "string",
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
}
}
},
{
"id": "kitchensink",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"id": { "$ref": "types#/definitions/uuid" }
}
}
];

var validator = Themis.validator(schemas);

var invalid_item = {
"id": "incorrect value"
};
var valid_item = {
"id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
}
validator(invalid_item, 'kitchensink').valid.should.be.false;
validator(valid_item, 'kitchensink').valid.should.be.true;
});

});

0 comments on commit 768ebe3

Please sign in to comment.