ASCII checker moved to v2 version. (#131)
# Describe Request ASCII checker moved to v2 version. # Change Type New code.
This commit is contained in:
parent
8a7b88acbe
commit
af8e0e9fbf
3 changed files with 120 additions and 0 deletions
43
v2/ascii.go
Normal file
43
v2/ascii.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"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameASCII is the name of the ASCII check.
|
||||
nameASCII = "ascii"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotASCII indicates that the given string contains non-ASCII characters.
|
||||
ErrNotASCII = NewCheckError("ASCII")
|
||||
)
|
||||
|
||||
// IsASCII checks if the given string consists of only ASCII characters.
|
||||
func IsASCII(value string) (string, error) {
|
||||
for _, c := range value {
|
||||
if c > unicode.MaxASCII {
|
||||
return value, ErrNotASCII
|
||||
}
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// checkASCII checks if the given string consists of only ASCII characters.
|
||||
func isASCII(value reflect.Value) (reflect.Value, error) {
|
||||
_, err := IsASCII(value.Interface().(string))
|
||||
return value, err
|
||||
}
|
||||
|
||||
// makeASCII makes a checker function for the ASCII checker.
|
||||
func makeASCII(_ string) CheckFunc[reflect.Value] {
|
||||
return isASCII
|
||||
}
|
||||
76
v2/ascii_test.go
Normal file
76
v2/ascii_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 ExampleIsASCII() {
|
||||
_, err := v2.IsASCII("Checker")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsASCIIInvalid(t *testing.T) {
|
||||
_, err := v2.IsASCII("𝄞 Music!")
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsASCIIValid(t *testing.T) {
|
||||
_, err := v2.IsASCII("Checker")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckASCIINonString(t *testing.T) {
|
||||
defer FailIfNoPanic(t, "expected panic")
|
||||
|
||||
type User struct {
|
||||
Username int `checkers:"ascii"`
|
||||
}
|
||||
|
||||
user := &User{}
|
||||
|
||||
v2.CheckStruct(user)
|
||||
}
|
||||
|
||||
func TestCheckASCIIInvalid(t *testing.T) {
|
||||
type User struct {
|
||||
Username string `checkers:"ascii"`
|
||||
}
|
||||
|
||||
user := &User{
|
||||
Username: "𝄞 Music!",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(user)
|
||||
if ok {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckASCIIValid(t *testing.T) {
|
||||
type User struct {
|
||||
Username string `checkers:"ascii"`
|
||||
}
|
||||
|
||||
user := &User{
|
||||
Username: "checker",
|
||||
}
|
||||
|
||||
_, ok := v2.CheckStruct(user)
|
||||
if !ok {
|
||||
t.Fatal("expected valid")
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ type MakeCheckFunc func(params string) CheckFunc[reflect.Value]
|
|||
// makers provides a mapping of maker functions keyed by the check name.
|
||||
var makers = map[string]MakeCheckFunc{
|
||||
nameAlphanumeric: makeAlphanumeric,
|
||||
nameASCII: makeASCII,
|
||||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRequired: makeRequired,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue