-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Describe Request URL checker is added. Fixes #96 # Change Type Checker added.
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |