validateEditTool.ts
1 import type { ValidationResult } from 'src/Tool.js' 2 import { isClaudeSettingsPath } from '../permissions/filesystem.js' 3 import { validateSettingsFileContent } from './validation.js' 4 5 /** 6 * Validates settings file edits to ensure the result conforms to SettingsSchema. 7 * This is used by FileEditTool to avoid code duplication. 8 * 9 * @param filePath - The file path being edited 10 * @param originalContent - The original file content before edits 11 * @param getUpdatedContent - A closure that returns the content after applying edits 12 * @returns Validation result with error details if validation fails 13 */ 14 export function validateInputForSettingsFileEdit( 15 filePath: string, 16 originalContent: string, 17 getUpdatedContent: () => string, 18 ): Extract<ValidationResult, { result: false }> | null { 19 // Only validate Claude settings files 20 if (!isClaudeSettingsPath(filePath)) { 21 return null 22 } 23 24 // Check if the current file (before edit) conforms to the schema 25 const beforeValidation = validateSettingsFileContent(originalContent) 26 27 if (!beforeValidation.isValid) { 28 // If the before version is invalid, allow the edit (don't block it) 29 return null 30 } 31 32 // If the before version is valid, ensure the after version is also valid 33 const updatedContent = getUpdatedContent() 34 const afterValidation = validateSettingsFileContent(updatedContent) 35 36 if (!afterValidation.isValid) { 37 return { 38 result: false, 39 message: `Claude Code settings.json validation failed after edit:\n${afterValidation.error}\n\nFull schema:\n${afterValidation.fullSchema}\nIMPORTANT: Do not update the env unless explicitly instructed to do so.`, 40 errorCode: 10, 41 } 42 } 43 44 return null 45 }