Skip to content

Commit af527e6

Browse files
authored
Zodify all the things (#249)
1 parent 037c03f commit af527e6

29 files changed

+579
-1258
lines changed

package-lock.json

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

packages/vscode-container-client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"dependencies": {
2626
"dayjs": "^1.11.2",
27-
"tree-kill": "^1.2.2"
27+
"tree-kill": "^1.2.2",
28+
"zod": "^3.25.56"
2829
},
2930
"devDependencies": {
3031
"@types/chai": "^4.3.0",
@@ -39,7 +40,7 @@
3940
"mocha-junit-reporter": "^2.0.2",
4041
"ts-node": "^10.4.0",
4142
"tsconfig-paths": "^4.0.0",
42-
"typescript": "^4.5.5",
43+
"typescript": "^5.8.3",
4344
"vscode-jsonrpc": "^8.0.1"
4445
},
4546
"mocha": {

packages/vscode-container-client/src/clients/DockerClient/DockerClient.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { asIds } from "../../utils/asIds";
99
import { CommandLineArgs, composeArgs, withArg } from "../../utils/commandLineBuilder";
1010
import { DockerClientBase } from "../DockerClientBase/DockerClientBase";
1111
import { withDockerJsonFormatArg } from "../DockerClientBase/withDockerJsonFormatArg";
12-
import { isDockerContextRecord } from "./DockerContextRecord";
13-
import { isDockerInspectContextRecord } from "./DockerInspectContextRecord";
12+
import { DockerContextRecordSchema } from "./DockerContextRecord";
13+
import { DockerInspectContextRecordSchema } from "./DockerInspectContextRecord";
1414

1515
export class DockerClient extends DockerClientBase implements IContainersClient {
1616
/**
@@ -67,13 +67,7 @@ export class DockerClient extends DockerClientBase implements IContainersClient
6767
return;
6868
}
6969

70-
const rawContext = JSON.parse(contextJson);
71-
72-
// Validate that the image object matches the expected output
73-
// for the list contexts command
74-
if (!isDockerContextRecord(rawContext)) {
75-
throw new Error('Invalid context JSON');
76-
}
70+
const rawContext = DockerContextRecordSchema.parse(JSON.parse(contextJson));
7771

7872
contexts.push({
7973
name: rawContext.Name,
@@ -172,11 +166,7 @@ export class DockerClient extends DockerClientBase implements IContainersClient
172166
}
173167

174168
try {
175-
const inspect = JSON.parse(inspectString);
176-
177-
if (!isDockerInspectContextRecord(inspect)) {
178-
throw new Error('Invalid context inspect json');
179-
}
169+
const inspect = DockerInspectContextRecordSchema.parse(JSON.parse(inspectString));
180170

181171
// Return the normalized InspectVolumesItem record
182172
const volume: InspectContextsItem = {

packages/vscode-container-client/src/clients/DockerClient/DockerContextRecord.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,11 @@
33
* Licensed under the MIT License. See LICENSE in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
export type DockerContextRecord = {
7-
Name: string;
8-
Current: boolean;
9-
Description?: string;
10-
DockerEndpoint?: string;
11-
};
12-
13-
export function isDockerContextRecord(maybeContext: unknown): maybeContext is DockerContextRecord {
14-
const context = maybeContext as DockerContextRecord;
15-
16-
if (!context || typeof context !== 'object') {
17-
return false;
18-
}
19-
20-
if (typeof context.Name !== 'string') {
21-
return false;
22-
}
23-
24-
if (typeof context.Current !== 'boolean') {
25-
return false;
26-
}
27-
28-
return true;
29-
}
6+
import { z } from 'zod/v4';
7+
8+
export const DockerContextRecordSchema = z.object({
9+
Name: z.string(),
10+
Current: z.boolean(),
11+
Description: z.string().optional(),
12+
DockerEndpoint: z.string().optional(),
13+
});

packages/vscode-container-client/src/clients/DockerClient/DockerInspectContextRecord.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
* Licensed under the MIT License. See LICENSE in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
export type DockerInspectContextRecord = {
7-
Name: string;
8-
Metadata?: {
9-
Description?: string;
10-
}
11-
};
6+
import { z } from 'zod/v4';
127

13-
// TODO: Actually test properties
14-
export function isDockerInspectContextRecord(maybeContext: unknown): maybeContext is DockerInspectContextRecord {
15-
return true;
16-
}
8+
export const DockerInspectContextRecordSchema = z.object({
9+
Name: z.string(),
10+
Metadata: z.object({
11+
Description: z.string().optional(),
12+
}).optional(),
13+
});

0 commit comments

Comments
 (0)