diff --git a/src/js/Node.js b/src/js/Node.js index c6c88ad..21dee0c 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -2634,9 +2634,13 @@ Node._findSchema = function (schema, schemaRefs, path) { } } else if (typeof key === 'string' && childSchema.properties) { - childSchema = childSchema.properties[key] || null; - if (childSchema) { - foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath); + if (!(key in childSchema.properties)) { + foundSchema = null; + } else { + childSchema = childSchema.properties[key]; + if (childSchema) { + foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath); + } } } else if (typeof key === 'number' && childSchema.items) { @@ -2648,6 +2652,12 @@ Node._findSchema = function (schema, schemaRefs, path) { } } + + // If the found schema is the input schema, the schema does not have the given path + if (foundSchema === schema && path.length > 0) { + return null; + } + return foundSchema }; diff --git a/test/Node.test.js b/test/Node.test.js index 893201c..f083454 100644 --- a/test/Node.test.js +++ b/test/Node.test.js @@ -59,6 +59,26 @@ describe('Node', function () { ); }); + it('should return null for path that has no schema', function () { + var schema = { + type: 'object', + properties: { + foo: { + type: 'object', + properties: { + baz: { + type: 'number' + } + } + } + } + }; + var path = ['bar']; + assert.strictEqual(Node._findSchema(schema, {}, path), null); + path = ['foo', 'bar']; + assert.strictEqual(Node._findSchema(schema, {}, path), null); + }); + describe('with $ref', function () { it('should find a referenced schema', function () { var schema = { @@ -212,6 +232,36 @@ describe('Node', function () { 'second pattern property child' ); }); + + it('should return null for path that has no schema', function () { + var schema = { + type: 'object', + properties: { + levelTwo: { + type: 'object', + properties: { + levelThree: { + type: 'number' + } + } + } + }, + patternProperties: { + '^foo[0-9]': { + title: 'foo[0-9] pattern property', + type: 'string' + }, + '^bar[0-9]': { + title: 'bar[0-9] pattern property', + type: 'string' + } + } + }; + var path = ['not-in-schema']; + assert.strictEqual(Node._findSchema(schema, {}, path), null); + path = ['levelOne', 'not-in-schema']; + assert.strictEqual(Node._findSchema(schema, {}, path), null); + }); }); }); });