From 95b907b157d85a876ceeff58681ce1621545357b Mon Sep 17 00:00:00 2001 From: biniam Date: Tue, 19 Feb 2019 16:39:42 -0500 Subject: [PATCH] fix: make prop def getter backward-compatible Return early for non-nested properties as before, and undefined instead of erroring out when the property type is undefined. --- lib/connector.js | 20 ++++++++++++-------- test/connector.test.js | 17 +++++++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/connector.js b/lib/connector.js index e5ed811..5154df7 100644 --- a/lib/connector.js +++ b/lib/connector.js @@ -155,20 +155,24 @@ Connector.getNestedPropertyDefinition = function(definition, propPath) { const isArray = !isPropUndefined && Array.isArray(prop.type); const isFunction = !isPropUndefined && !isArray && typeof prop.type === 'function'; - if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) { - throw new Error(g.f('Invalid property path')); - } - if (propPath.length === 1) return prop; + if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) { + return undefined; + } + const nextDefinition = (isArray && prop.type[0].definition) || (isFunction && prop.type.definition); - return Connector.getNestedPropertyDefinition( - nextDefinition, - propPath.slice(1) - ); + if (nextDefinition === undefined) { + return undefined; + } else { + return Connector.getNestedPropertyDefinition( + nextDefinition, + propPath.slice(1) + ); + } }; /** diff --git a/test/connector.test.js b/test/connector.test.js index cc5985c..0570e44 100644 --- a/test/connector.test.js +++ b/test/connector.test.js @@ -96,12 +96,17 @@ describe('Connector', () => { expect(propDefinition.type).to.equal(Date); }); - it('should fail for non-existing properties', () => { - expect(() => - connector.getPropertyDefinition( - 'MyModel', - 'non.existing.property' - )).to.throw(/Invalid property path/); + it('should return undefined for non-existing nested property', () => { + const definition = connector.getPropertyDefinition('MyModel', + 'someProp.innerArray.foo'); + // eslint-disable-next-line no-unused-expressions + expect(definition).to.be.undefined; + }); + + it('should preserve backward-compatibility for non-existing property', () => { + const definition = connector.getPropertyDefinition('MyModel', 'idontexist'); + // eslint-disable-next-line no-unused-expressions + expect(definition).to.be.undefined; }); }); });