Skip to content

Commit

Permalink
[knf] Add method 'Validators.AddIf'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 20, 2024
1 parent 55cf6bc commit f672300
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@ func (v Validators) Add(validators Validators) Validators {
return append(v, validators...)
}

// AddIf adds given validators if conditional is true
func (v Validators) AddIf(cond bool, validators Validators) Validators {
if !cond {
return v
}

return v.Add(validators)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Merge merges two configurations
Expand Down
6 changes: 5 additions & 1 deletion knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,11 @@ func (s *KNFSuite) TestSimpleValidator(c *check.C) {
{"string:test2", simpleValidator, nil},
}

validators = validators.Add(Validators{
validators = validators.AddIf(false, Validators{
{"string:test2", simpleValidator, nil},
})

validators = validators.AddIf(true, Validators{
{"string:test2", simpleValidator, nil},
})

Expand Down

0 comments on commit f672300

Please sign in to comment.