From ecb20713aa64c35cf7a9a804715c110244205205 Mon Sep 17 00:00:00 2001 From: John Donaldson Date: Thu, 18 Jul 2019 18:40:47 +0100 Subject: [PATCH] Adding unique answer validator --- devel/validate.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/devel/validate.py b/devel/validate.py index 60f2288..3f94e5b 100644 --- a/devel/validate.py +++ b/devel/validate.py @@ -79,6 +79,22 @@ class MothValidator: if len(puzzle.answers) == 0: raise MothValidationError("No answers provided") + @staticmethod + def check_unique_answers(puzzle): + """Check if puzzle answers are unique""" + known_answers = [] + duplicate_answers = [] + + for answer in puzzle.answers: + if answer not in known_answers: + known_answers.append(answer) + else: + duplicate_answers.append(answer) + + if len(duplicate_answers) > 0: + raise MothValidationError("Duplicate answer(s) %s" % ", ".join(duplicate_answers)) + + @staticmethod def check_has_authors(puzzle): """Check if the puzzle has authors defined"""