-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from cqframework/s/xsd-action
GitHub actions schema validator for submitted XML tests
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
xmlschema==3.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |