Add trim left and right normalizers and tests to v2.
This commit is contained in:
parent
1cff930c51
commit
989f7d05ef
5 changed files with 160 additions and 0 deletions
|
|
@ -34,6 +34,8 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRequired: makeRequired,
|
||||
nameTrimLeft: makeTrimLeft,
|
||||
nameTrimRight: makeTrimRight,
|
||||
nameTrimSpace: makeTrimSpace,
|
||||
nameURL: makeURL,
|
||||
nameURLEscape: makeURLEscape,
|
||||
|
|
|
|||
32
v2/trim_left.go
Normal file
32
v2/trim_left.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameTrimLeft is the name of the trim left normalizer.
|
||||
nameTrimLeft = "trim-left"
|
||||
)
|
||||
|
||||
// TrimLeft returns the value of the string with whitespace removed from the beginning.
|
||||
func TrimLeft(value string) (string, error) {
|
||||
return strings.TrimLeft(value, " \t"), nil
|
||||
}
|
||||
|
||||
// reflectTrimLeft returns the value of the string with whitespace removed from the beginning.
|
||||
func reflectTrimLeft(value reflect.Value) (reflect.Value, error) {
|
||||
newValue, err := TrimLeft(value.Interface().(string))
|
||||
return reflect.ValueOf(newValue), err
|
||||
}
|
||||
|
||||
// makeTrimLeft returns the trim left normalizer function.
|
||||
func makeTrimLeft(_ string) CheckFunc[reflect.Value] {
|
||||
return reflectTrimLeft
|
||||
}
|
||||
47
v2/trim_left_test.go
Normal file
47
v2/trim_left_test.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_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
v2 "github.com/cinar/checker/v2"
|
||||
)
|
||||
|
||||
func TestTrimLeft(t *testing.T) {
|
||||
input := " test "
|
||||
expected := "test "
|
||||
|
||||
actual, err := v2.TrimLeft(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("actual %s expected %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectTrimLeft(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string `checkers:"trim-left"`
|
||||
}
|
||||
|
||||
person := &Person{
|
||||
Name: " test ",
|
||||
}
|
||||
|
||||
expected := "test "
|
||||
|
||||
errs, ok := v2.CheckStruct(person)
|
||||
if !ok {
|
||||
t.Fatalf("got unexpected errors %v", errs)
|
||||
}
|
||||
|
||||
if person.Name != expected {
|
||||
t.Fatalf("actual %s expected %s", person.Name, expected)
|
||||
}
|
||||
}
|
||||
32
v2/trim_right.go
Normal file
32
v2/trim_right.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameTrimRight is the name of the trim right normalizer.
|
||||
nameTrimRight = "trim-right"
|
||||
)
|
||||
|
||||
// TrimRight returns the value of the string with whitespace removed from the end.
|
||||
func TrimRight(value string) (string, error) {
|
||||
return strings.TrimRight(value, " \t"), nil
|
||||
}
|
||||
|
||||
// reflectTrimRight returns the value of the string with whitespace removed from the end.
|
||||
func reflectTrimRight(value reflect.Value) (reflect.Value, error) {
|
||||
newValue, err := TrimRight(value.Interface().(string))
|
||||
return reflect.ValueOf(newValue), err
|
||||
}
|
||||
|
||||
// makeTrimRight returns the trim right normalizer function.
|
||||
func makeTrimRight(_ string) CheckFunc[reflect.Value] {
|
||||
return reflectTrimRight
|
||||
}
|
||||
47
v2/trim_right_test.go
Normal file
47
v2/trim_right_test.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_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
v2 "github.com/cinar/checker/v2"
|
||||
)
|
||||
|
||||
func TestTrimRight(t *testing.T) {
|
||||
input := " test "
|
||||
expected := " test"
|
||||
|
||||
actual, err := v2.TrimRight(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("actual %s expected %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectTrimRight(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string `checkers:"trim-right"`
|
||||
}
|
||||
|
||||
person := &Person{
|
||||
Name: " test ",
|
||||
}
|
||||
|
||||
expected := " test"
|
||||
|
||||
errs, ok := v2.CheckStruct(person)
|
||||
if !ok {
|
||||
t.Fatalf("got unexpected errors %v", errs)
|
||||
}
|
||||
|
||||
if person.Name != expected {
|
||||
t.Fatalf("actual %s expected %s", person.Name, expected)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue