Skip to content

Commit

Permalink
Fix Questionnaire/Assessment validation
Browse files Browse the repository at this point in the history
The validator used by Gin 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 <[email protected]>
  • Loading branch information
mansam committed Dec 4, 2023
1 parent 0ecb271 commit 94de6e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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 94de6e8

Please sign in to comment.