|
| 1 | +import sys |
| 2 | +import yaml |
| 3 | + |
| 4 | +REQUIRED_FIELDS = ['time', 'title', 'type'] |
| 5 | + |
| 6 | + |
| 7 | +def main(): |
| 8 | + filepath = '_data/events.yaml' |
| 9 | + |
| 10 | + with open(filepath, encoding='utf-8') as f: |
| 11 | + try: |
| 12 | + events = yaml.safe_load(f) |
| 13 | + except yaml.YAMLError as e: |
| 14 | + print(f"YAML parsing error in {filepath}: {e}") |
| 15 | + print("Suggestion: Check for incorrect indentation, missing quotes, or invalid characters.") |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | + if not isinstance(events, list): |
| 19 | + print(f"ERROR: {filepath} must contain a list of events.") |
| 20 | + print("Suggestion: Ensure the file starts with a list (each entry begins with '- ').") |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + errors = [] |
| 24 | + for i, event in enumerate(events): |
| 25 | + if not isinstance(event, dict): |
| 26 | + errors.append( |
| 27 | + f"Event at index {i} is not a dictionary. " |
| 28 | + "Suggestion: Ensure each event entry is a YAML mapping (key: value pairs)." |
| 29 | + ) |
| 30 | + continue |
| 31 | + for field in REQUIRED_FIELDS: |
| 32 | + if field not in event or not event[field]: |
| 33 | + errors.append( |
| 34 | + f"Event at index {i} (title: '{event.get('title', 'unknown')}') " |
| 35 | + f"is missing required field '{field}'. " |
| 36 | + f"Suggestion: Add '{field}: <value>' to this event entry." |
| 37 | + ) |
| 38 | + if 'youtube' in event and event['youtube'] is not None: |
| 39 | + if not isinstance(event['youtube'], str): |
| 40 | + errors.append( |
| 41 | + f"Event at index {i} (title: '{event.get('title', 'unknown')}') " |
| 42 | + "has a non-string 'youtube' field. " |
| 43 | + "Suggestion: Set 'youtube' to a string URL (e.g. https://www.youtube.com/watch?v=...) " |
| 44 | + "or leave it empty / remove it entirely." |
| 45 | + ) |
| 46 | + |
| 47 | + if errors: |
| 48 | + print(f"Found {len(errors)} error(s) in {filepath}:\n") |
| 49 | + for err in errors: |
| 50 | + print(f" ERROR: {err}") |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + print(f"Validation passed: {len(events)} events OK.") |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments