make validate more compact
This commit is contained in:
parent
be4d3544ae
commit
3151c8fe81
3 changed files with 73 additions and 32 deletions
|
@ -136,9 +136,18 @@ func IsValidUsername(name string) bool {
|
|||
return validUsernamePatternWithoutDots.MatchString(name) && !invalidUsernamePattern.MatchString(name)
|
||||
}
|
||||
|
||||
func ValidateNotEmpty(value string, fieldName string) error {
|
||||
func ValidateNotEmpty(value string, fieldName string) []string {
|
||||
if value == "" {
|
||||
return fmt.Errorf("Field %v may not be empty.", fieldName)
|
||||
return []string{fmt.Sprintf("Field %v may not be empty.", fieldName)}
|
||||
}
|
||||
return nil
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func ValidateOneOf(value string, allowed []string) []string {
|
||||
for _, allowedElem := range allowed {
|
||||
if value == allowedElem {
|
||||
return []string{}
|
||||
}
|
||||
}
|
||||
return []string{fmt.Sprintf("Value %v is not contained in allowed values [%v]", value, allowed)}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue