validate.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #!/usr/bin/env bash
  2. # claude-mods validation script
  3. # Validates YAML frontmatter, required fields, and naming conventions
  4. set -Eeuo pipefail
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. NC='\033[0m' # No Color
  10. # Counters
  11. PASS=0
  12. FAIL=0
  13. WARN=0
  14. # Get script directory
  15. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  16. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  17. # Parse arguments
  18. YAML_ONLY=false
  19. NAMES_ONLY=false
  20. while [[ $# -gt 0 ]]; do
  21. case $1 in
  22. --yaml-only)
  23. YAML_ONLY=true
  24. shift
  25. ;;
  26. --names-only)
  27. NAMES_ONLY=true
  28. shift
  29. ;;
  30. *)
  31. echo "Unknown option: $1"
  32. exit 1
  33. ;;
  34. esac
  35. done
  36. # Helper functions
  37. log_pass() {
  38. echo -e "${GREEN}PASS${NC}: $1"
  39. PASS=$((PASS + 1))
  40. }
  41. log_fail() {
  42. echo -e "${RED}FAIL${NC}: $1"
  43. FAIL=$((FAIL + 1))
  44. }
  45. log_warn() {
  46. echo -e "${YELLOW}WARN${NC}: $1"
  47. WARN=$((WARN + 1))
  48. }
  49. # Check if file has valid YAML frontmatter
  50. check_yaml_frontmatter() {
  51. local file="$1"
  52. local content
  53. content=$(cat "$file")
  54. # Check for opening ---
  55. if [[ "$content" != ---* ]]; then
  56. log_fail "$file - Missing YAML frontmatter (no opening ---)"
  57. return 1
  58. fi
  59. # Check for closing ---
  60. local frontmatter
  61. frontmatter=$(echo "$content" | sed -n '1,/^---$/p' | tail -n +2)
  62. if [[ -z "$frontmatter" ]]; then
  63. log_fail "$file - Invalid YAML frontmatter (no closing ---)"
  64. return 1
  65. fi
  66. return 0
  67. }
  68. # Extract field from YAML frontmatter
  69. get_yaml_field() {
  70. local file="$1"
  71. local field="$2"
  72. # Extract frontmatter and get field value
  73. sed -n '2,/^---$/p' "$file" | grep "^${field}:" | sed "s/^${field}:[[:space:]]*//" | sed 's/^["'"'"']//' | sed 's/["'"'"']$//'
  74. }
  75. # Check required fields in agents/commands
  76. check_required_fields() {
  77. local file="$1"
  78. local type="$2"
  79. local name
  80. local description
  81. name=$(get_yaml_field "$file" "name")
  82. description=$(get_yaml_field "$file" "description")
  83. # Agents require both name and description
  84. if [[ "$type" == "agent" ]]; then
  85. if [[ -z "$name" ]]; then
  86. log_fail "$file - Missing required field: name"
  87. return 1
  88. fi
  89. if [[ -z "$description" ]]; then
  90. log_fail "$file - Missing required field: description"
  91. return 1
  92. fi
  93. fi
  94. # Commands only require description
  95. if [[ "$type" == "command" ]]; then
  96. if [[ -z "$description" ]]; then
  97. log_fail "$file - Missing required field: description"
  98. return 1
  99. fi
  100. fi
  101. return 0
  102. }
  103. # Check naming convention (kebab-case)
  104. check_naming() {
  105. local file="$1"
  106. local basename
  107. basename=$(basename "$file" .md)
  108. # Check if filename is kebab-case
  109. if [[ ! "$basename" =~ ^[a-z][a-z0-9]*(-[a-z0-9]+)*$ ]]; then
  110. log_warn "$file - Filename not kebab-case: $basename"
  111. return 1
  112. fi
  113. # Check if name field matches filename (for agents)
  114. local name
  115. name=$(get_yaml_field "$file" "name")
  116. if [[ -n "$name" && "$name" != "$basename" ]]; then
  117. log_warn "$file - Name field '$name' doesn't match filename '$basename'"
  118. return 1
  119. fi
  120. return 0
  121. }
  122. # Validate agents
  123. validate_agents() {
  124. echo ""
  125. echo "=== Validating Agents ==="
  126. local agent_dir="$PROJECT_DIR/agents"
  127. if [[ ! -d "$agent_dir" ]]; then
  128. log_warn "agents/ directory not found"
  129. return
  130. fi
  131. # Use find for better Windows compatibility
  132. while IFS= read -r -d '' file; do
  133. if ! $NAMES_ONLY; then
  134. if check_yaml_frontmatter "$file"; then
  135. if check_required_fields "$file" "agent"; then
  136. log_pass "$file - Valid agent"
  137. fi
  138. fi
  139. fi
  140. if ! $YAML_ONLY; then
  141. check_naming "$file" || true
  142. fi
  143. done < <(find "$agent_dir" -maxdepth 1 -name "*.md" -type f -print0)
  144. }
  145. # Validate commands
  146. validate_commands() {
  147. echo ""
  148. echo "=== Validating Commands ==="
  149. local cmd_dir="$PROJECT_DIR/commands"
  150. if [[ ! -d "$cmd_dir" ]]; then
  151. log_warn "commands/ directory not found"
  152. return
  153. fi
  154. # Check .md files directly in commands/
  155. while IFS= read -r -d '' file; do
  156. if ! $NAMES_ONLY; then
  157. if check_yaml_frontmatter "$file"; then
  158. if check_required_fields "$file" "command"; then
  159. log_pass "$file - Valid command"
  160. fi
  161. fi
  162. fi
  163. if ! $YAML_ONLY; then
  164. check_naming "$file" || true
  165. fi
  166. done < <(find "$cmd_dir" -maxdepth 1 -name "*.md" -type f -print0)
  167. # Check subdirectories (like g-slave/, session-manager/)
  168. while IFS= read -r -d '' subdir; do
  169. # Look for main command file (exclude README.md, LICENSE.md)
  170. while IFS= read -r -d '' file; do
  171. local basename
  172. basename=$(basename "$file")
  173. # Skip README and LICENSE files
  174. [[ "$basename" == "README.md" || "$basename" == "LICENSE.md" ]] && continue
  175. if ! $NAMES_ONLY; then
  176. if check_yaml_frontmatter "$file"; then
  177. # Commands in subdirs may have different required fields
  178. local desc
  179. desc=$(get_yaml_field "$file" "description")
  180. if [[ -n "$desc" ]]; then
  181. log_pass "$file - Valid subcommand"
  182. else
  183. log_warn "$file - Missing description"
  184. fi
  185. fi
  186. fi
  187. done < <(find "$subdir" -maxdepth 1 -name "*.md" -type f -print0)
  188. done < <(find "$cmd_dir" -mindepth 1 -maxdepth 1 -type d -print0)
  189. }
  190. # Validate skills
  191. validate_skills() {
  192. echo ""
  193. echo "=== Validating Skills ==="
  194. local skills_dir="$PROJECT_DIR/skills"
  195. if [[ ! -d "$skills_dir" ]]; then
  196. log_warn "skills/ directory not found"
  197. return
  198. fi
  199. while IFS= read -r -d '' skill_subdir; do
  200. # Skip shared helper dirs (e.g. _lib) - not skills, no SKILL.md expected.
  201. [[ "$(basename "$skill_subdir")" == _* ]] && continue
  202. local skill_file="$skill_subdir/SKILL.md"
  203. if [[ ! -f "$skill_file" ]]; then
  204. log_fail "$skill_subdir - Missing SKILL.md"
  205. continue
  206. fi
  207. if ! $NAMES_ONLY; then
  208. if check_yaml_frontmatter "$skill_file"; then
  209. local name
  210. local desc
  211. name=$(get_yaml_field "$skill_file" "name")
  212. desc=$(get_yaml_field "$skill_file" "description")
  213. if [[ -n "$name" && -n "$desc" ]]; then
  214. log_pass "$skill_file - Valid skill"
  215. else
  216. [[ -z "$name" ]] && log_fail "$skill_file - Missing name"
  217. [[ -z "$desc" ]] && log_fail "$skill_file - Missing description"
  218. fi
  219. fi
  220. fi
  221. done < <(find "$skills_dir" -mindepth 1 -maxdepth 1 -type d -print0)
  222. }
  223. # Validate rules (optional YAML frontmatter with optional paths field)
  224. validate_rules() {
  225. echo ""
  226. echo "=== Validating Rules ==="
  227. local rules_dir="$PROJECT_DIR/templates/rules"
  228. if [[ ! -d "$rules_dir" ]]; then
  229. echo " (no templates/rules/ directory - skipping)"
  230. return
  231. fi
  232. while IFS= read -r -d '' file; do
  233. local basename
  234. basename=$(basename "$file")
  235. # Rules should be .md files
  236. if [[ "$file" != *.md ]]; then
  237. log_warn "$file - Rule file should be .md"
  238. continue
  239. fi
  240. # Check if file has content
  241. if [[ ! -s "$file" ]]; then
  242. log_fail "$file - Empty rule file"
  243. continue
  244. fi
  245. # Check for valid YAML frontmatter if present
  246. local content
  247. content=$(cat "$file")
  248. if [[ "$content" == ---* ]]; then
  249. # Has frontmatter - validate it
  250. local closing
  251. closing=$(echo "$content" | sed -n '2,${/^---$/=;}'| head -1)
  252. if [[ -z "$closing" ]]; then
  253. log_fail "$file - Invalid YAML frontmatter (no closing ---)"
  254. continue
  255. fi
  256. # If paths field exists, validate it's not empty
  257. local paths
  258. paths=$(get_yaml_field "$file" "paths")
  259. if grep -q "^paths:" "$file" && [[ -z "$paths" ]]; then
  260. log_warn "$file - paths field is empty"
  261. fi
  262. fi
  263. # Check naming convention (kebab-case)
  264. local name
  265. name=$(basename "$file" .md)
  266. if [[ ! "$name" =~ ^[a-z][a-z0-9]*(-[a-z0-9]+)*$ ]]; then
  267. log_warn "$file - Filename not kebab-case: $name"
  268. fi
  269. log_pass "$file - Valid rule"
  270. done < <(find "$rules_dir" -name "*.md" -type f -print0)
  271. }
  272. # Validate settings files (permissions and hooks)
  273. validate_settings() {
  274. echo ""
  275. echo "=== Validating Settings ==="
  276. local settings_file="$PROJECT_DIR/templates/settings.local.json"
  277. if [[ ! -f "$settings_file" ]]; then
  278. echo " (no templates/settings.local.json - skipping)"
  279. return
  280. fi
  281. # Check if valid JSON
  282. if ! jq empty "$settings_file" 2>/dev/null; then
  283. log_fail "$settings_file - Invalid JSON"
  284. return
  285. fi
  286. # Check for permissions structure
  287. if ! jq -e '.permissions' "$settings_file" >/dev/null 2>&1; then
  288. log_fail "$settings_file - Missing 'permissions' key"
  289. else
  290. # Check permissions has allow array
  291. if ! jq -e '.permissions.allow | type == "array"' "$settings_file" >/dev/null 2>&1; then
  292. log_fail "$settings_file - permissions.allow should be an array"
  293. else
  294. log_pass "$settings_file - Valid permissions structure"
  295. fi
  296. fi
  297. # Check for hooks structure (optional but if present should be object)
  298. if jq -e '.hooks' "$settings_file" >/dev/null 2>&1; then
  299. if ! jq -e '.hooks | type == "object"' "$settings_file" >/dev/null 2>&1; then
  300. log_fail "$settings_file - hooks should be an object"
  301. else
  302. # Validate hook event names if any hooks defined
  303. local hook_events
  304. hook_events=$(jq -r '.hooks | keys[]' "$settings_file" 2>/dev/null || true)
  305. local valid_events="PreToolUse PostToolUse PermissionRequest Notification UserPromptSubmit Stop SubagentStop PreCompact SessionStart SessionEnd"
  306. for event in $hook_events; do
  307. if [[ ! " $valid_events " =~ " $event " ]]; then
  308. log_warn "$settings_file - Unknown hook event: $event"
  309. fi
  310. done
  311. if [[ -n "$hook_events" ]]; then
  312. log_pass "$settings_file - Valid hooks structure"
  313. else
  314. log_pass "$settings_file - Hooks defined (empty)"
  315. fi
  316. fi
  317. fi
  318. }
  319. # Validate plugin + marketplace manifests (.claude-plugin/)
  320. #
  321. # The authoritative validator is `claude plugin validate` (it tracks the live
  322. # schema - it caught a bad plugin `source` shape and an `author` type error
  323. # that a hand-rolled jq check sailed past). We prefer it when the CLI is
  324. # present and fall back to lightweight structural jq checks otherwise. The
  325. # stray-root-file guard runs regardless, because the official tool validates
  326. # whatever path it is given and cannot see a misplaced copy.
  327. validate_plugin() {
  328. echo ""
  329. echo "=== Validating Plugin Manifests ==="
  330. local plugin_dir="$PROJECT_DIR/.claude-plugin"
  331. local plugin_file="$plugin_dir/plugin.json"
  332. local mkt_file="$plugin_dir/marketplace.json"
  333. # --- location guard (official tool can't see this) ---
  334. # The spec mandates .claude-plugin/marketplace.json. A copy at the repo
  335. # root is the regression that caused /plugin marketplace add to fail (#4).
  336. if [[ -f "$PROJECT_DIR/marketplace.json" ]]; then
  337. log_fail "marketplace.json found at repo root - must live at .claude-plugin/marketplace.json"
  338. fi
  339. [[ -f "$plugin_file" ]] || log_fail ".claude-plugin/plugin.json - Missing"
  340. [[ -f "$mkt_file" ]] || log_fail ".claude-plugin/marketplace.json - Missing (required for /plugin marketplace add)"
  341. # --- authoritative path: claude plugin validate ---
  342. if command -v claude >/dev/null 2>&1; then
  343. # Marketplace manifest (repo root resolves to the marketplace).
  344. if claude plugin validate "$PROJECT_DIR" >/dev/null 2>&1; then
  345. log_pass "marketplace.json - claude plugin validate passed"
  346. else
  347. log_fail "marketplace.json - claude plugin validate failed (run: claude plugin validate .)"
  348. fi
  349. # Plugin manifest: validate in isolation so it is not shadowed by the
  350. # marketplace manifest in the same .claude-plugin/ directory.
  351. if [[ -f "$plugin_file" ]]; then
  352. local tmp
  353. tmp=$(mktemp -d)
  354. mkdir -p "$tmp/.claude-plugin"
  355. cp "$plugin_file" "$tmp/.claude-plugin/plugin.json"
  356. if claude plugin validate "$tmp" >/dev/null 2>&1; then
  357. log_pass "plugin.json - claude plugin validate passed"
  358. else
  359. log_fail "plugin.json - claude plugin validate failed (unrecognized keys or wrong field types)"
  360. fi
  361. rm -rf "$tmp"
  362. fi
  363. return
  364. fi
  365. # --- fallback path: lightweight structural checks (jq) ---
  366. log_warn "claude CLI not found - using lightweight manifest checks only (install Claude Code for authoritative validation)"
  367. if [[ -f "$plugin_file" ]]; then
  368. if ! jq empty "$plugin_file" 2>/dev/null; then
  369. log_fail "$plugin_file - Invalid JSON"
  370. elif jq -e '.name | strings' "$plugin_file" >/dev/null 2>&1; then
  371. log_pass "$plugin_file - structurally OK (name present)"
  372. else
  373. log_fail "$plugin_file - Missing required field: name"
  374. fi
  375. fi
  376. if [[ -f "$mkt_file" ]]; then
  377. if ! jq empty "$mkt_file" 2>/dev/null; then
  378. log_fail "$mkt_file - Invalid JSON"
  379. else
  380. jq -e '.name | strings' "$mkt_file" >/dev/null 2>&1 \
  381. || log_fail "$mkt_file - Missing required field: name (string)"
  382. jq -e '.owner.name | strings' "$mkt_file" >/dev/null 2>&1 \
  383. || log_fail "$mkt_file - owner.name missing (owner must be an object with a name)"
  384. jq -e '.plugins | arrays' "$mkt_file" >/dev/null 2>&1 \
  385. || log_fail "$mkt_file - Missing required field: plugins (array)"
  386. if jq -e '.name and (.owner.name | strings) and (.plugins | arrays)' "$mkt_file" >/dev/null 2>&1; then
  387. log_pass "$mkt_file - structurally OK (run claude plugin validate for full schema)"
  388. fi
  389. fi
  390. fi
  391. }
  392. # Main
  393. main() {
  394. echo "claude-mods Validation"
  395. echo "======================"
  396. echo "Project: $PROJECT_DIR"
  397. validate_agents
  398. validate_commands
  399. validate_skills
  400. validate_rules
  401. validate_settings
  402. validate_plugin
  403. echo ""
  404. echo "======================"
  405. echo -e "Results: ${GREEN}$PASS passed${NC}, ${RED}$FAIL failed${NC}, ${YELLOW}$WARN warnings${NC}"
  406. if [[ $FAIL -gt 0 ]]; then
  407. exit 1
  408. fi
  409. exit 0
  410. }
  411. main "$@"