diff --git a/go.mod b/go.mod index 7db9c00..3a9be79 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module sneak.berlin/go/util go 1.14 -require github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 +require ( + github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect + github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 +) diff --git a/go.sum b/go.sum index 300d53f..267d253 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= +github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 h1:60gBOooTSmNtrqNaRvrDbi8VAne0REaek2agjnITKSw= github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE= diff --git a/util.go b/util.go index cffbad4..535a00e 100644 --- a/util.go +++ b/util.go @@ -8,6 +8,7 @@ import ( "regexp" "time" + "github.com/asaskevich/govalidator" "github.com/hako/durafmt" ) @@ -24,6 +25,10 @@ func TimeDiffHuman(first time.Time, second time.Time) string { } } +func IsValidURL(url string) bool { + return govalidator.IsURL(url) && (govalidator.IsRequestURL(url) || govalidator.IsRequestURI(url)) +} + // Does anyone else use uints for things that are always >=0 like counts and func TimeDiffAbsSeconds(first time.Time, second time.Time) uint { return uint(math.Abs(first.Sub(second).Truncate(time.Second).Seconds()))