github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_url.go (about) 1 package inputvalidation 2 3 import ( 4 "errors" 5 "regexp" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 8 9 "github.com/asaskevich/govalidator" 10 validation "github.com/go-ozzo/ozzo-validation/v4" 11 "github.com/go-ozzo/ozzo-validation/v4/is" 12 ) 13 14 type urlValidator struct{} 15 16 // IsURL missing godoc 17 var IsURL = &urlValidator{} 18 19 const protocolRegex = govalidator.URLSchema 20 21 const errMsg = "must be a valid URL" 22 23 // Validate missing godoc 24 func (v *urlValidator) Validate(value interface{}) error { 25 s, isNil, err := ensureIsString(value) 26 if err != nil { 27 return err 28 } 29 if isNil { 30 return nil 31 } 32 33 matched, err := regexp.Match(protocolRegex, []byte(s)) 34 if err != nil { 35 return apperrors.InternalErrorFrom(err, "error during checking URL: %s", value) 36 } 37 38 if !matched { 39 return errors.New(errMsg) 40 } 41 42 return validation.Validate(value, is.URL) 43 }