Skip to content

Commit 2ffa3b1

Browse files
authored
Merge pull request #147 from snyk/fix/allow-skipping-validation
fix: allow skipping validation
2 parents b9e4b3c + 5498faa commit 2ffa3b1

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/core/create-from-json.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ export const SUPPORTED_SCHEMA_RANGE = '^1.0.0';
1313
* Create a DepGraph instance from a JSON representation of a dep graph. This
1414
* is typically used after passing the graph over the wire as `DepGraphData`.
1515
*/
16-
export function createFromJSON(depGraphData: DepGraphData): DepGraph {
17-
assertValidSchema(depGraphData);
16+
export function createFromJSON(
17+
depGraphData: DepGraphData,
18+
shouldValidate = true,
19+
): DepGraph {
20+
if (shouldValidate) {
21+
assertValidSchema(depGraphData);
22+
}
1823

1924
const graph = new graphlib.Graph({
2025
directed: true,
@@ -25,21 +30,24 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
2530
const pkgNodes: { [pkgId: string]: Set<string> } = {};
2631

2732
for (const { id, info } of depGraphData.pkgs) {
28-
assertValidPkg(id, info, pkgs);
33+
if (shouldValidate) {
34+
assertValidPkg(id, info, pkgs);
35+
}
2936
pkgs[id] = info.version ? info : { ...info, version: undefined };
3037
}
3138

3239
const rootNodeId = depGraphData.graph.rootNodeId;
3340
let rootNode;
3441

3542
for (const node of depGraphData.graph.nodes) {
36-
assert(!graph.hasNode(node.nodeId), 'more than one node with same id');
37-
43+
if (shouldValidate) {
44+
assert(!graph.hasNode(node.nodeId), 'more than one node with same id');
45+
assert(
46+
!!pkgs[node.pkgId],
47+
'some instance nodes belong to non-existing pkgIds',
48+
);
49+
}
3850
if (node.nodeId === rootNodeId) rootNode = node;
39-
assert(
40-
!!pkgs[node.pkgId],
41-
'some instance nodes belong to non-existing pkgIds',
42-
);
4351
const pkgId = node.pkgId;
4452
if (!pkgNodes[pkgId]) {
4553
pkgNodes[pkgId] = new Set();
@@ -55,11 +63,13 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
5563
}
5664
}
5765

58-
assert(!!rootNode, `.${rootNodeId} root graph node is missing`);
59-
const rootPkgId = rootNode.pkgId;
60-
assert(!!pkgs[rootPkgId], `.${rootPkgId} root pkg missing`);
66+
if (shouldValidate) {
67+
assert(!!rootNode, `.${rootNodeId} root graph node is missing`);
68+
const rootPkgId = rootNode.pkgId;
69+
assert(!!pkgs[rootPkgId], `.${rootPkgId} root pkg missing`);
6170

62-
validateGraph(graph, depGraphData.graph.rootNodeId, pkgs, pkgNodes);
71+
validateGraph(graph, depGraphData.graph.rootNodeId, pkgs, pkgNodes);
72+
}
6373

6474
return new DepGraphImpl(
6575
graph,

0 commit comments

Comments
 (0)