Add FQDN validation checker and tests to v2. (#135)
# Describe Request Add FQDN validation checker and tests to v2. # Change Type New code.
This commit is contained in:
parent
c47eeb6451
commit
dac7298ca7
3 changed files with 120 additions and 0 deletions
43
v2/fqdn.go
Normal file
43
v2/fqdn.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// 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 (
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameFQDN is the name of the FQDN check.
|
||||
nameFQDN = "fqdn"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotFQDN indicates that the given value is not a valid FQDN.
|
||||
ErrNotFQDN = NewCheckError("FQDN")
|
||||
|
||||
// fqdnRegex is the regular expression for validating FQDN.
|
||||
fqdnRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
)
|
||||
|
||||
// IsFQDN checks if the value is a valid fully qualified domain name (FQDN).
|
||||
func IsFQDN(value string) (string, error) {
|
||||
if !fqdnRegex.MatchString(value) {
|
||||
return value, ErrNotFQDN
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// checkFQDN checks if the value is a valid fully qualified domain name (FQDN).
|
||||
func checkFQDN(value reflect.Value) (reflect.Value, error) {
|
||||
_, err := IsFQDN(value.Interface().(string))
|
||||
return value, err
|
||||
}
|
||||
|
||||
// makeFQDN makes a checker function for the FQDN checker.
|
||||
func makeFQDN(_ string) CheckFunc[reflect.Value] {
|
||||
return checkFQDN
|
||||
}
|
||||
76
v2/fqdn_test.go
Normal file
76
v2/fqdn_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 ExampleIsFQDN() {
|
||||
_, err := v2.IsFQDN("example.com")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFQDNInvalid(t *testing.T) {
|
||||
_, err := v2.IsFQDN("invalid_fqdn")
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFQDNValid(t *testing.T) {
|
||||
_, err := v2.IsFQDN("example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckFQDNNonString(t *testing.T) {
|
||||
defer FailIfNoPanic(t, "expected panic")
|
||||
|
||||
type Domain struct {
|
||||
Name int `checkers:"fqdn"`
|
||||
}
|
||||
|
||||
domain := &Domain{}
|
||||
|
||||
v2.CheckStruct(domain)
|
||||
}
|
||||
|
||||
func TestCheckFQDNInvalid(t *testing.T) {
|
||||
type Domain struct {
|
||||
Name string `checkers:"fqdn"`
|
||||
}
|
||||
|
||||
domain := &Domain{
|
||||
Name: "invalid_fqdn",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(domain)
|
||||
if ok {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckFQDNValid(t *testing.T) {
|
||||
type Domain struct {
|
||||
Name string `checkers:"fqdn"`
|
||||
}
|
||||
|
||||
domain := &Domain{
|
||||
Name: "example.com",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(domain)
|
||||
if !ok {
|
||||
t.Fatal("expected valid")
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameCIDR: makeCIDR,
|
||||
nameDigits: makeDigits,
|
||||
nameEmail: makeEmail,
|
||||
nameFQDN: makeFQDN,
|
||||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRequired: makeRequired,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue