diff --git a/.github/python/requirements.txt b/.github/python/requirements.txt new file mode 100644 index 0000000..3dc7512 --- /dev/null +++ b/.github/python/requirements.txt @@ -0,0 +1 @@ +xmlschema==3.3.1 diff --git a/.github/python/xsd_validate.py b/.github/python/xsd_validate.py new file mode 100644 index 0000000..77bb45f --- /dev/null +++ b/.github/python/xsd_validate.py @@ -0,0 +1,31 @@ +import argparse +import os +import xmlschema + +def validate_xml_files(xsd_path, xml_dir): + """Validates XML files in a directory against a given XSD schema.""" + schema = xmlschema.XMLSchema(xsd_path) + at_least_one_invalid = False + for filename in os.listdir(xml_dir): + if filename.endswith(".xml"): + xml_path = os.path.join(xml_dir, filename) + try: + schema.validate(xml_path) + print(f"✅ {filename} is valid") + except xmlschema.XMLSchemaValidationError as e: + print(f"❌ {filename} is NOT valid:\n{e}") + at_least_one_invalid = True + + if at_least_one_invalid: + os._exit(1) + +def main(): + parser = argparse.ArgumentParser(description="Validate XML files against an XSD schema.") + parser.add_argument("-xsd_file", help="Path to the XSD schema file") + parser.add_argument("-xml_directory", help="Directory containing XML files to validate") + args = parser.parse_args() + + validate_xml_files(args.xsd_file, args.xml_directory) + +if __name__ == "__main__": + main() diff --git a/.github/workflows/xsd_validation.yaml b/.github/workflows/xsd_validation.yaml new file mode 100644 index 0000000..ccf1865 --- /dev/null +++ b/.github/workflows/xsd_validation.yaml @@ -0,0 +1,16 @@ +name: XML Validation + +on: [push, pull_request] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.12 + uses: actions/setup-python@v5 + with: + python-version: "3.12" + - name: Install dependencies + run: pip install -r .github/python/requirements.txt + - name: Validate CQL Test XML + run: python -u .github/python/xsd_validate.py -xsd_file tests/testSchema.xsd -xml_directory tests/cql