Skip to content

Commit

Permalink
Make validator code more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
atomgomba committed Aug 31, 2022
1 parent 3b3297c commit cd3ab81
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import json
from urllib.error import URLError
from urllib.request import urlopen, Request
from http.client import InvalidURL
from datetime import datetime

import jsonschema
from jsonschema import validate as validate_json, SchemaError, ValidationError

CURRENT_VERSION = "v1"

Expand All @@ -32,21 +31,21 @@ def main(schema_path: str, input_path: str, check_broken_urls: bool) -> int:
return error(f"decode error: {e} (file: {input_path})")

try:
jsonschema.validate(data, schema)
except jsonschema.SchemaError as e:
validate_json(data, schema)
except SchemaError as e:
return error(f"bad schema: {e}")
except jsonschema.ValidationError as e:
except ValidationError as e:
return error(f"bad input: {e}")

seen_ids = set()
for fact in data:
fact_id = fact["id"]
if fact_id in seen_ids:
return error(f"duplicate id: {fact_id}")
seen_ids.add(fact_id)
iso_date = fact["meta"]["date"].get("iso")
if iso_date and not is_iso_date_valid(iso_date):
return error(f"invalid date for id: {fact_id} ({iso_date})")
seen_ids.add(fact_id)

if check_broken_urls:
for fact in data:
Expand All @@ -62,21 +61,21 @@ def main(schema_path: str, input_path: str, check_broken_urls: bool) -> int:


def is_url_broken(url: str) -> bool:
req = Request(url=url, method="HEAD")
resp = None
try:
resp = urlopen(req)
resp = urlopen(Request(url=url, method="HEAD"))
except URLError:
return True
return resp.status != 200
else:
return resp.status != 200


def is_iso_date_valid(input: str) -> bool:
def is_iso_date_valid(date_string: str) -> bool:
try:
datetime.fromisoformat(input)
except:
datetime.fromisoformat(date_string)
except ValueError:
return False
return True
else:
return True


def error(msg: str) -> int:
Expand All @@ -89,10 +88,12 @@ def error(msg: str) -> int:
description="Validate the Bridge Facts JSON file against the schema",
formatter_class=ArgumentDefaultsHelpFormatter,
)
parser.add_argument("input_file", help="path to a file to be validated")
parser.add_argument("-s", "--schema", metavar="path",
default=DEFAULT_SCHEMA_PATH, help="path to schema file")
parser.add_argument("-b", "--broken-urls", action="store_true", help="check also for broken URLs")
parser.add_argument("input_file",
help="path to a file to be validated")
parser.add_argument("-s", "--schema", metavar="path", default=DEFAULT_SCHEMA_PATH,
help="path to schema file")
parser.add_argument("-b", "--broken-urls", action="store_true",
help="check also for broken URLs")

args = parser.parse_args()

Expand Down

0 comments on commit cd3ab81

Please # to comment.