Adding unique answer validator

This commit is contained in:
John Donaldson 2019-07-18 18:40:47 +01:00 committed by Donaldson
parent 7701e98a2d
commit ecb20713aa
1 changed files with 16 additions and 0 deletions

View File

@ -79,6 +79,22 @@ class MothValidator:
if len(puzzle.answers) == 0: if len(puzzle.answers) == 0:
raise MothValidationError("No answers provided") 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 @staticmethod
def check_has_authors(puzzle): def check_has_authors(puzzle):
"""Check if the puzzle has authors defined""" """Check if the puzzle has authors defined"""