From 9fc9bdbb1a7725e4d186be5bc644802f221eab12 Mon Sep 17 00:00:00 2001 From: Samuel Lucidi Date: Tue, 19 Dec 2023 08:48:02 -0500 Subject: [PATCH] :bug: [MTA-1907] Questionnaires require at least one section/question/answer (#582) Adds validation to ensure that Questionnaires have at least one section, each section has at least one question, and each question has at least one answer. Fixes a problem where submitting a questionnaire with a question with no answers would break the UI. Fixes https://issues.redhat.com/browse/MTA-1907 Signed-off-by: Sam Lucidi --- api/questionnaire.go | 2 +- assessment/assessment.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/questionnaire.go b/api/questionnaire.go index ff25a92c1..c7f69935c 100644 --- a/api/questionnaire.go +++ b/api/questionnaire.go @@ -193,7 +193,7 @@ type Questionnaire struct { Name string `json:"name" yaml:"name" binding:"required"` Description string `json:"description" yaml:"description"` Required bool `json:"required" yaml:"required"` - Sections []assessment.Section `json:"sections" yaml:"sections" binding:"required,dive"` + Sections []assessment.Section `json:"sections" yaml:"sections" binding:"required,min=1,dive"` Thresholds assessment.Thresholds `json:"thresholds" yaml:"thresholds" binding:"required"` RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages" binding:"required"` Builtin bool `json:"builtin,omitempty" yaml:"builtin,omitempty"` diff --git a/assessment/assessment.go b/assessment/assessment.go index a9e0f30e2..43fdef1d6 100644 --- a/assessment/assessment.go +++ b/assessment/assessment.go @@ -130,7 +130,7 @@ func (r *Assessment) Confidence() (score int) { type Section struct { Order *uint `json:"order" yaml:"order" binding:"required"` Name string `json:"name" yaml:"name"` - Questions []Question `json:"questions" yaml:"questions" binding:"dive"` + Questions []Question `json:"questions" yaml:"questions" binding:"min=1,dive"` Comment string `json:"comment,omitempty" yaml:"comment,omitempty"` } @@ -184,7 +184,7 @@ type Question struct { Explanation string `json:"explanation" yaml:"explanation"` IncludeFor []CategorizedTag `json:"includeFor,omitempty" yaml:"includeFor,omitempty"` ExcludeFor []CategorizedTag `json:"excludeFor,omitempty" yaml:"excludeFor,omitempty"` - Answers []Answer `json:"answers" yaml:"answers" binding:"dive"` + Answers []Answer `json:"answers" yaml:"answers" binding:"min=1,dive"` } //