Skip to content

Commit

Permalink
handling inline references in the main schema even when subSchemas ar…
Browse files Browse the repository at this point in the history
…e provided
  • Loading branch information
bardzusny committed Mar 2, 2015
1 parent 2db9269 commit dd68e6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/enjoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function enjoi(schema, subSchemas) {
id = value.substr(0, value.indexOf('#') + 1);
path = value.substr(value.indexOf('#') + 1);

if (id && subSchemas) {
if (id && id.length > 1 && subSchemas) {
refschema = subSchemas[id] || subSchemas[id.substr(0, id.length - 1)];
}
else {
Expand Down
32 changes: 32 additions & 0 deletions test/test-enjoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@ Test('enjoi', function (t) {
});
});

t.test('with both inline and external refs', function (t) {
t.plan(1);

var schema = Enjoi({
'title': 'Example Schema',
'type': 'object',
'properties': {
'firstname': {
'$ref': '#/definitions/firstname'
},
'surname': {
'$ref': 'definitions#/surname'
}
},
'definitions': {
'firstname': {
'type': 'string'
}
}
}, {
'definitions': {
'surname': {
'type': 'string'
}
}
});

Joi.validate({firstname: 'Joe', surname: 'Doe'}, schema, function (error, value) {
t.ok(!error, 'no error.');
});
});

});

Test('types', function (t) {
Expand Down

1 comment on commit dd68e6c

@larrymyers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the id.length > 1 check seems to have broken swaggerize-hapi parsing of a swagger spec to generation the validations.

In this case "value" passed to refresolver:

/definitions/Workflow

Since the id is "#' it doesn't use subschemas. This means it never finds a fragment, so fragment is returned from refresolver as "undefined", which means the "current" arg passed to resolver is "undefined", so then trying to evaluate "currrent.type" in the first conditional check causes the following error:

TypeError: Cannot read property 'type' of undefined

Please sign in to comment.