1use crate::file_references::FileReferenceValidator;
7use crate::limits::{
8 MAX_ALLOWED_TOOLS, MAX_COMPATIBILITY_CHARS, MAX_LICENSE_CHARS, MAX_SKILL_DESCRIPTION_CHARS, MAX_SKILL_NAME_CHARS,
9 allowed_tool_count,
10};
11use crate::types::{SkillManifest, resolve_skill_dir};
12use crate::validation_report::SkillValidationReport;
13use std::path::Path;
14
15pub struct ComprehensiveSkillValidator {
17 strict_mode: bool,
18}
19
20impl ComprehensiveSkillValidator {
21 pub fn new() -> Self {
22 Self { strict_mode: false }
23 }
24
25 pub fn strict() -> Self {
26 Self { strict_mode: true }
27 }
28
29 pub fn validate_manifest(&self, manifest: &SkillManifest, skill_path: &Path) -> SkillValidationReport {
31 let mut report = SkillValidationReport::new(manifest.name.clone(), skill_path.to_path_buf());
32
33 self.validate_name_field(manifest, &mut report);
35
36 self.validate_description_field(manifest, &mut report);
38
39 self.validate_directory_match(manifest, skill_path, &mut report);
41
42 self.validate_optional_fields(manifest, &mut report);
44
45 self.validate_instructions_length(manifest, &mut report);
47
48 report.finalize();
49 report
50 }
51
52 fn validate_name_field(&self, manifest: &SkillManifest, report: &mut SkillValidationReport) {
54 if manifest.name.is_empty() {
56 report.add_error(Some("name".to_string()), "name is required and must not be empty".to_string(), None);
57 return;
58 }
59
60 if manifest.name.len() > MAX_SKILL_NAME_CHARS {
62 report.add_error(
63 Some("name".to_string()),
64 format!("name exceeds maximum length: {} characters (max {MAX_SKILL_NAME_CHARS})", manifest.name.len()),
65 Some(format!("Use a shorter name (1-{MAX_SKILL_NAME_CHARS} characters)")),
66 );
67 }
68
69 if !manifest.name.chars().all(|c| c.is_lowercase() || c.is_numeric() || c == '-') {
71 report.add_error(
72 Some("name".to_string()),
73 format!(
74 "name contains invalid characters: '{}'\nMust contain only lowercase letters, numbers, and hyphens",
75 manifest.name
76 ),
77 Some("Use only a-z, 0-9, and hyphens".to_string()),
78 );
79 }
80
81 if manifest.name.contains("--") {
83 report.add_error(
84 Some("name".to_string()),
85 format!("name contains consecutive hyphens: '{}'", manifest.name),
86 Some("Remove consecutive hyphens (--)".to_string()),
87 );
88 }
89
90 if manifest.name.starts_with('-') {
92 report.add_error(
93 Some("name".to_string()),
94 format!("name starts with hyphen: '{}'", manifest.name),
95 Some("Remove the leading hyphen".to_string()),
96 );
97 }
98
99 if manifest.name.ends_with('-') {
101 report.add_error(
102 Some("name".to_string()),
103 format!("name ends with hyphen: '{}'", manifest.name),
104 Some("Remove the trailing hyphen".to_string()),
105 );
106 }
107
108 if manifest.name.contains("anthropic") || manifest.name.contains("claude") {
110 report.add_error(
111 Some("name".to_string()),
112 format!("name contains reserved word: '{}'\nMust not contain 'anthropic' or 'claude'", manifest.name),
113 Some("Choose a different name without these words".to_string()),
114 );
115 }
116 }
117
118 fn validate_description_field(&self, manifest: &SkillManifest, report: &mut SkillValidationReport) {
120 if manifest.description.is_empty() {
121 report.add_error(
122 Some("description".to_string()),
123 "description is required and must not be empty".to_string(),
124 Some("Add a description explaining what the skill does and when to use it".to_string()),
125 );
126 return;
127 }
128
129 if manifest.description.len() > MAX_SKILL_DESCRIPTION_CHARS {
130 report.add_error(
131 Some("description".to_string()),
132 format!(
133 "description exceeds maximum length: {} characters (max {MAX_SKILL_DESCRIPTION_CHARS})",
134 manifest.description.len()
135 ),
136 Some(format!("Shorten the description to {MAX_SKILL_DESCRIPTION_CHARS} characters or less")),
137 );
138 }
139
140 if manifest.description.len() < 50 {
142 report.add_suggestion(Some("description".to_string()), "Description is very short".to_string());
143 }
144 }
145
146 fn validate_directory_match(
148 &self,
149 manifest: &SkillManifest,
150 skill_path: &Path,
151 report: &mut SkillValidationReport,
152 ) {
153 if let Err(e) = manifest.validate_directory_name_match(skill_path) {
154 report.add_warning(
155 Some("name".to_string()),
156 e.to_string(),
157 Some("Rename the skill directory to match the name field, or rename the skill".to_string()),
158 );
159 }
160 }
161
162 fn validate_optional_fields(&self, manifest: &SkillManifest, report: &mut SkillValidationReport) {
164 if let Some(allowed_tools) = &manifest.allowed_tools {
166 let tool_count = allowed_tool_count(allowed_tools);
167
168 if tool_count > MAX_ALLOWED_TOOLS {
169 report.add_error(
170 Some("allowed-tools".to_string()),
171 format!("allowed-tools exceeds maximum tool count: {tool_count} tools (max {MAX_ALLOWED_TOOLS})"),
172 Some(format!("Reduce the number of tools to {MAX_ALLOWED_TOOLS} or fewer")),
173 );
174 }
175
176 if tool_count == 0 {
177 report.add_error(
178 Some("allowed-tools".to_string()),
179 "allowed-tools must not be empty if specified".to_string(),
180 Some("Either remove the field or add valid tool names".to_string()),
181 );
182 }
183 }
184
185 if let Some(license) = &manifest.license
187 && license.len() > MAX_LICENSE_CHARS
188 {
189 report.add_error(
190 Some("license".to_string()),
191 format!("license exceeds maximum length: {} characters (max {MAX_LICENSE_CHARS})", license.len()),
192 Some("Shorten the license field".to_string()),
193 );
194 }
195
196 if let Some(compatibility) = &manifest.compatibility {
198 if compatibility.is_empty() {
199 report.add_error(
200 Some("compatibility".to_string()),
201 "compatibility must not be empty if specified".to_string(),
202 Some("Either remove the field or add meaningful compatibility info".to_string()),
203 );
204 } else if compatibility.len() > MAX_COMPATIBILITY_CHARS {
205 report.add_error(
206 Some("compatibility".to_string()),
207 format!(
208 "compatibility exceeds maximum length: {} characters (max {MAX_COMPATIBILITY_CHARS})",
209 compatibility.len()
210 ),
211 Some("Shorten the compatibility field".to_string()),
212 );
213 }
214 }
215
216 if manifest.license.is_none() {
218 report.add_suggestion(Some("license".to_string()), "Consider adding a license field".to_string());
219 }
220
221 if manifest.compatibility.is_none() {
222 report.add_suggestion(
223 Some("compatibility".to_string()),
224 "Consider adding a compatibility field if the skill has specific requirements".to_string(),
225 );
226 }
227
228 if !self.description_has_routing_signals(&manifest.description) {
229 self.report_routing_quality_issue(
230 report,
231 "description",
232 "Description lacks concrete routing signals (inputs/triggers/outputs)",
233 "Rewrite description as routing logic: when to use, when not to use, expected result.",
234 );
235 }
236 }
237
238 fn report_routing_quality_issue(
239 &self,
240 report: &mut SkillValidationReport,
241 field: &str,
242 message: &str,
243 remediation: &str,
244 ) {
245 if self.strict_mode {
246 report.add_error(Some(field.to_string()), message.to_string(), Some(remediation.to_string()));
247 } else {
248 report.add_warning(Some(field.to_string()), message.to_string(), Some(remediation.to_string()));
249 }
250 }
251
252 fn description_has_routing_signals(&self, description: &str) -> bool {
253 let text = description.to_lowercase();
254 let has_trigger_hint =
255 text.contains("when") || text.contains("if ") || text.contains("for ") || text.contains("trigger");
256 let has_output_hint = text.contains("output")
257 || text.contains("returns")
258 || text.contains("result")
259 || text.contains("generat")
260 || text.contains("produ");
261 let has_action_hint = text.contains("analy")
262 || text.contains("extract")
263 || text.contains("transform")
264 || text.contains("validate")
265 || text.contains("summar")
266 || text.contains("convert")
267 || text.contains("clean");
268
269 has_action_hint && (has_trigger_hint || has_output_hint)
270 }
271
272 fn validate_instructions_length(&self, _manifest: &SkillManifest, report: &mut SkillValidationReport) {
274 report.add_suggestion(None, "Keep SKILL.md under 500 lines for optimal context usage".to_string());
276 }
277
278 pub fn validate_file_references(
280 &self,
281 _manifest: &SkillManifest,
282 skill_path: &Path,
283 instructions: &str,
284 report: &mut SkillValidationReport,
285 ) {
286 let skill_root = resolve_skill_dir(skill_path);
287 let validator = FileReferenceValidator::new(skill_root.to_path_buf());
288 let reference_errors = validator.validate_references(instructions);
289
290 for error in reference_errors {
291 if self.strict_mode {
293 report.add_error(
294 None,
295 format!("File reference issue: {error}"),
296 Some("Fix the file reference or ensure the referenced file exists".to_string()),
297 );
298 } else {
299 report.add_warning(
300 None,
301 format!("File reference issue: {error}"),
302 Some("Fix the file reference or ensure the referenced file exists".to_string()),
303 );
304 }
305 }
306
307 let valid_refs = validator.list_valid_references();
309 if !valid_refs.is_empty() {
310 let ref_list: Vec<String> = valid_refs.iter().map(|p| p.to_string_lossy().to_string()).collect();
311 report.add_suggestion(
312 None,
313 format!("Found {} valid file references: {}", ref_list.len(), ref_list.join(", ")),
314 );
315 }
316 }
317}
318
319impl Default for ComprehensiveSkillValidator {
320 fn default() -> Self {
321 Self::new()
322 }
323}
324
325#[cfg(test)]
326mod tests {
327 use super::*;
328 use std::path::PathBuf;
329 use tempfile::TempDir;
330
331 #[test]
332 fn test_comprehensive_validation() {
333 let validator = ComprehensiveSkillValidator::new();
334 let manifest = SkillManifest {
335 name: "test-skill".to_string(),
336 description: "A test skill for validation".to_string(),
337 version: Some("1.0.0".to_string()),
338 author: Some("Test Author".to_string()),
339 allowed_tools: Some("Read Write Bash".to_string()),
340 compatibility: Some("Designed for VT Code".to_string()),
341 ..Default::default()
342 };
343
344 let report = validator.validate_manifest(&manifest, PathBuf::from("/tmp/nonexistent").as_path());
347
348 assert!(report.suggestions.iter().any(|s| s.field == Some("license".to_string())));
350 }
351
352 #[test]
357 fn reports_allowed_tools_limit_at_the_same_boundary() {
358 let validator = ComprehensiveSkillValidator::new();
359 let report_for = |count: usize| {
360 let tools = (0..count).map(|i| format!("Tool{i}")).collect::<Vec<_>>().join(" ");
361 let manifest = SkillManifest {
362 name: "test-skill".to_string(),
363 description: "A test skill for validation".to_string(),
364 allowed_tools: Some(tools),
365 ..Default::default()
366 };
367 validator
368 .validate_manifest(&manifest, PathBuf::from("/tmp/nonexistent").as_path())
369 .to_json()
370 .expect("report serializes")
371 };
372
373 let over_limit = report_for(MAX_ALLOWED_TOOLS + 1);
374 assert!(
375 over_limit.contains("allowed-tools exceeds maximum tool count"),
376 "expected an allowed-tools error, got {over_limit}"
377 );
378
379 let at_limit = report_for(MAX_ALLOWED_TOOLS);
380 assert!(
381 !at_limit.contains("allowed-tools"),
382 "{} tools is the inclusive limit and must not error, got {at_limit}",
383 MAX_ALLOWED_TOOLS
384 );
385 }
386
387 #[test]
388 fn directory_name_check_uses_skill_directory_not_its_parent() {
389 let temp = TempDir::new().expect("temp dir");
394 let skill_dir = temp.path().join("skills").join("my-skill");
395 std::fs::create_dir_all(&skill_dir).expect("create skill dir");
396
397 let manifest = SkillManifest {
398 name: "my-skill".to_string(),
399 description: "A test skill for validation".to_string(),
400 ..Default::default()
401 };
402
403 let validator = ComprehensiveSkillValidator::new();
404 let report = validator.validate_manifest(&manifest, &skill_dir);
405
406 assert!(
407 !report.generate_summary().contains("does not match directory name"),
408 "matching skill directory must not warn: {}",
409 report.generate_summary()
410 );
411 }
412
413 #[test]
414 fn directory_name_check_accepts_skill_md_path() {
415 let temp = TempDir::new().expect("temp dir");
418 let skill_dir = temp.path().join("skills").join("my-skill");
419 std::fs::create_dir_all(&skill_dir).expect("create skill dir");
420 std::fs::write(skill_dir.join("SKILL.md"), "# Body").expect("write SKILL.md");
421
422 let manifest = SkillManifest {
423 name: "my-skill".to_string(),
424 description: "A test skill for validation".to_string(),
425 ..Default::default()
426 };
427
428 let validator = ComprehensiveSkillValidator::new();
429 let report = validator.validate_manifest(&manifest, &skill_dir.join("SKILL.md"));
430
431 assert!(
432 !report.generate_summary().contains("does not match directory name"),
433 "SKILL.md path must resolve to the skill directory: {}",
434 report.generate_summary()
435 );
436 }
437
438 #[test]
439 fn file_references_resolve_against_skill_directory() {
440 let temp = TempDir::new().expect("temp dir");
443 let skill_dir = temp.path().join("skills").join("ref-skill");
444 std::fs::create_dir_all(skill_dir.join("references")).expect("create references dir");
445 std::fs::write(skill_dir.join("references").join("guide.md"), "# Guide").expect("write reference");
446
447 let manifest = SkillManifest {
448 name: "ref-skill".to_string(),
449 description: "Reference skill".to_string(),
450 ..Default::default()
451 };
452 let instructions = "See references/guide.md for details.";
453
454 let validator = ComprehensiveSkillValidator::new();
455 let mut report = validator.validate_manifest(&manifest, &skill_dir);
456 validator.validate_file_references(&manifest, &skill_dir, instructions, &mut report);
457 report.finalize();
458
459 assert!(
460 !report.generate_summary().contains("File reference issue"),
461 "existing reference must resolve against the skill directory: {}",
462 report.generate_summary()
463 );
464 }
465
466 #[test]
467 fn file_references_report_missing_resource() {
468 let temp = TempDir::new().expect("temp dir");
471 let skill_dir = temp.path().join("skills").join("ref-skill");
472 std::fs::create_dir_all(&skill_dir).expect("create skill dir");
473
474 let manifest = SkillManifest {
475 name: "ref-skill".to_string(),
476 description: "Reference skill".to_string(),
477 ..Default::default()
478 };
479 let instructions = "See references/missing.md for details.";
480
481 let validator = ComprehensiveSkillValidator::new();
482 let mut report = validator.validate_manifest(&manifest, &skill_dir);
483 validator.validate_file_references(&manifest, &skill_dir, instructions, &mut report);
484 report.finalize();
485
486 assert!(
487 report.generate_summary().contains("File reference issue"),
488 "missing reference must still be flagged: {}",
489 report.generate_summary()
490 );
491 }
492}