def validate_subjects_json_structure()

in glan-instruct/glan.py [0:0]


def validate_subjects_json_structure(data):
    """
    Check if the JSON data has the correct structure for the subjects.
    """
    # Check if the top-level key "subjects" exists and is a list
    if "subjects" not in data or not isinstance(data["subjects"], list):
        return False
    
    # Iterate through each subject to validate its structure
    for subject in data["subjects"]:
        # Check if each subject is a dictionary
        if not isinstance(subject, dict):
            return False
        # Check if required keys exist in each subject and have the correct types
        if "subject" not in subject or not isinstance(subject["subject"], str):
            return False
        if "level" not in subject or not isinstance(subject["level"], int):
            return False
        if "subtopics" not in subject or not isinstance(subject["subtopics"], list):
            return False
        # Check if each item in "subtopics" is a string
        if not all(isinstance(subtopic, str) for subtopic in subject["subtopics"]):
            return False
    
    return True