Merge pull request #138 from strongloop/fix/reserve-bc-propdef
fix: return undefined for first level undefined props
This commit is contained in:
commit
386e0413b5
|
@ -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);
|
||||||
|
|
||||||
|
if (nextDefinition === undefined) {
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
return Connector.getNestedPropertyDefinition(
|
return Connector.getNestedPropertyDefinition(
|
||||||
nextDefinition,
|
nextDefinition,
|
||||||
propPath.slice(1)
|
propPath.slice(1)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue