Skip to content

Commit

Permalink
URL checker is added. (#97)
Browse files Browse the repository at this point in the history
# Describe Request

URL checker is added.

Fixes #96

# Change Type

Checker added.
  • Loading branch information
cinar authored Jun 24, 2023
1 parent 144cab8 commit c823d27
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ This package currently provides the following checkers:
- [regexp](doc/checkers/regexp.md) checks if the given string matches the regexp pattern.
- [required](doc/checkers/required.md) checks if the required value is provided.
- [same](doc/checkers/same.md) checks if the given value is equal to the value of the field with the given name.
- [url](doc/checkers/url.md) checks if the given value is a valid URL.

# Normalizers Provided

Expand Down
1 change: 1 addition & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var makers = map[string]MakeFunc{
CheckerRegexp: makeRegexp,
CheckerRequired: makeRequired,
CheckerSame: makeSame,
CheckerURL: makeURL,
NormalizerLower: makeLower,
NormalizerUpper: makeUpper,
NormalizerTitle: makeTitle,
Expand Down
29 changes: 29 additions & 0 deletions doc/checkers/url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# URL Checker

The ```url``` checker checks if the given value is a valid URL. If the given value is not a valid URL, the checker will return the ```NOT_URL``` result. The checker uses [ParseRequestURI](https://pkg.go.dev/net/url#ParseRequestURI) function to parse the URL, and then checks if the schema or the host are both set.

Here is an example:

```golang
type Bookmark struct {
URL string `checkers:"url"`
}

bookmark := &Bookmark{
URL: "https://zdo.com",
}

_, valid := checker.Check(bookmark)
if !valid {
// Send the mistakes back to the user
}
```

In your custom checkers, you can call the ```url``` checker function ```IsURL``` to validate the user input. Here is an example:

```golang
result := checker.IsURL("https://zdo.com")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
```
44 changes: 44 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package checker

import (
"net/url"
"reflect"
)

// CheckerURL is the name of the checker.
const CheckerURL = "url"

// ResultNotURL indicates that the given value is not a valid URL.
const ResultNotURL = "NOT_URL"

// IsURL checks if the given value is a valid URL.
func IsURL(value string) Result {
url, err := url.ParseRequestURI(value)
if err != nil {
return ResultNotURL
}

if url.Scheme == "" {
return ResultNotURL
}

if url.Host == "" {
return ResultNotURL
}

return ResultValid
}

// makeURL makes a checker function for the URL checker.
func makeURL(_ string) CheckFunc {
return checkURL
}

// checkURL checks if the given value is a valid URL.
func checkURL(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsURL(value.String())
}
93 changes: 93 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package checker_test

import (
"testing"

"github.com/cinar/checker"
)

func TestIsURLValid(t *testing.T) {
result := checker.IsURL("https://zdo.com")
if result != checker.ResultValid {
t.Fail()
}
}

func TestIsURLInvalid(t *testing.T) {
result := checker.IsURL("https:://index.html")
if result == checker.ResultValid {
t.Fail()
}
}

func TestCheckURLNonString(t *testing.T) {
defer checker.FailIfNoPanic(t)

type Bookmark struct {
URL int `checkers:"url"`
}

bookmark := &Bookmark{}

checker.Check(bookmark)
}

func TestCheckURLValid(t *testing.T) {
type Bookmark struct {
URL string `checkers:"url"`
}

bookmark := &Bookmark{
URL: "https://zdo.com",
}

_, valid := checker.Check(bookmark)
if !valid {
t.Fail()
}
}

func TestCheckURLInvalid(t *testing.T) {
type Bookmark struct {
URL string `checkers:"url"`
}

bookmark := &Bookmark{
URL: "zdo.com/index.html",
}

_, valid := checker.Check(bookmark)
if valid {
t.Fail()
}
}

func TestCheckURLWithoutSchema(t *testing.T) {
type Bookmark struct {
URL string `checkers:"url"`
}

bookmark := &Bookmark{
URL: "//zdo.com/index.html",
}

_, valid := checker.Check(bookmark)
if valid {
t.Fail()
}
}

func TestCheckURLWithoutHost(t *testing.T) {
type Bookmark struct {
URL string `checkers:"url"`
}

bookmark := &Bookmark{
URL: "https:://index.html",
}

_, valid := checker.Check(bookmark)
if valid {
t.Fail()
}
}

0 comments on commit c823d27

Please sign in to comment.