-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate JSONs against schema and sanity check them.
- Loading branch information
Showing
2 changed files
with
80 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,18 @@ | ||
name: Validate | ||
|
||
on: | ||
push: | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Validate JSON files | ||
run: | | ||
pip install jsonschema==4.6.1 | ||
python schemas/validate.py |
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,62 @@ | ||
from jsonschema import validate as json_validate | ||
import json | ||
import os | ||
import urllib.request | ||
|
||
|
||
GENERIC_EA_SCHEMA = 'generic-ea-schema.json' | ||
LATEST_EA_JSON = 'latest-ea.json' | ||
LATEST_EA_SCHEMA = 'latest-ea-schema.json' | ||
ROOT_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) | ||
SCHEMAS_DIR = 'schemas' | ||
VERSIONS_DIR = 'versions' | ||
|
||
|
||
def validate(json_name, schema_name): | ||
print(f'Validating {json_name}...') | ||
with open(os.path.join(ROOT_PATH, VERSIONS_DIR, json_name)) as f: | ||
json_contents = json.load(f) | ||
with open(os.path.join(ROOT_PATH, SCHEMAS_DIR, schema_name)) as f: | ||
schema_contents = json.load(f) | ||
json_validate(json_contents, schema_contents) | ||
print(f' {json_name} validates against {schema_name}') | ||
if schema_name == LATEST_EA_SCHEMA: | ||
builds = [json_contents] | ||
else: | ||
builds = json_contents | ||
ensure_one_latest_build(json_name, builds) | ||
validate_builds(builds) | ||
|
||
|
||
def ensure_one_latest_build(json_name, builds): | ||
num_latest = sum([1 if build['latest'] else 0 for build in builds]) | ||
assert num_latest == 1, f'Expected one latest build in {json_name}, got {num_latest}' | ||
|
||
|
||
def validate_builds(builds): | ||
for build in builds: | ||
version = build['version'] | ||
download_base_url = build['download_base_url'] | ||
files = build['files'] | ||
assert version in download_base_url, f'version not found in download_base_url: {json.dumps(build)}' | ||
assert all(version in file['filename'] for file in files), f'version not found in all filenames: {json.dumps(build)}' | ||
check_urls_exist(download_base_url, files) | ||
|
||
|
||
def check_urls_exist(download_base_url, files): | ||
for file in files: | ||
download_url = f'{download_base_url}{file["filename"]}' | ||
print(f' Checking {download_url}...') | ||
request = urllib.request.Request(download_url, method='HEAD') | ||
response = urllib.request.urlopen(request) | ||
assert response.status == 200, f'Expected status code of 200, got {response.status} for {download_url}' | ||
|
||
|
||
if __name__ == '__main__': | ||
for file_name in os.listdir('versions'): | ||
assert file_name.endswith('.json'), 'Unexpected file' | ||
|
||
schema_name = LATEST_EA_SCHEMA if file_name == LATEST_EA_JSON else GENERIC_EA_SCHEMA | ||
validate(file_name, schema_name) | ||
|
||
print('JSON validation successful') |