generated from finos-labs/project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spectral rules to validate that interfaces are defined on the spe…
…cific node referenced (169) (#743) * 169 detect missing interfaces on node for architectures * 169 add equivalent rules for patterns * Lint and remove unused lint annotations * Fix tests and add link back to dev deps * Try rollup fix * Add install for rollup needed by spectral CLI * Fix package.json and gh action * Update package-lock
- Loading branch information
1 parent
c424cf6
commit 76de03e
Showing
11 changed files
with
276 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
"link:cli": "npm link --workspace cli" | ||
}, | ||
"devDependencies": { | ||
"link": "^2.1.1", | ||
"npm-run-all2": "^5.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
shared/src/spectral/functions/architecture/interface-id-exists-on-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
import { difference } from 'lodash'; | ||
|
||
/** | ||
* Checks that the input value exists as an interface with matching unique ID defined under a node in the document. | ||
*/ | ||
export function interfaceIdExistsOnNode(input, _, context) { | ||
if (!input || !input.interfaces) { | ||
return []; | ||
} | ||
|
||
if (!input.node) { | ||
return [{ | ||
message: 'Invalid connects relationship - no node defined.', | ||
path: [...context.path] | ||
}]; | ||
} | ||
|
||
const nodeId = input.node; | ||
const nodeMatch: object[] = JSONPath({ path: `$.nodes[?(@['unique-id'] == '${nodeId}')]`, json: context.document.data }); | ||
if (!nodeMatch || nodeMatch.length === 0) { | ||
// other rule will report undefined node | ||
return []; | ||
} | ||
|
||
// all of these must be present on the referenced node | ||
const desiredInterfaces = input.interfaces; | ||
|
||
const node = nodeMatch[0]; | ||
|
||
const nodeInterfaces = JSONPath({ path: '$.interfaces[*].unique-id', json: node }); | ||
if (!nodeInterfaces || nodeInterfaces.length === 0) { | ||
return [ | ||
{ message: `Node with unique-id ${nodeId} has no interfaces defined, expected interfaces [${desiredInterfaces}].` } | ||
]; | ||
} | ||
|
||
const missingInterfaces = difference(desiredInterfaces, nodeInterfaces); | ||
|
||
// difference always returns an array | ||
if (missingInterfaces.length === 0) { | ||
return []; | ||
} | ||
const results = []; | ||
|
||
for (const missing of missingInterfaces) { | ||
results.push({ | ||
message: `Referenced interface with ID '${missing}' was not defined on the node with ID '${nodeId}'.`, | ||
path: [...context.path] | ||
}); | ||
} | ||
return results; | ||
} |
53 changes: 53 additions & 0 deletions
53
shared/src/spectral/functions/pattern/interface-id-exists-on-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
import { difference } from 'lodash'; | ||
|
||
/** | ||
* Checks that the input value exists as an interface with matching unique ID defined under a node in the document. | ||
*/ | ||
export function interfaceIdExistsOnNode(input, _, context) { | ||
if (!input || !input.interfaces) { | ||
return []; | ||
} | ||
|
||
if (!input.node) { | ||
return [{ | ||
message: 'Invalid connects relationship - no node defined.', | ||
path: [...context.path] | ||
}]; | ||
} | ||
|
||
const nodeId = input.node; | ||
const nodeMatch: object[] = JSONPath({ path: `$.properties.nodes.prefixItems[?(@.properties['unique-id'].const == '${nodeId}')]`, json: context.document.data }); | ||
if (!nodeMatch || nodeMatch.length === 0) { | ||
// other rule will report undefined node | ||
return []; | ||
} | ||
|
||
// all of these must be present on the referenced node | ||
const desiredInterfaces = input.interfaces; | ||
|
||
const node = nodeMatch[0]; | ||
|
||
const nodeInterfaces = JSONPath({ path: '$.properties.interfaces.prefixItems[*].properties.unique-id.const', json: node }); | ||
if (!nodeInterfaces || nodeInterfaces.length === 0) { | ||
return [ | ||
{ message: `Node with unique-id ${nodeId} has no interfaces defined, expected interfaces [${desiredInterfaces}]` } | ||
]; | ||
} | ||
|
||
const missingInterfaces = difference(desiredInterfaces, nodeInterfaces); | ||
|
||
// difference always returns an array | ||
if (missingInterfaces.length === 0) { | ||
return []; | ||
} | ||
const results = []; | ||
|
||
for (const missing of missingInterfaces) { | ||
results.push({ | ||
message: `Referenced interface with ID '${missing}' was not defined on the node with ID '${nodeId}'.`, | ||
path: [...context.path] | ||
}); | ||
} | ||
return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters