From b709836dede603d710f78f24a5eb7908392d8167 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Thu, 26 Dec 2024 19:12:46 -0800 Subject: [PATCH] Add URL validation checker and tests to v2. (#140) # Describe Request Add URL validation checker and tests to v2. # Change Type New code. --- v2/maker.go | 1 + v2/url.go | 41 +++++++++++++++++++++++++++ v2/url_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 v2/url.go create mode 100644 v2/url_test.go diff --git a/v2/maker.go b/v2/maker.go index b20feb6..a056507 100644 --- a/v2/maker.go +++ b/v2/maker.go @@ -32,6 +32,7 @@ var makers = map[string]MakeCheckFunc{ nameMinLen: makeMinLen, nameRequired: makeRequired, nameTrimSpace: makeTrimSpace, + nameURL: makeURL, } // makeChecks take a checker config and returns the check functions. diff --git a/v2/url.go b/v2/url.go new file mode 100644 index 0000000..6c32ff8 --- /dev/null +++ b/v2/url.go @@ -0,0 +1,41 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2 + +import ( + "net/url" + "reflect" +) + +const ( + // nameURL is the name of the URL check. + nameURL = "url" +) + +var ( + // ErrNotURL indicates that the given value is not a valid URL. + ErrNotURL = NewCheckError("URL") +) + +// IsURL checks if the value is a valid URL. +func IsURL(value string) (string, error) { + _, err := url.ParseRequestURI(value) + if err != nil { + return value, ErrNotURL + } + return value, nil +} + +// checkURL checks if the value is a valid URL. +func checkURL(value reflect.Value) (reflect.Value, error) { + _, err := IsURL(value.Interface().(string)) + return value, err +} + +// makeURL makes a checker function for the URL checker. +func makeURL(_ string) CheckFunc[reflect.Value] { + return checkURL +} diff --git a/v2/url_test.go b/v2/url_test.go new file mode 100644 index 0000000..d3777c1 --- /dev/null +++ b/v2/url_test.go @@ -0,0 +1,76 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2_test + +import ( + "fmt" + "testing" + + v2 "github.com/cinar/checker/v2" +) + +func ExampleIsURL() { + _, err := v2.IsURL("https://example.com") + if err != nil { + fmt.Println(err) + } +} + +func TestIsURLInvalid(t *testing.T) { + _, err := v2.IsURL("invalid-url") + if err == nil { + t.Fatal("expected error") + } +} + +func TestIsURLValid(t *testing.T) { + _, err := v2.IsURL("https://example.com") + if err != nil { + t.Fatal(err) + } +} + +func TestCheckURLNonString(t *testing.T) { + defer FailIfNoPanic(t, "expected panic") + + type Website struct { + Link int `checkers:"url"` + } + + website := &Website{} + + v2.CheckStruct(website) +} + +func TestCheckURLInvalid(t *testing.T) { + type Website struct { + Link string `checkers:"url"` + } + + website := &Website{ + Link: "invalid-url", + } + + _, ok := v2.CheckStruct(website) + if ok { + t.Fatal("expected error") + } +} + +func TestCheckURLValid(t *testing.T) { + type Website struct { + Link string `checkers:"url"` + } + + website := &Website{ + Link: "https://example.com", + } + + _, ok := v2.CheckStruct(website) + if !ok { + t.Fatal("expected valid") + } +}