github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_string.go (about) 1 package inputvalidation 2 3 import ( 4 "reflect" 5 6 "github.com/pkg/errors" 7 k8svalidation "k8s.io/apimachinery/pkg/api/validation" 8 ) 9 10 // DNSName missing godoc 11 var DNSName = &dnsNameRule{} 12 13 type dnsNameRule struct{} 14 15 // Validate missing godoc 16 func (v *dnsNameRule) Validate(value interface{}) error { 17 s, isNil, err := ensureIsString(value) 18 if err != nil { 19 return err 20 } 21 if isNil { 22 return nil 23 } 24 25 if len(s) > 36 { 26 return errors.New("must be no more than 36 characters") 27 } 28 if s[0] >= '0' && s[0] <= '9' { 29 return errors.New("cannot start with digit") 30 } 31 if errorMsg := k8svalidation.NameIsDNSSubdomain(s, false); errorMsg != nil { 32 return errors.Errorf("%v", errorMsg) 33 } 34 return nil 35 } 36 37 func ensureIsString(in interface{}) (val string, isNil bool, err error) { 38 t := reflect.ValueOf(in) 39 if t.Kind() == reflect.Ptr { 40 if t.IsNil() { 41 return "", true, nil 42 } 43 t = t.Elem() 44 } 45 46 if t.Kind() != reflect.String { 47 return "", false, errors.New("type has to be a string") 48 } 49 50 return t.String(), false, nil 51 }