Skip to content

Commit

Permalink
Merge pull request #21 from cqframework/s/xsd-action
Browse files Browse the repository at this point in the history
GitHub actions schema validator for submitted XML tests
  • Loading branch information
ewoutkramer authored May 28, 2024
2 parents 20fbead + 81bfa32 commit eb84c1e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmlschema==3.3.1
31 changes: 31 additions & 0 deletions .github/python/xsd_validate.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions .github/workflows/xsd_validation.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb84c1e

Please sign in to comment.