Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex check is added. #151

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions v2/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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"
)

const (
// nameHex is the name of the hex check.
nameHex = "hex"
)

var (
// ErrNotHex indicates that the given string contains hex characters.
ErrNotHex = NewCheckError("HEX")
)

// IsHex checks if the given string consists of only hex characters.
func IsHex(value string) (string, error) {
return IsRegexp("^[0-9a-fA-F]+$", value)
}

// isHex checks if the given string consists of only hex characters.
func isHex(value reflect.Value) (reflect.Value, error) {
_, err := IsHex(value.Interface().(string))
return value, err
}

// makeAlphanumeric makes a checker function for the alphanumeric checker.
func makeHex(_ string) CheckFunc[reflect.Value] {
return isHex
}
76 changes: 76 additions & 0 deletions v2/hex_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 ExampleIsHex() {
_, err := v2.IsHex("0123456789abcdefABCDEF")
if err != nil {
fmt.Println(err)
}
}

func TestIsHexInvalid(t *testing.T) {
_, err := v2.IsHex("ONUR")
if err == nil {
t.Fatal("expected error")
}
}

func TestIsHexValid(t *testing.T) {
_, err := v2.IsHex("0123456789abcdefABCDEF")
if err != nil {
t.Fatal(err)
}
}

func TestCheckHexNonString(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")

type Car struct {
Color int `checkers:"hex"`
}

car := &Car{}

v2.CheckStruct(car)
}

func TestCheckHexInvalid(t *testing.T) {
type Car struct {
Color string `checkers:"hex"`
}

car := &Car{
Color: "red",
}

_, ok := v2.CheckStruct(car)
if ok {
t.Fatal("expected error")
}
}

func TestCheckHexValid(t *testing.T) {
type Car struct {
Color string `checkers:"hex"`
}

car := &Car{
Color: "ABcd1234",
}

errs, ok := v2.CheckStruct(car)
if !ok {
t.Fatal(errs)
}
}
7 changes: 7 additions & 0 deletions v2/luhn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func TestIsLUHNInvalid(t *testing.T) {
}
}

func TestIsLUHNInvalidDigits(t *testing.T) {
_, err := v2.IsLUHN("ABCD")
if err == nil {
t.Fatal("expected error")
}
}

func TestIsLUHNValid(t *testing.T) {
_, err := v2.IsLUHN("4012888888881881")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var makers = map[string]MakeCheckFunc{
nameDigits: makeDigits,
nameEmail: makeEmail,
nameFQDN: makeFQDN,
nameHex: makeHex,
nameHTMLEscape: makeHTMLEscape,
nameHTMLUnescape: makeHTMLUnescape,
nameIP: makeIP,
Expand Down
20 changes: 10 additions & 10 deletions v2/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ const nameRegexp = "regexp"
// ErrNotMatch indicates that the given string does not match the regexp pattern.
var ErrNotMatch = NewCheckError("REGEXP")

// IsRegexp checks if the given string matches the given regexp expression.
func IsRegexp(expression, value string) (string, error) {
if !regexp.MustCompile(expression).MatchString(value) {
return value, ErrNotMatch
}

return value, nil
}

// MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
func MakeRegexpChecker(expression string, invalidError error) CheckFunc[reflect.Value] {
return func(value reflect.Value) (reflect.Value, error) {
if value.Kind() != reflect.String {
panic("string expected")
}

matched, err := regexp.MatchString(expression, value.String())
_, err := IsRegexp(expression, value.String())
if err != nil {
return value, err
}

if !matched {
return value, invalidError
}

Expand All @@ -40,8 +45,3 @@ func MakeRegexpChecker(expression string, invalidError error) CheckFunc[reflect.
func makeRegexp(config string) CheckFunc[reflect.Value] {
return MakeRegexpChecker(config, ErrNotMatch)
}

// checkRegexp checks if the given string matches the regexp pattern.
func checkRegexp(value reflect.Value) (reflect.Value, error) {
return makeRegexp(value.String())(value)
}
32 changes: 22 additions & 10 deletions v2/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,33 @@
package v2_test

import (
"reflect"
"fmt"
"testing"

v2 "github.com/cinar/checker/v2"
)

func ExampleIsRegexp() {
_, err := v2.IsRegexp("^[0-9a-fA-F]+$", "ABcd1234")
if err != nil {
fmt.Println(err)
}
}

func TestIsRegexpInvalid(t *testing.T) {
_, err := v2.IsRegexp("^[0-9a-fA-F]+$", "Onur")
if err == nil {
t.Fatal("expected error")
}
}

func TestIsRegexpValid(t *testing.T) {
_, err := v2.IsRegexp("^[0-9a-fA-F]+$", "ABcd1234")
if err != nil {
t.Fatal(err)
}
}

func TestCheckRegexpNonString(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")

Expand Down Expand Up @@ -53,12 +74,3 @@ func TestCheckRegexpValid(t *testing.T) {
t.Fatal("expected valid")
}
}

func TestMakeRegexpChecker(t *testing.T) {
checkHex := v2.MakeRegexpChecker("^[A-Fa-f0-9]+$", v2.ErrNotMatch)

_, err := checkHex(reflect.ValueOf("f0f0f0"))
if err != nil {
t.Fail()
}
}
Loading