Skip to content

Commit

Permalink
Validate JSONs against schema and sanity check them.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Mar 6, 2024
1 parent bfd5201 commit 4182891
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Validate

on:
push:
pull_request:
workflow_dispatch:

jobs:
build:
name: Validate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install jsonschema
run: pip install jsonschema==4.6.1

- name: Validate JSON files
run: python schemas/validate.py
62 changes: 62 additions & 0 deletions schemas/validate.py
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')

0 comments on commit 4182891

Please sign in to comment.