Skip to content

Commit

Permalink
🐛 [MTA-1349] Fix Questionnaire/Assessment validation (konveyor#570)
Browse files Browse the repository at this point in the history
The validator used by Gin will validate nested structs, but will not
validate a slice of structs unless the binding field tag specifies the
`dive` keyword. This needs to be present on every level of nested slices
of structs.

Fixes https://issues.redhat.com/browse/MTA-1349

Signed-off-by: Sam Lucidi <slucidi@redhat.com>
  • Loading branch information
mansam authored Dec 6, 2023
1 parent 2eac93e commit 3391469
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/assessment.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type Assessment struct {
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
Questionnaire Ref `json:"questionnaire" binding:"required"`
Sections []assessment.Section `json:"sections"`
Sections []assessment.Section `json:"sections" binding:"dive"`
Stakeholders []Ref `json:"stakeholders"`
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
// read only
Expand Down
5 changes: 3 additions & 2 deletions api/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package api

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/assessment"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm/clause"
"net/http"
)

// Routes
Expand Down Expand Up @@ -192,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"`
Sections []assessment.Section `json:"sections" yaml:"sections" binding:"required,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"`
Expand Down
9 changes: 5 additions & 4 deletions assessment/assessment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package assessment

import (
"encoding/json"
"github.com/konveyor/tackle2-hub/model"
"math"

"github.com/konveyor/tackle2-hub/model"
)

//
Expand Down Expand Up @@ -129,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"`
Questions []Question `json:"questions" yaml:"questions" binding:"dive"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}

Expand Down Expand Up @@ -183,7 +184,7 @@ type Question struct {
Explanation string `json:"explanation" yaml:"explanation"`
IncludeFor []CategorizedTag `json:"includeFor,omitempty" yaml:"includeFor,omitempty" binding:"excluded_with=ExcludeFor"`
ExcludeFor []CategorizedTag `json:"excludeFor,omitempty" yaml:"excludeFor,omitempty" binding:"excluded_with=IncludeFor"`
Answers []Answer `json:"answers" yaml:"answers"`
Answers []Answer `json:"answers" yaml:"answers" binding:"dive"`
}

//
Expand Down Expand Up @@ -225,7 +226,7 @@ func (r *Question) Tags() (tags []CategorizedTag) {
type Answer struct {
Order uint `json:"order" yaml:"order" binding:"required"`
Text string `json:"text" yaml:"text"`
Risk string `json:"risk" yaml:"risk" binding:"oneof=red,yellow,green,unknown"`
Risk string `json:"risk" yaml:"risk" binding:"oneof=red yellow green unknown"`
Rationale string `json:"rationale" yaml:"rationale"`
Mitigation string `json:"mitigation" yaml:"mitigation"`
ApplyTags []CategorizedTag `json:"applyTags,omitempty" yaml:"applyTags,omitempty"`
Expand Down

0 comments on commit 3391469

Please sign in to comment.