Merge pull request #138 from strongloop/fix/reserve-bc-propdef

fix: return undefined for first level undefined props
This commit is contained in:
Biniam Admikew 2019-02-25 14:58:40 -05:00 committed by GitHub
commit 386e0413b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View File

@ -155,20 +155,24 @@ Connector.getNestedPropertyDefinition = function(definition, propPath) {
const isArray = !isPropUndefined && Array.isArray(prop.type); const isArray = !isPropUndefined && Array.isArray(prop.type);
const isFunction = !isPropUndefined && !isArray && typeof prop.type === 'function'; 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 (propPath.length === 1) return prop;
if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) {
return undefined;
}
const nextDefinition = const nextDefinition =
(isArray && prop.type[0].definition) || (isArray && prop.type[0].definition) ||
(isFunction && prop.type.definition); (isFunction && prop.type.definition);
return Connector.getNestedPropertyDefinition( if (nextDefinition === undefined) {
nextDefinition, return undefined;
propPath.slice(1) } else {
); return Connector.getNestedPropertyDefinition(
nextDefinition,
propPath.slice(1)
);
}
}; };
/** /**

View File

@ -96,12 +96,17 @@ describe('Connector', () => {
expect(propDefinition.type).to.equal(Date); expect(propDefinition.type).to.equal(Date);
}); });
it('should fail for non-existing properties', () => { it('should return undefined for non-existing nested property', () => {
expect(() => const definition = connector.getPropertyDefinition('MyModel',
connector.getPropertyDefinition( 'someProp.innerArray.foo');
'MyModel', // eslint-disable-next-line no-unused-expressions
'non.existing.property' expect(definition).to.be.undefined;
)).to.throw(/Invalid property path/); });
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;
}); });
}); });
}); });