Fix Node._findSchema bug when path is not present in schema

Previously, the function would return the wrong schema when it was
unable to find a path in the given schema.
This commit is contained in:
Adam Vigneaux 2019-03-15 15:29:08 -04:00
parent f36cecdfde
commit 4f4a733fd6
No known key found for this signature in database
GPG Key ID: D6FE74FFE0A970E7
2 changed files with 63 additions and 3 deletions

View File

@ -2634,9 +2634,13 @@ Node._findSchema = function (schema, schemaRefs, path) {
} }
} }
else if (typeof key === 'string' && childSchema.properties) { else if (typeof key === 'string' && childSchema.properties) {
childSchema = childSchema.properties[key] || null; if (!(key in childSchema.properties)) {
if (childSchema) { foundSchema = null;
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath); } else {
childSchema = childSchema.properties[key];
if (childSchema) {
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
}
} }
} }
else if (typeof key === 'number' && childSchema.items) { 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 return foundSchema
}; };

View File

@ -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 () { describe('with $ref', function () {
it('should find a referenced schema', function () { it('should find a referenced schema', function () {
var schema = { var schema = {
@ -212,6 +232,36 @@ describe('Node', function () {
'second pattern property child' '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);
});
}); });
}); });
}); });