Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithans authored and wtrocki committed Jun 29, 2021
1 parent 4d1de9d commit cd285f2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/src/validator/validator.md
Expand Up @@ -16,7 +16,7 @@ var vali rules.RuleConfig
```
or overriding default config
```go
var vali rules.RuleConfig = rules.RuleConfig{
vali := rules.RuleConfig{
Verbose: true,
MustExist: rules.MustExist{
Fields: []string{"Args"},
Expand Down
21 changes: 4 additions & 17 deletions validator/example/cmd_test.go
@@ -1,8 +1,6 @@
package example

import (
"bytes"
"io/ioutil"
"testing"

"github.com/aerogear/charmil/validator/rules"
Expand All @@ -12,15 +10,7 @@ func Test_ExecuteCommand(t *testing.T) {
cmd := NewCommand()

// Testing cobra commands with default recommended config
// default config can also be overrided by
/*
var vali rules.RuleConfig = rules.RuleConfig{
Verbose: true,
MustExist: rules.MustExist{
Fields: []string{"Args"},
},
}
*/
// default config can also be overrided
var vali rules.RuleConfig
validationErr := vali.ExecuteRules(cmd)
for _, errs := range validationErr {
Expand All @@ -29,11 +19,8 @@ func Test_ExecuteCommand(t *testing.T) {
}
}

b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.Execute()
_, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
if len(validationErr) == 0 {
t.Log("success, Test Passed!")
}

}
8 changes: 4 additions & 4 deletions validator/rules/executor.go
Expand Up @@ -51,10 +51,10 @@ func (config *RuleConfig) initDefaultRules() {
Verbose: false,
Length: Length{
Limits: map[string]Limit{
"Use": {Min: 2, Max: 20},
"Short": {Min: 15, Max: 200},
"Long": {Min: 100, Max: 10000},
"Example": {Min: 100, Max: 10000},
"Use": {Min: 2},
"Short": {Min: 15},
"Long": {Min: 100},
"Example": {Min: 100},
},
},
MustExist: MustExist{
Expand Down
4 changes: 2 additions & 2 deletions validator/rules/length.go
Expand Up @@ -74,7 +74,7 @@ func validateField(cmd *cobra.Command, limit Limit, value string, path string, f
if length < limit.Min {
return validator.ValidationError{Name: fmt.Sprintf("%s length should be at least %d", fieldName, limit.Min), Err: ErrLengthMin, Rule: LengthRule, Cmd: cmd}
}
if length > limit.Max {
if limit.Max != 0 && length > limit.Max {
return validator.ValidationError{Name: fmt.Sprintf("%s length should be less than %d", fieldName, limit.Max), Err: ErrLengthMax, Rule: LengthRule, Cmd: cmd}
}

Expand All @@ -90,7 +90,7 @@ func isLimitSet(cmd *cobra.Command, limit Limit) (bool, validator.ValidationErro
if limit.Max == 0 && limit.Min == 0 {
return false, validator.ValidationError{Name: "limit not set", Err: ErrLengthZeroValue, Rule: LengthRule, Cmd: cmd}
}
if limit.Max < limit.Min {
if limit.Max != 0 && limit.Max < limit.Min {
return true, validator.ValidationError{Name: "max limit must be greater than min limit", Err: ErrLengthMin, Rule: LengthRule, Cmd: cmd}
}

Expand Down

0 comments on commit cd285f2

Please sign in to comment.