@sap/cds-compiler
Version:
CDS (Core Data Services) compiler and backends
30 lines (25 loc) • 1.36 kB
JavaScript
;
// Only to be used with validator.js - a correct this value needs to be provided!
const { forEachMemberRecursively } = require('../model/csnUtils');
/**
* Asserts that there is no association usage outside of the specified service.
* We do not check in type-ofs - we resolve them, so they are not a problem.
*
* @param {CSN.Artifact} artifact Artifact to validate
* @param {string} artifactName Name of the artifact
*/
function assertNoAssocUsageOutsideOfService( artifact, artifactName ) {
if (artifact.kind !== 'entity' || this.csn.definitions[this.options.effectiveServiceName]?.kind !== 'service' ||
!artifactName.startsWith(`${ this.options.effectiveServiceName }.`))
return;
if (artifact.kind === 'entity' && (artifact.query || artifact.projection)) {
forEachMemberRecursively(artifact, (element, elementName, prop, path) => {
if (element && element.target && !element.target.startsWith(`${ this.options.effectiveServiceName }.`)) {
this.error('assoc-invalid-outside-service', path,
{ name: this.options.effectiveServiceName, id: elementName },
'Association $(ID) pointing outside of service $(NAME) must not be published');
}
}, [ 'definitions', artifactName ], true, { elementsOnly: true });
}
}
module.exports = assertNoAssocUsageOutsideOfService;