Add Digits validation checker and tests to v2.
This commit is contained in:
parent
9e75deef97
commit
ad183bfb1e
3 changed files with 119 additions and 0 deletions
42
v2/digits.go
Normal file
42
v2/digits.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// 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"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameDigits is the name of the digits check.
|
||||
nameDigits = "digits"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotDigits indicates that the given value is not a valid digits string.
|
||||
ErrNotDigits = NewCheckError("Digits")
|
||||
)
|
||||
|
||||
// IsDigits checks if the value contains only digit characters.
|
||||
func IsDigits(value string) (string, error) {
|
||||
for _, r := range value {
|
||||
if !unicode.IsDigit(r) {
|
||||
return value, ErrNotDigits
|
||||
}
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// checkDigits checks if the value contains only digit characters.
|
||||
func checkDigits(value reflect.Value) (reflect.Value, error) {
|
||||
_, err := IsDigits(value.Interface().(string))
|
||||
return value, err
|
||||
}
|
||||
|
||||
// makeDigits makes a checker function for the digits checker.
|
||||
func makeDigits(_ string) CheckFunc[reflect.Value] {
|
||||
return checkDigits
|
||||
}
|
||||
76
v2/digits_test.go
Normal file
76
v2/digits_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 ExampleIsDigits() {
|
||||
_, err := v2.IsDigits("123456")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDigitsInvalid(t *testing.T) {
|
||||
_, err := v2.IsDigits("123a456")
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDigitsValid(t *testing.T) {
|
||||
_, err := v2.IsDigits("123456")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckDigitsNonString(t *testing.T) {
|
||||
defer FailIfNoPanic(t, "expected panic")
|
||||
|
||||
type Code struct {
|
||||
Value int `checkers:"digits"`
|
||||
}
|
||||
|
||||
code := &Code{}
|
||||
|
||||
v2.CheckStruct(code)
|
||||
}
|
||||
|
||||
func TestCheckDigitsInvalid(t *testing.T) {
|
||||
type Code struct {
|
||||
Value string `checkers:"digits"`
|
||||
}
|
||||
|
||||
code := &Code{
|
||||
Value: "123a456",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(code)
|
||||
if ok {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckDigitsValid(t *testing.T) {
|
||||
type Code struct {
|
||||
Value string `checkers:"digits"`
|
||||
}
|
||||
|
||||
code := &Code{
|
||||
Value: "123456",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(code)
|
||||
if !ok {
|
||||
t.Fatal("expected valid")
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameAlphanumeric: makeAlphanumeric,
|
||||
nameASCII: makeASCII,
|
||||
nameCIDR: makeCIDR,
|
||||
nameDigits: makeDigits,
|
||||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRequired: makeRequired,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue