Add URL validation checker and tests to v2. (#140)
# Describe Request Add URL validation checker and tests to v2. # Change Type New code.
This commit is contained in:
parent
29a1a9ba0d
commit
b709836ded
3 changed files with 118 additions and 0 deletions
|
|
@ -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.
|
||||
|
|
|
|||
41
v2/url.go
Normal file
41
v2/url.go
Normal file
|
|
@ -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
|
||||
}
|
||||
76
v2/url_test.go
Normal file
76
v2/url_test.go
Normal file
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue