The current quiz schema uses a set in the Pydantic model to enforce uniqueness for quiz questions. However, this leads to an issue where the questions and tags lose their order since sets are unordered collections. The goal is to switch to using a list while maintaining uniqueness through a custom Pydantic validator that removes duplicates.
Problem:
The quiz schema currently uses a set to ensure that questions and tags are unique
As sets are unordered, the order of questions and tags is lost when saving
Users expect that the order in which they input questions/tags should be maintained
Solution:
Replace the set with a list in the Pydantic schema
Implement a custom Pydantic validator to check for uniqueness while preserving the order of items
The validator should remove any duplicate questions or tags, maintaining the order of the first occurrence of each unique item
Requirements:
Update Schema:
Change the type of the questions and tags fields from set to list
Custom Validator:
Implement a validator that ensures uniqueness within the list without altering the order of the elements.
The validator should:
Iterate through the list
Keep only the first occurrence of any duplicate question or tag
Return the updated list of unique questions/tags in the original order
Acceptance Criteria:
[x] The quiz schema no longer uses a set for questions and tags
[x] A custom Pydantic validator is implemented to enforce uniqueness in the list while maintaining the order of input
[x] Duplicate questions/tags are removed, but the order of non-duplicate items remains unchanged
[x] Add/Update unit tests to ensure that both the order and uniqueness are properly enforced
[x] Confirm the order of questions and tags remains consistent in database entries
Additional Notes:
Ensure this fix doesn’t negatively affect other parts of the application that rely on the quiz schema
Review and update relevant API documentation to reflect this change
The current quiz schema uses a
set
in the Pydantic model to enforce uniqueness for quiz questions. However, this leads to an issue where the questions and tags lose their order since sets are unordered collections. The goal is to switch to using alist
while maintaining uniqueness through a custom Pydantic validator that removes duplicates.Problem:
set
to ensure that questions and tags are uniqueSolution:
set
with a list in the Pydantic schemaRequirements:
set
tolist
Acceptance Criteria:
set
for questions and tagslist
while maintaining the order of inputAdditional Notes: