Skip to content

Commit

Permalink
feat(config): print more info about validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Sep 20, 2023
1 parent 67099f1 commit 7b57fa6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,23 +394,32 @@ func validate(value interface{}) error {
continue
}

var value string
var strVal string

if fieldType.Type.Kind() == reflect.Int {
value = strconv.FormatInt(fieldValue.Int(), 10)
strVal = strconv.FormatInt(fieldValue.Int(), 10)
} else if fieldType.Type.Kind() == reflect.Uint {
value = strconv.FormatUint(fieldValue.Uint(), 10)
strVal = strconv.FormatUint(fieldValue.Uint(), 10)
} else {
value = fieldValue.String()
strVal = fieldValue.String()
}

match, _ := regexp.MatchString(rule, value)
if !match {
return fmt.Errorf(
"value of field '%v' is not valid! (Must match regex: '%v')",
fieldType.Name, rule,
)
match, _ := regexp.MatchString(rule, strVal)

if match {
continue
}

fieldName := strings.ToLower(fieldType.Name)

if strings.Contains(fieldName, "password") || strings.Contains(fieldName, "secret") || strings.Contains(fieldName, "key") {
strVal = "***"
}

return fmt.Errorf(
"value of field '%v' is not valid: %v (Must match regex: '%v')",
fieldType.Name, strVal, rule,
)
}

return nil
Expand Down

0 comments on commit 7b57fa6

Please sign in to comment.