bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/name/basic.go (about) 1 package name 2 3 import "errors" 4 5 type basicValidationConfig struct { 6 isEmptyValid bool 7 isValidRuneCheck func(rune) bool 8 } 9 10 // NewBasicValidator constructs a RuneLevelValidator which can validate names for being empty or containing isolated 11 // runes which fail a basic validation check 12 func NewBasicValidator(isEmptyValid bool, isValidRuneCheck func(rune) bool) (RuneLevelValidator, error) { 13 if isValidRuneCheck == nil { 14 return nil, errors.New("no isValidRuneCheck provided") 15 } 16 17 result := &basicValidationConfig{isEmptyValid: isEmptyValid, isValidRuneCheck: isValidRuneCheck} 18 return result, nil 19 } 20 21 // IsValid returns true if the given name is valid according the objects validation checks 22 func (c *basicValidationConfig) IsValid(name string) bool { 23 if len(name) == 0 { 24 return c.isEmptyValid 25 } 26 27 for _, r := range name { 28 if !c.isValidRuneCheck(r) { 29 return false 30 } 31 } 32 33 return true 34 } 35 36 // IsRuneValid returns true if the given rune is valid for use within a name 37 func (c *basicValidationConfig) IsRuneValid(r rune) bool { 38 return c.isValidRuneCheck(r) 39 }