From 5b49781fa658979a5ad8915c2725b42b9317f9fa Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Fri, 14 Jun 2024 14:47:00 +0200 Subject: [PATCH] /bin/python /home/marco/so-un-bot/scripts/validate_questions.py --- scripts/validate_questions.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/validate_questions.py b/scripts/validate_questions.py index 8bcf750..907b8c1 100644 --- a/scripts/validate_questions.py +++ b/scripts/validate_questions.py @@ -12,22 +12,25 @@ for filename in os.listdir("data/questions"): data = json.loads(text) for q in data: + if type(q["quest"]) is not str or type(q["image"]) is not str or type(q["answers"]) is not list or type(q["correct"]) is not int: + raise Exception(str(data.index(q)) + ": Some parameters are null, missing or their type is wrong.") + if q["quest"] == "" and q["image"] == "": raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.") - if q["quest"] is None or q["image"] is None or q["answers"] is None or q["correct"] is None: - raise Exception(str(data.index(q)) + ") Some parameters are null or missing.") if len(q["answers"]) == 0: - raise Exception(str(data.index(q)) + ") Question has no answers.") + raise Exception(str(data.index(q)) + ": Question has no answers.") for a in q["answers"]: if a["text"] == "" and a["image"] == "": - raise Exception(str(data.index(q)) + ") Answer's text and image cannot both be empty.") - if a["text"] is None or a["image"] is None: - raise Exception(str(data.index(q)) + ") Some answer's parameters are null or missing.") + raise Exception(str(data.index(q)) + ": Answer's text and image cannot both be empty.") + if type(a["text"]) is not str or type(a["image"]) is not str: + raise Exception(str(data.index(q)) + ": Some answer's parameters are null, missing or their type is wrong.") except Exception as e: logging.error(getattr(e, 'message', repr(e))) logging.fatal(filename + " is invalid. Aborting.") + exit(1) -logging.info("Parsing successful!") \ No newline at end of file +logging.info("Parsing successful!") +exit(0) \ No newline at end of file