Skip to content

Commit

Permalink
fix: make prop def getter backward-compatible
Browse files Browse the repository at this point in the history
Return early for non-nested properties as before,
and undefined instead of erroring out when the
property type is undefined.
  • Loading branch information
biniam committed Feb 23, 2019
1 parent 93e49f1 commit 95b907b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
18 changes: 11 additions & 7 deletions lib/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (propPath.length === 1) return prop;

if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) {
throw new Error(g.f('Invalid property path'));
return undefined;
}

if (propPath.length === 1) return prop;

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

/**
Expand Down
17 changes: 11 additions & 6 deletions test/connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});

0 comments on commit 95b907b

Please sign in to comment.