Skip to content

Commit

Permalink
consolidate validation for trigger values with validation for section…
Browse files Browse the repository at this point in the history
… name uniqueness in survey class
  • Loading branch information
RuthShryock committed Oct 9, 2024
1 parent 04b82d9 commit 4199320
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
7 changes: 0 additions & 7 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ def validate(self):
# question type dictionary.
if self.type not in QUESTION_TYPE_DICT:
raise PyXFormError(f"Unknown question type '{self.type}'.")
# ensure that background-geopoint questions have triggers that correspond to an existing questions
if self.type == "background-geopoint":
trigger_cleaned = self.trigger.strip("${}")
if not self.get_root().question_exists(trigger_cleaned):
raise PyXFormError(
f"background-geopoint question '{self.name}' must have a trigger corresponding to an existing question."
)

def xml_instance(self, **kwargs):
survey = self.get_root()
Expand Down
30 changes: 19 additions & 11 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,21 @@ def validate(self):
if self.id_string in [None, "None"]:
raise PyXFormError("Survey cannot have an empty id_string")
super().validate()
self._validate_uniqueness_of_section_names()
self._validate_section_and_trigger_names()

def _validate_uniqueness_of_section_names(self):
def _validate_section_and_trigger_names(self):
root_node_name = self.name
section_names = []
existing_question_names = set()
bg_geopoint_elements = []

for element in self.iter_descendants():
if "name" in element:
existing_question_names.add(element["name"])

if element["type"] == "background-geopoint":
bg_geopoint_elements.append(element)

if isinstance(element, Section):
if element.name in section_names:
if element.name == root_node_name:
Expand All @@ -245,6 +254,14 @@ def _validate_uniqueness_of_section_names(self):
raise PyXFormError(msg)
section_names.append(element.name)

# Ensure that background-geopoint questions have triggers that correspond to an existing questions
for bg_geo in bg_geopoint_elements:
trigger_cleaned = bg_geo.get("trigger", "").strip("${}")
if trigger_cleaned not in existing_question_names:
raise PyXFormError(
f"background-geopoint question '{bg_geo['name']}' must have a trigger corresponding to an existing question."
)

def get_nsmap(self):
"""Add additional namespaces"""
namespaces = getattr(self, constants.NAMESPACES, "")
Expand Down Expand Up @@ -306,15 +323,6 @@ def get_trigger_values_for_question_name(self, question_name, trigger_type):

return trigger_map.get(trigger_type, {}).get(f"${{{question_name}}}")

def question_exists(self, question_name: str) -> bool:
"""
Check if a question with the given name exists in the survey.
"""
for element in self.iter_descendants():
if isinstance(element, Question) and element.name == question_name:
return True
return False

def _generate_static_instances(self, list_name, choice_list) -> InstanceInfo:
"""
Generate <instance> elements for static data (e.g. choices for selects)
Expand Down

0 comments on commit 4199320

Please sign in to comment.