skills-vault/scripts/quick_validate.py

77 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Quick validation for a Codex skill directory."""
from __future__ import annotations
import re
import sys
from pathlib import Path
import yaml
MAX_SKILL_NAME_LENGTH = 64
def validate_skill(skill_path: Path) -> tuple[bool, str]:
skill_md = skill_path / "SKILL.md"
if not skill_md.exists():
return False, "SKILL.md not found"
content = skill_md.read_text(encoding="utf-8")
if not content.startswith("---"):
return False, "No YAML frontmatter found"
match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
if not match:
return False, "Invalid frontmatter format"
try:
frontmatter = yaml.safe_load(match.group(1))
except yaml.YAMLError as exc:
return False, f"Invalid YAML in frontmatter: {exc}"
if not isinstance(frontmatter, dict):
return False, "Frontmatter must be a YAML dictionary"
allowed_keys = {"name", "description"}
unexpected_keys = set(frontmatter) - allowed_keys
if unexpected_keys:
return False, "Unexpected frontmatter key(s): " + ", ".join(
sorted(unexpected_keys)
)
name = frontmatter.get("name")
if not isinstance(name, str) or not name.strip():
return False, "Missing or invalid 'name' in frontmatter"
if not re.fullmatch(r"[a-z0-9-]+", name):
return False, "Name must use lowercase letters, digits, and hyphens only"
if name.startswith("-") or name.endswith("-") or "--" in name:
return False, "Name cannot start/end with hyphen or contain consecutive hyphens"
if len(name) > MAX_SKILL_NAME_LENGTH:
return False, f"Name exceeds {MAX_SKILL_NAME_LENGTH} characters"
description = frontmatter.get("description")
if not isinstance(description, str) or not description.strip():
return False, "Missing or invalid 'description' in frontmatter"
if "<" in description or ">" in description:
return False, "Description cannot contain angle brackets"
if len(description) > 1024:
return False, "Description exceeds 1024 characters"
return True, "Skill is valid!"
def main(argv: list[str]) -> int:
if len(argv) != 1:
print("Usage: python scripts/quick_validate.py <skill-directory>", file=sys.stderr)
return 2
valid, message = validate_skill(Path(argv[0]))
print(message)
return 0 if valid else 1
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))