forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.ts
More file actions
265 lines (242 loc) · 7.76 KB
/
Copy pathvalidation.ts
File metadata and controls
265 lines (242 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import type { ConfigScope } from 'src/services/mcp/types.js'
import type { ZodError, ZodIssue } from 'zod/v4'
import { jsonParse } from '../slowOperations.js'
import { plural } from '../stringUtils.js'
import { validatePermissionRule } from './permissionValidation.js'
import { generateSettingsJSONSchema } from './schemaOutput.js'
import type { SettingsJson } from './types.js'
import { SettingsSchema } from './types.js'
import { getValidationTip } from './validationTips.js'
/**
* Helper type guards for specific Zod v4 issue types
* In v4, issue types have different structures than v3
*/
function isInvalidTypeIssue(issue: ZodIssue): issue is ZodIssue & {
code: 'invalid_type'
expected: string
input: unknown
} {
return issue.code === 'invalid_type'
}
function isInvalidValueIssue(issue: ZodIssue): issue is ZodIssue & {
code: 'invalid_value'
values: unknown[]
input: unknown
} {
return issue.code === 'invalid_value'
}
function isUnrecognizedKeysIssue(
issue: ZodIssue,
): issue is ZodIssue & { code: 'unrecognized_keys'; keys: string[] } {
return issue.code === 'unrecognized_keys'
}
function isTooSmallIssue(issue: ZodIssue): issue is ZodIssue & {
code: 'too_small'
minimum: number | bigint
origin: string
} {
return issue.code === 'too_small'
}
/** Field path in dot notation (e.g., "permissions.defaultMode", "env.DEBUG") */
export type FieldPath = string
export type ValidationError = {
/** Relative file path */
file?: string
/** Field path in dot notation */
path: FieldPath
/** Human-readable error message */
message: string
/** Expected value or type */
expected?: string
/** The actual invalid value that was provided */
invalidValue?: unknown
/** Suggestion for fixing the error */
suggestion?: string
/** Link to relevant documentation */
docLink?: string
/** MCP-specific metadata - only present for MCP configuration errors */
mcpErrorMetadata?: {
/** Which configuration scope this error came from */
scope: ConfigScope
/** The server name if error is specific to a server */
serverName?: string
/** Severity of the error */
severity?: 'fatal' | 'warning'
}
}
export type SettingsWithErrors = {
settings: SettingsJson
errors: ValidationError[]
}
/**
* Format a Zod validation error into human-readable validation errors
*/
/**
* Get the type string for an unknown value (for error messages)
*/
function getReceivedType(value: unknown): string {
if (value === null) return 'null'
if (value === undefined) return 'undefined'
if (Array.isArray(value)) return 'array'
return typeof value
}
function extractReceivedFromMessage(msg: string): string | undefined {
const match = msg.match(/received (\w+)/)
return match ? match[1] : undefined
}
export function formatZodError(
error: ZodError,
filePath: string,
): ValidationError[] {
return error.issues.map((issue): ValidationError => {
const path = issue.path.map(String).join('.')
let message = issue.message
let expected: string | undefined
let enumValues: string[] | undefined
let expectedValue: string | undefined
let receivedValue: unknown
let invalidValue: unknown
if (isInvalidValueIssue(issue)) {
enumValues = issue.values.map(v => String(v))
expectedValue = enumValues.join(' | ')
receivedValue = undefined
invalidValue = undefined
} else if (isInvalidTypeIssue(issue)) {
expectedValue = issue.expected
const receivedType = extractReceivedFromMessage(issue.message)
receivedValue = receivedType ?? getReceivedType(issue.input)
invalidValue = receivedType ?? getReceivedType(issue.input)
} else if (isTooSmallIssue(issue)) {
expectedValue = String(issue.minimum)
} else if (issue.code === 'custom' && 'params' in issue) {
const params = issue.params as { received?: unknown }
receivedValue = params.received
invalidValue = receivedValue
}
const tip = getValidationTip({
path,
code: issue.code,
expected: expectedValue,
received: receivedValue,
enumValues,
message: issue.message,
value: receivedValue,
})
if (isInvalidValueIssue(issue)) {
expected = enumValues?.map(v => `"${v}"`).join(', ')
message = `Invalid value. Expected one of: ${expected}`
} else if (isInvalidTypeIssue(issue)) {
const receivedType =
extractReceivedFromMessage(issue.message) ??
getReceivedType(issue.input)
if (
issue.expected === 'object' &&
receivedType === 'null' &&
path === ''
) {
message = 'Invalid or malformed JSON'
} else {
message = `Expected ${issue.expected}, but received ${receivedType}`
}
} else if (isUnrecognizedKeysIssue(issue)) {
const keys = issue.keys.join(', ')
message = `Unrecognized ${plural(issue.keys.length, 'field')}: ${keys}`
} else if (isTooSmallIssue(issue)) {
message = `Number must be greater than or equal to ${issue.minimum}`
expected = String(issue.minimum)
}
return {
file: filePath,
path,
message,
expected,
invalidValue,
suggestion: tip?.suggestion,
docLink: tip?.docLink,
}
})
}
/**
* Validates that settings file content conforms to the SettingsSchema.
* This is used during file edits to ensure the resulting file is valid.
*/
export function validateSettingsFileContent(content: string):
| {
isValid: true
}
| {
isValid: false
error: string
fullSchema: string
} {
try {
// Parse the JSON first
const jsonData = jsonParse(content)
// Validate against SettingsSchema in strict mode
const result = SettingsSchema().strict().safeParse(jsonData)
if (result.success) {
return { isValid: true }
}
// Format the validation error in a helpful way
const errors = formatZodError(result.error, 'settings')
const errorMessage =
'Settings validation failed:\n' +
errors.map(err => `- ${err.path}: ${err.message}`).join('\n')
return {
isValid: false,
error: errorMessage,
fullSchema: generateSettingsJSONSchema(),
}
} catch (parseError) {
return {
isValid: false,
error: `Invalid JSON: ${parseError instanceof Error ? parseError.message : 'Unknown parsing error'}`,
fullSchema: generateSettingsJSONSchema(),
}
}
}
/**
* Filters invalid permission rules from raw parsed JSON data before schema validation.
* This prevents one bad rule from poisoning the entire settings file.
* Returns warnings for each filtered rule.
*/
export function filterInvalidPermissionRules(
data: unknown,
filePath: string,
): ValidationError[] {
if (!data || typeof data !== 'object') return []
const obj = data as Record<string, unknown>
if (!obj.permissions || typeof obj.permissions !== 'object') return []
const perms = obj.permissions as Record<string, unknown>
const warnings: ValidationError[] = []
for (const key of ['allow', 'deny', 'ask']) {
const rules = perms[key]
if (!Array.isArray(rules)) continue
perms[key] = rules.filter(rule => {
if (typeof rule !== 'string') {
warnings.push({
file: filePath,
path: `permissions.${key}`,
message: `Non-string value in ${key} array was removed`,
invalidValue: rule,
})
return false
}
const result = validatePermissionRule(rule)
if (!result.valid) {
let message = `Invalid permission rule "${rule}" was skipped`
if (result.error) message += `: ${result.error}`
if (result.suggestion) message += `. ${result.suggestion}`
warnings.push({
file: filePath,
path: `permissions.${key}`,
message,
invalidValue: rule,
})
return false
}
return true
})
}
return warnings
}