|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import yaml |
| 7 | +from jsonschema import Draft202012Validator |
| 8 | + |
| 9 | + |
| 10 | +def _split_frontmatter_and_body(md_path: Path) -> tuple[dict, str]: |
| 11 | + text = md_path.read_text(encoding="utf-8") |
| 12 | + if not text.startswith("---"): |
| 13 | + raise ValueError(f"{md_path} does not start with YAML frontmatter ('---').") |
| 14 | + |
| 15 | + first_sep = text.find("\n---", 3) |
| 16 | + if first_sep == -1: |
| 17 | + raise ValueError(f"{md_path} frontmatter block not properly terminated with '---'.") |
| 18 | + |
| 19 | + frontmatter_block = text[0:first_sep] |
| 20 | + body = text[first_sep + len("\n---") :].lstrip("\n") |
| 21 | + |
| 22 | + frontmatter = yaml.safe_load(frontmatter_block.lstrip("-\n")) or {} |
| 23 | + return frontmatter, body |
| 24 | + |
| 25 | + |
| 26 | +def _extract_headings(markdown_body: str) -> list[str]: |
| 27 | + """Return the text of headings that start with '## ' only.""" |
| 28 | + headings: list[str] = [] |
| 29 | + for line in markdown_body.splitlines(): |
| 30 | + # we care only about second-level headings, literally starting with "## " |
| 31 | + if line.startswith("## "): |
| 32 | + heading_text = line[len("## ") :].strip() |
| 33 | + if heading_text: |
| 34 | + headings.append(heading_text) |
| 35 | + return headings |
| 36 | + |
| 37 | + |
| 38 | +def _check_heading_order(markdown_body: str, expected_order: list[str]) -> list[str]: |
| 39 | + """ |
| 40 | + Check that: |
| 41 | + - for each expected heading text E in expected_order, |
| 42 | + - there is a corresponding '## ' heading whose text CONTAINS E, |
| 43 | + - and they appear in the same order. |
| 44 | +
|
| 45 | + Extra headings are allowed; mismatch in order or missing headings is an error. |
| 46 | + """ |
| 47 | + errors: list[str] = [] |
| 48 | + actual = _extract_headings(markdown_body) |
| 49 | + |
| 50 | + # Greedy left-to-right match where each expected string must appear |
| 51 | + # as a substring of some heading, in order. |
| 52 | + idx = 0 |
| 53 | + matched: list[str] = [] |
| 54 | + for expected in expected_order: |
| 55 | + found_at = None |
| 56 | + while idx < len(actual): |
| 57 | + if expected in actual[idx]: |
| 58 | + found_at = idx |
| 59 | + matched.append(actual[idx]) |
| 60 | + idx += 1 |
| 61 | + break |
| 62 | + idx += 1 |
| 63 | + if found_at is None: |
| 64 | + errors.append( |
| 65 | + f"Missing or out-of-order heading containing: {expected!r}.\n" |
| 66 | + f" All '## ' headings: {actual}" |
| 67 | + ) |
| 68 | + # stop early; further checks won't be meaningful |
| 69 | + return errors |
| 70 | + |
| 71 | + # Optional: if you want to ensure there are no unexpected extra headings |
| 72 | + # or stricter equality, you could add more checks here. |
| 73 | + return errors |
| 74 | + |
| 75 | + |
| 76 | +def main(argv: list[str] | None = None) -> int: |
| 77 | + argv = argv or sys.argv[1:] |
| 78 | + if len(argv) != 2: |
| 79 | + print("Usage: validate_recipe SCHEMA_JSON_PATH AI_CURATOR_RECIPE.md", file=sys.stderr) |
| 80 | + return 1 |
| 81 | + |
| 82 | + schema_path = Path(argv[0]) |
| 83 | + recipe_path = Path(argv[1]) |
| 84 | + |
| 85 | + if not schema_path.is_file(): |
| 86 | + print(f"Schema file not found: {schema_path}", file=sys.stderr) |
| 87 | + return 1 |
| 88 | + if not recipe_path.is_file(): |
| 89 | + print(f"Markdown file not found: {recipe_path}", file=sys.stderr) |
| 90 | + return 1 |
| 91 | + |
| 92 | + schema = json.loads(schema_path.read_text(encoding="utf-8")) |
| 93 | + frontmatter, body = _split_frontmatter_and_body(recipe_path) |
| 94 | + |
| 95 | + # JSON Schema: front-matter |
| 96 | + validator = Draft202012Validator(schema) |
| 97 | + schema_errors = sorted(validator.iter_errors(frontmatter), key=lambda e: e.path) |
| 98 | + |
| 99 | + # Heading order from schema definitions.required_headings.enum |
| 100 | + required_headings = ( |
| 101 | + schema.get("definitions", {}) |
| 102 | + .get("required_headings", {}) |
| 103 | + .get("enum", []) |
| 104 | + ) |
| 105 | + heading_errors: list[str] = [] |
| 106 | + if required_headings: |
| 107 | + heading_errors = _check_heading_order(body, required_headings) |
| 108 | + |
| 109 | + if not schema_errors and not heading_errors: |
| 110 | + print(f"{recipe_path} is valid according to {schema_path} and heading rules") |
| 111 | + return 0 |
| 112 | + |
| 113 | + print(f"{recipe_path} is INVALID:") |
| 114 | + for err in schema_errors: |
| 115 | + loc = ".".join(map(str, err.path)) or "<root>" |
| 116 | + print(f" - schema:{loc}: {err.message}") |
| 117 | + for msg in heading_errors: |
| 118 | + print(f" - heading: {msg}") |
| 119 | + return 1 |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + raise SystemExit(main()) |
0 commit comments