Skip to content

Commit 93d793c

Browse files
committed
Use json module for JSON validation in output-cache
1 parent c2fd8f5 commit 93d793c

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

lib/entry-points.js

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/output-cache.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path";
33

44
import { getTemporaryDirectory } from "../actions-util";
55
import { Env } from "../environment";
6+
import * as json from "../json";
67
import { Logger } from "../logging";
78

89
import type { VersionInfo } from "./types";
@@ -127,16 +128,16 @@ export function getCachedCodeQlVersion(
127128
* @param x The value to test
128129
*/
129130
function isVersionInfo(x: unknown): x is VersionInfo {
130-
const candidate = x as Partial<VersionInfo> | null;
131131
return (
132-
typeof candidate === "object" &&
133-
candidate !== null &&
134-
typeof candidate.version === "string" &&
135-
(candidate.features === undefined ||
136-
(typeof candidate.features === "object" &&
137-
candidate.features !== null)) &&
138-
(candidate.overlayVersion === undefined ||
139-
typeof candidate.overlayVersion === "number")
132+
json.isObject(x) &&
133+
json.validateSchema(
134+
{
135+
version: json.string,
136+
features: json.optional(json.object({})),
137+
overlayVersion: json.optional(json.number),
138+
} as const satisfies json.Schema,
139+
x,
140+
)
140141
);
141142
}
142143

@@ -145,12 +146,16 @@ function isVersionInfo(x: unknown): x is VersionInfo {
145146
* @param x The value to test
146147
*/
147148
function isOutputCache(x: unknown): x is OutputCache {
148-
const candidate = x as Partial<OutputCache> | null;
149149
return (
150-
typeof candidate === "object" &&
151-
candidate !== null &&
152-
typeof candidate.cmd === "string" &&
153-
candidate.entries !== undefined &&
154-
isVersionInfo(candidate.entries.version)
150+
json.isObject(x) &&
151+
json.validateSchema(
152+
{
153+
cmd: json.string,
154+
entries: json.object({}),
155+
} as const satisfies json.Schema,
156+
x,
157+
) &&
158+
json.isObject<{ version: unknown }>(x.entries) &&
159+
isVersionInfo(x.entries.version)
155160
);
156161
}

0 commit comments

Comments
 (0)