Skip to content

Commit 2e327e9

Browse files
committed
feat(config): integrate Zod schema validation into config loading
- Replace type assertion with safeParse validation - Add error reporting for invalid config values - Import types from centralized config module
1 parent b5274fc commit 2e327e9

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import type { Plugin } from "@opencode-ai/plugin"
2-
import { createBuiltinAgents, type AgentName, type AgentOverrides } from "./agents"
2+
import { createBuiltinAgents } from "./agents"
33
import { createTodoContinuationEnforcer, createContextWindowMonitorHook, createSessionRecoveryHook } from "./hooks"
44
import { updateTerminalTitle } from "./features/terminal"
55
import { builtinTools } from "./tools"
6-
import { createBuiltinMcps, type McpName } from "./mcp"
6+
import { createBuiltinMcps } from "./mcp"
7+
import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "./config"
78
import * as fs from "fs"
89
import * as path from "path"
910

10-
interface OhMyOpenCodeConfig {
11-
disabled_mcps?: McpName[]
12-
disabled_agents?: AgentName[]
13-
agents?: AgentOverrides
14-
}
15-
1611
function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
1712
const configPaths = [
1813
path.join(directory, "oh-my-opencode.json"),
@@ -23,7 +18,18 @@ function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
2318
try {
2419
if (fs.existsSync(configPath)) {
2520
const content = fs.readFileSync(configPath, "utf-8")
26-
return JSON.parse(content) as OhMyOpenCodeConfig
21+
const rawConfig = JSON.parse(content)
22+
const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig)
23+
24+
if (!result.success) {
25+
console.error(`[oh-my-opencode] Config validation error in ${configPath}:`)
26+
for (const issue of result.error.issues) {
27+
console.error(` - ${issue.path.join(".")}: ${issue.message}`)
28+
}
29+
return {}
30+
}
31+
32+
return result.data
2733
}
2834
} catch {
2935
// Ignore parse errors, use defaults
@@ -184,5 +190,11 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
184190

185191
export default OhMyOpenCodePlugin
186192

187-
export type { AgentName, AgentOverrideConfig, AgentOverrides } from "./agents"
188-
export type { McpName } from "./mcp"
193+
export { OhMyOpenCodeConfigSchema } from "./config"
194+
export type {
195+
OhMyOpenCodeConfig,
196+
AgentName,
197+
AgentOverrideConfig,
198+
AgentOverrides,
199+
McpName,
200+
} from "./config"

0 commit comments

Comments
 (0)