From af8e0e9fbf27a6d9e41bc167a012947ca9b335bb Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Thu, 26 Dec 2024 16:04:46 -0800 Subject: [PATCH] ASCII checker moved to v2 version. (#131) # Describe Request ASCII checker moved to v2 version. # Change Type New code. --- v2/ascii.go | 43 +++++++++++++++++++++++++++ v2/ascii_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ v2/maker.go | 1 + 3 files changed, 120 insertions(+) create mode 100644 v2/ascii.go create mode 100644 v2/ascii_test.go diff --git a/v2/ascii.go b/v2/ascii.go new file mode 100644 index 0000000..5bb7946 --- /dev/null +++ b/v2/ascii.go @@ -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 +} diff --git a/v2/ascii_test.go b/v2/ascii_test.go new file mode 100644 index 0000000..cffca70 --- /dev/null +++ b/v2/ascii_test.go @@ -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") + } +} diff --git a/v2/maker.go b/v2/maker.go index bec2435..631363a 100644 --- a/v2/maker.go +++ b/v2/maker.go @@ -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,