From 7382cf106380bf82517f3fa41dc113a702a6d98f Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Sat, 17 Jun 2023 02:06:08 +0000 Subject: [PATCH] Digits checker is added. --- README.md | 1 + checker.go | 1 + digits.go | 36 ++++++++++++++++++++++++++ digits_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++ doc/checkers/ascii.md | 2 +- doc/checkers/digits.md | 28 +++++++++++++++++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 digits.go create mode 100644 digits_test.go create mode 100644 doc/checkers/digits.md diff --git a/README.md b/README.md index cc915ee..20c7f73 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ type Person struct { This package currently provides the following checkers: - [ascii](doc/checkers/ascii.md) checks if the given string consists of only ASCII characters. +- [digits](doc/checkers/digits.md) checks if the given string consists of only digit characters. - [max](doc/checkers/max.md) checks if the given value is less than the given maximum. - [max-length](doc/checkers/maxlength.md) checks if the length of the given value is less than the given maximum length. - [min](doc/checkers/min.md) checks if the given value is greather than the given minimum. diff --git a/checker.go b/checker.go index cc714d8..d298934 100644 --- a/checker.go +++ b/checker.go @@ -32,6 +32,7 @@ const ResultValid Result = "VALID" // makers provides mapping to maker function for the checkers. var makers = map[string]MakeFunc{ CheckerAscii: makeAscii, + CheckerDigits: makeDigits, CheckerMax: makeMax, CheckerMaxLength: makeMaxLength, CheckerMin: makeMin, diff --git a/digits.go b/digits.go new file mode 100644 index 0000000..4ab56ac --- /dev/null +++ b/digits.go @@ -0,0 +1,36 @@ +package checker + +import ( + "reflect" +) + +// CheckerDigits is the name of the checker. +const CheckerDigits = "digits" + +// ResultNotDigits indicates that the given string contains non-digit characters. +const ResultNotDigits = "NOT_DIGITS" + +// IsDigits checks if the given string consists of only digit characters. +func IsDigits(value string) Result { + for i := 0; i < len(value); i++ { + if value[i] < '0' || value[i] > '9' { + return ResultNotDigits + } + } + + return ResultValid +} + +// makeDigits makes a checker function for the digits checker. +func makeDigits(_ string) CheckFunc { + return checkDigits +} + +// checkDigits checks if the given string consists of only digit characters. +func checkDigits(value, _ reflect.Value) Result { + if value.Kind() != reflect.String { + panic("string expected") + } + + return IsDigits(value.String()) +} diff --git a/digits_test.go b/digits_test.go new file mode 100644 index 0000000..9928989 --- /dev/null +++ b/digits_test.go @@ -0,0 +1,57 @@ +package checker + +import "testing" + +func TestIsDigitsInvalid(t *testing.T) { + if IsDigits("checker") == ResultValid { + t.Fail() + } +} + +func TestIsDigitsValid(t *testing.T) { + if IsDigits("1234") != ResultValid { + t.Fail() + } +} + +func TestCheckDigitsNonString(t *testing.T) { + defer FailIfNoPanic(t) + + type User struct { + Id int `checkers:"digits"` + } + + user := &User{} + + Check(user) +} + +func TestCheckDigitsInvalid(t *testing.T) { + type User struct { + Id string `checkers:"digits"` + } + + user := &User{ + Id: "checker", + } + + _, valid := Check(user) + if valid { + t.Fail() + } +} + +func TestCheckDigitsValid(t *testing.T) { + type User struct { + Id string `checkers:"digits"` + } + + user := &User{ + Id: "1234", + } + + _, valid := Check(user) + if !valid { + t.Fail() + } +} diff --git a/doc/checkers/ascii.md b/doc/checkers/ascii.md index 7ef5ca4..a28f6a2 100644 --- a/doc/checkers/ascii.md +++ b/doc/checkers/ascii.md @@ -1,6 +1,6 @@ # ASCII Checker -The ```ascii``` checkr checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the ```NOT_ASCII``` result. Here is an example: +The ```ascii``` checker checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the ```NOT_ASCII``` result. Here is an example: ```golang type User struct { diff --git a/doc/checkers/digits.md b/doc/checkers/digits.md new file mode 100644 index 0000000..d4999c3 --- /dev/null +++ b/doc/checkers/digits.md @@ -0,0 +1,28 @@ +# Digits Checker + +The ```digits``` checker checks if the given string consists of only digit characters. If the string contains non-digit characters, the checker will return the ```NOT_DIGITS``` result. Here is an example: + +```golang +type User struct { + Id string `checkers:"digits"` +} + +user := &User{ + Id: "1234", +} + +_, valid := Check(user) +if !valid { + // Send the mistakes back to the user +} +``` + +In your custom checkers, you can call the ```digits``` checker function ```IsDigits``` to validate the user input. Here is an example: + +```golang +result := IsDigits("1234") + +if result != ResultValid { + // Send the mistakes back to the user +} +```