Skip to content

Commit 94915d3

Browse files
committed
added json-schema validation in CI
1 parent 2fdcf1a commit 94915d3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

.github/workflows/json-schema.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Json Schema
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
json-schema:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.11'
19+
20+
- name: Install Python dependencies
21+
run: |
22+
pip install jsonschema
23+
24+
- name: Run proxy URL tests
25+
run: python tests/test_jsonschema.py --wallets-file=wallets-v2.json --json-schema-file=wallets-v2.schema.json

tests/test_jsonschema.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import argparse
2+
import json
3+
import sys
4+
from pathlib import Path
5+
6+
import jsonschema
7+
from jsonschema import Draft7Validator
8+
9+
10+
class JsonSchemaNamespace(argparse.Namespace):
11+
wallets_file: Path
12+
json_schema_file: Path
13+
14+
15+
def create_argument_parser() -> argparse.ArgumentParser:
16+
parser = argparse.ArgumentParser(
17+
description="Checking a json file using json-schema",
18+
formatter_class=argparse.RawDescriptionHelpFormatter,
19+
epilog="""
20+
Examples:
21+
python test_jsonschema.py --wallets-file=wallets-v2.json --json-schema-file=wallets-v2.schema.json
22+
""".strip(),
23+
)
24+
25+
parser.add_argument(
26+
"--wallets-file",
27+
type=Path,
28+
required=True,
29+
help="Path to the 'wallets-v2.json' file",
30+
)
31+
32+
parser.add_argument(
33+
"--json-schema-file",
34+
type=Path,
35+
required=True,
36+
help="Path to the 'wallets-v2.json' schema file",
37+
)
38+
39+
return parser
40+
41+
42+
def main() -> None:
43+
parser = create_argument_parser()
44+
args = parser.parse_args(namespace=JsonSchemaNamespace())
45+
46+
wallets = json.loads(args.wallets_file.read_bytes())
47+
json_schema = json.loads(args.json_schema_file.read_bytes())
48+
49+
error_message: str | None = None
50+
try:
51+
jsonschema.validate(instance=wallets, schema=json_schema, cls=Draft7Validator)
52+
except jsonschema.exceptions.ValidationError as e:
53+
error_message = str(e)
54+
55+
if error_message is None:
56+
print("ALL TESTS PASSED")
57+
sys.exit(0)
58+
else:
59+
print("TESTS FAILED:\n")
60+
print(error_message)
61+
sys.exit(1)
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)