quick_validate.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. """
  3. Quick validation script for skills - minimal version
  4. """
  5. import sys
  6. import os
  7. import re
  8. import yaml
  9. from pathlib import Path
  10. def validate_skill(skill_path):
  11. """Basic validation of a skill"""
  12. skill_path = Path(skill_path)
  13. # Check SKILL.md exists
  14. skill_md = skill_path / 'SKILL.md'
  15. if not skill_md.exists():
  16. return False, "SKILL.md not found"
  17. # Read and validate frontmatter
  18. content = skill_md.read_text()
  19. if not content.startswith('---'):
  20. return False, "No YAML frontmatter found"
  21. # Extract frontmatter
  22. match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
  23. if not match:
  24. return False, "Invalid frontmatter format"
  25. frontmatter_text = match.group(1)
  26. # Parse YAML frontmatter
  27. try:
  28. frontmatter = yaml.safe_load(frontmatter_text)
  29. if not isinstance(frontmatter, dict):
  30. return False, "Frontmatter must be a YAML dictionary"
  31. except yaml.YAMLError as e:
  32. return False, f"Invalid YAML in frontmatter: {e}"
  33. # Define allowed properties
  34. ALLOWED_PROPERTIES = {'name', 'description', 'license', 'allowed-tools', 'metadata'}
  35. # Check for unexpected properties (excluding nested keys under metadata)
  36. unexpected_keys = set(frontmatter.keys()) - ALLOWED_PROPERTIES
  37. if unexpected_keys:
  38. return False, (
  39. f"Unexpected key(s) in SKILL.md frontmatter: {', '.join(sorted(unexpected_keys))}. "
  40. f"Allowed properties are: {', '.join(sorted(ALLOWED_PROPERTIES))}"
  41. )
  42. # Check required fields
  43. if 'name' not in frontmatter:
  44. return False, "Missing 'name' in frontmatter"
  45. if 'description' not in frontmatter:
  46. return False, "Missing 'description' in frontmatter"
  47. # Extract name for validation
  48. name = frontmatter.get('name', '')
  49. if not isinstance(name, str):
  50. return False, f"Name must be a string, got {type(name).__name__}"
  51. name = name.strip()
  52. if name:
  53. # Check naming convention (hyphen-case: lowercase with hyphens)
  54. if not re.match(r'^[a-z0-9-]+$', name):
  55. return False, f"Name '{name}' should be hyphen-case (lowercase letters, digits, and hyphens only)"
  56. if name.startswith('-') or name.endswith('-') or '--' in name:
  57. return False, f"Name '{name}' cannot start/end with hyphen or contain consecutive hyphens"
  58. # Check name length (max 64 characters per spec)
  59. if len(name) > 64:
  60. return False, f"Name is too long ({len(name)} characters). Maximum is 64 characters."
  61. # Extract and validate description
  62. description = frontmatter.get('description', '')
  63. if not isinstance(description, str):
  64. return False, f"Description must be a string, got {type(description).__name__}"
  65. description = description.strip()
  66. if description:
  67. # Check for angle brackets
  68. if '<' in description or '>' in description:
  69. return False, "Description cannot contain angle brackets (< or >)"
  70. # Check description length (max 1024 characters per spec)
  71. if len(description) > 1024:
  72. return False, f"Description is too long ({len(description)} characters). Maximum is 1024 characters."
  73. return True, "Skill is valid!"
  74. if __name__ == "__main__":
  75. if len(sys.argv) != 2:
  76. print("Usage: python quick_validate.py <skill_directory>")
  77. sys.exit(1)
  78. valid, message = validate_skill(sys.argv[1])
  79. print(message)
  80. sys.exit(0 if valid else 1)