Skip to content

Commit

Permalink
ASCII checker moved to v2 version. (#131)
Browse files Browse the repository at this point in the history
# Describe Request

ASCII checker moved to v2 version.

# Change Type

New code.
  • Loading branch information
cinar authored Dec 27, 2024
1 parent 8a7b88a commit af8e0e9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
43 changes: 43 additions & 0 deletions v2/ascii.go
Original file line number Diff line number Diff line change
@@ -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 changes: 76 additions & 0 deletions v2/ascii_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
1 change: 1 addition & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit af8e0e9

Please sign in to comment.