From 43cc0a56527559c7ef3e55b2ca7ae3822d64928b Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Sun, 19 Jun 2022 10:48:17 +0100 Subject: [PATCH] Added detailed data validation --- ref-test/app/tools/data.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/ref-test/app/tools/data.py b/ref-test/app/tools/data.py index d72c60f..8702741 100644 --- a/ref-test/app/tools/data.py +++ b/ref-test/app/tools/data.py @@ -19,6 +19,39 @@ def validate_json(file): file.stream.seek(0) data = json.loads(file.read()) if not isinstance(data, list): return False + for block in data: + block_type = block.pop('type', None) + if block_type not in ['block', 'question']: return False + if block_type == 'question': + if not all (key in block for key in ['q_no', 'text', 'options', 'correct', 'q_type', 'tags']): return False + if not isinstance(block['q_no'], int): return False + if not isinstance(block['text'], str): return False + if not isinstance(block['options'], list): return False + for option in block['options']: + if not isinstance(option, str): return False + if not isinstance(block['correct'], int): return False + if not isinstance(block['q_type'], str): return False + if block['q_type'] not in ['Multiple Choice', 'Yes/No', 'List']: return False + if not isinstance(block['tags'], list): return False + for tag in block['tags']: + if not isinstance(tag, str): return False + if block_type == 'block': + if not all (key in block for key in ['question_header', 'questions']): return False + if not isinstance(block['question_header'], str): return False + if not isinstance(block['questions'], list): return False + for question in block['questions']: + if not all (key in question for key in ['q_no', 'text', 'options', 'correct', 'q_type', 'tags']): return False + if not isinstance(question['text'], str): return False + if not isinstance(question['q_no'], int): return False + if not isinstance(question['options'], list): return False + for option in question['options']: + if not isinstance(option, str): return False + if not isinstance(question['correct'], int): return False + if not isinstance(question['q_type'], str): return False + if question['q_type'] not in ['Multiple Choice', 'Yes/No', 'List']: return False + if not isinstance(question['tags'], list): return False + for tag in question['tags']: + if not isinstance(tag, str): return False return True def randomise_list(list:list):