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 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)
);
}
};
/**

View File

@ -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;
});
});
});