Skip to content

feat: config validation#39

Merged
tongsh6 merged 2 commits intomainfrom
dev
Feb 11, 2026
Merged

feat: config validation#39
tongsh6 merged 2 commits intomainfrom
dev

Conversation

@tongsh6
Copy link
Owner

@tongsh6 tongsh6 commented Feb 11, 2026

Summary

Merge dev into main: config validation feature (PR #38).

  • 新增 validateConfig(raw) 函数,规范化任意配置输入
  • 共享类型 TrackerConfigToastConfigBudgetConfig
  • 重构 index.tsbin/opencode-tokens.ts 移除重复定义
  • 12 个新单元测试(共 31 个,全部通过,CI Node 18+22 green)

tongshuanglong and others added 2 commits February 11, 2026 13:39
- Add TrackerConfig, ToastConfig, BudgetConfig interfaces to lib/shared.ts
- Add validateConfig(raw) that normalizes arbitrary input to valid config
- Invalid fields silently corrected to defaults with warnings collected
- Validates types, non-negative numbers, ranges, Infinity/NaN rejection
- Refactor index.ts and bin/opencode-tokens.ts to use shared types/validation
- Remove duplicated Config interfaces and DEFAULT_CONFIG from consumers
- Plugin shows validation warnings via Toast; CLI prints to stderr
- Add 12 new unit tests for validateConfig (31 total, all passing)
Copilot AI review requested due to automatic review settings February 11, 2026 05:40
@tongsh6 tongsh6 merged commit 1110eec into main Feb 11, 2026
8 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces shared config normalization/validation so both the plugin and CLI can safely consume arbitrary JSON config, emitting user-facing warnings instead of failing at runtime.

Changes:

  • Added shared config types, DEFAULT_CONFIG, and validateConfig(raw) normalization in lib/shared.ts.
  • Refactored index.ts (plugin) and bin/opencode-tokens.ts (CLI) to use the shared validator and surface warnings (toast / stderr).
  • Added 12 unit tests covering validateConfig behavior.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
lib/shared.ts Adds shared config types/defaults and implements validateConfig + helpers.
index.ts Switches plugin config loading to validateConfig and displays config warnings via toast.
bin/opencode-tokens.ts Switches CLI config loading to validateConfig and prints config warnings to stderr.
test/shared.test.ts Adds unit tests for config validation/normalization and warning behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +346 to +351
if (obj["warnAt"] !== undefined) {
if (isFiniteNumber(obj["warnAt"]) && obj["warnAt"] > 0 && obj["warnAt"] <= 1) {
result.warnAt = obj["warnAt"]
} else {
warnings.push("budget.warnAt should be a number between 0 and 1 (exclusive-inclusive), using default")
}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

budget.warnAt validation currently requires warnAt > 0 && warnAt <= 1, but the PR description says the allowed range is 0–1. Either adjust the check to match the intended range (e.g., allow 0) or update the docs/CLI text accordingly. Also the warning message text "between 0 and 1 (exclusive-inclusive)" is confusing; consider rephrasing it to precisely match the implemented bounds.

Copilot uses AI. Check for mistakes.

if (raw === null || raw === undefined || typeof raw !== "object" || Array.isArray(raw)) {
warnings.push("Config is not a valid object, using defaults")
return { config: DEFAULT_CONFIG, warnings }
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateConfig returns DEFAULT_CONFIG by reference for invalid/non-object input. Because DEFAULT_CONFIG contains nested mutable objects, any downstream mutation of the returned config would mutate the shared default for the rest of the process. Consider returning a fresh copy (e.g., clone defaults / call the same normalization path) instead of the constant object.

Suggested change
return { config: DEFAULT_CONFIG, warnings }
const config: TrackerConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))
return { config, warnings }

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants