Validate a single Markdown file's front matter.
(md_file: Path, errors: list, warnings: list)
| 75 | |
| 76 | |
| 77 | def validate_md_file(md_file: Path, errors: list, warnings: list) -> bool: |
| 78 | """Validate a single Markdown file's front matter.""" |
| 79 | content = md_file.read_text(encoding="utf-8") |
| 80 | fm = extract_front_matter(content) |
| 81 | |
| 82 | if fm is None: |
| 83 | warnings.append(f" WARN: No front matter in {md_file.relative_to(ROOT)}") |
| 84 | return True # Not an error, might be a README |
| 85 | |
| 86 | # Check required fields |
| 87 | missing = [f for f in REQUIRED_FRONT_MATTER_FIELDS if f not in fm] |
| 88 | if missing: |
| 89 | errors.append( |
| 90 | f" ERROR: {md_file.relative_to(ROOT)}: missing fields: {', '.join(missing)}" |
| 91 | ) |
| 92 | return False |
| 93 | |
| 94 | # Check title has zh key |
| 95 | if isinstance(fm.get("title"), dict): |
| 96 | if not fm["title"].get("zh") and not fm["title"].get("en"): |
| 97 | errors.append( |
| 98 | f" ERROR: {md_file.relative_to(ROOT)}: title must have 'zh' or 'en'" |
| 99 | ) |
| 100 | return False |
| 101 | elif isinstance(fm.get("title"), str): |
| 102 | warnings.append( |
| 103 | f" WARN: {md_file.relative_to(ROOT)}: title should be object with zh/en keys" |
| 104 | ) |
| 105 | |
| 106 | # Check status value |
| 107 | valid_statuses = {"active", "superseded", "draft"} |
| 108 | if fm.get("status") not in valid_statuses: |
| 109 | errors.append( |
| 110 | f" ERROR: {md_file.relative_to(ROOT)}: invalid status '{fm.get('status')}'" |
| 111 | f" (must be one of: {', '.join(valid_statuses)})" |
| 112 | ) |
| 113 | return False |
| 114 | |
| 115 | # Check regulation value |
| 116 | valid_regulations = {"eu_mdr", "fda", "nmpa", "shared"} |
| 117 | if fm.get("regulation") not in valid_regulations: |
| 118 | errors.append( |
| 119 | f" ERROR: {md_file.relative_to(ROOT)}: invalid regulation '{fm.get('regulation')}'" |
| 120 | ) |
| 121 | return False |
| 122 | |
| 123 | # Check source_url is not empty |
| 124 | if not fm.get("source_url"): |
| 125 | warnings.append( |
| 126 | f" WARN: {md_file.relative_to(ROOT)}: source_url is empty" |
| 127 | ) |
| 128 | |
| 129 | return True |
| 130 | |
| 131 | |
| 132 | def validate_index_file(index_file: Path, errors: list, warnings: list) -> bool: |
no test coverage detected