Add Regexp checker and tests to v2. (#148)
# Describe Request Add Regexp checker and tests to v2. # Change Type New code.
This commit is contained in:
parent
08498b7929
commit
23f3064805
3 changed files with 112 additions and 0 deletions
|
|
@ -34,6 +34,7 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameMAC: makeMAC,
|
||||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRegexp: makeRegexp,
|
||||
nameRequired: makeRequired,
|
||||
nameTitle: makeTitle,
|
||||
nameTrimLeft: makeTrimLeft,
|
||||
|
|
|
|||
47
v2/regexp.go
Normal file
47
v2/regexp.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// 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"
|
||||
)
|
||||
|
||||
// nameRegexp is the name of the regexp check.
|
||||
const nameRegexp = "regexp"
|
||||
|
||||
// ErrNotMatch indicates that the given string does not match the regexp pattern.
|
||||
var ErrNotMatch = NewCheckError("REGEXP")
|
||||
|
||||
// MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
|
||||
func MakeRegexpChecker(expression string, invalidError error) CheckFunc[reflect.Value] {
|
||||
return func(value reflect.Value) (reflect.Value, error) {
|
||||
if value.Kind() != reflect.String {
|
||||
panic("string expected")
|
||||
}
|
||||
|
||||
matched, err := regexp.MatchString(expression, value.String())
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
|
||||
if !matched {
|
||||
return value, invalidError
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
// makeRegexp makes a checker function for the regexp.
|
||||
func makeRegexp(config string) CheckFunc[reflect.Value] {
|
||||
return MakeRegexpChecker(config, ErrNotMatch)
|
||||
}
|
||||
|
||||
// checkRegexp checks if the given string matches the regexp pattern.
|
||||
func checkRegexp(value reflect.Value) (reflect.Value, error) {
|
||||
return makeRegexp(value.String())(value)
|
||||
}
|
||||
64
v2/regexp_test.go
Normal file
64
v2/regexp_test.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// 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 (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
v2 "github.com/cinar/checker/v2"
|
||||
)
|
||||
|
||||
func TestCheckRegexpNonString(t *testing.T) {
|
||||
defer FailIfNoPanic(t, "expected panic")
|
||||
|
||||
type User struct {
|
||||
Username int `checkers:"regexp:^[A-Za-z]$"`
|
||||
}
|
||||
|
||||
user := &User{}
|
||||
|
||||
v2.CheckStruct(user)
|
||||
}
|
||||
|
||||
func TestCheckRegexpInvalid(t *testing.T) {
|
||||
type User struct {
|
||||
Username string `checkers:"regexp:^[A-Za-z]+$"`
|
||||
}
|
||||
|
||||
user := &User{
|
||||
Username: "abcd1234",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(user)
|
||||
if ok {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckRegexpValid(t *testing.T) {
|
||||
type User struct {
|
||||
Username string `checkers:"regexp:^[A-Za-z]+$"`
|
||||
}
|
||||
|
||||
user := &User{
|
||||
Username: "abcd",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(user)
|
||||
if !ok {
|
||||
t.Fatal("expected valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeRegexpChecker(t *testing.T) {
|
||||
checkHex := v2.MakeRegexpChecker("^[A-Fa-f0-9]+$", v2.ErrNotMatch)
|
||||
|
||||
_, err := checkHex(reflect.ValueOf("f0f0f0"))
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue