Skip to content

Commit

Permalink
Upper and lower normalizers. Fixes #43 Fixes #44 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Jun 18, 2023
1 parent 21d89ba commit ef73947
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ This package currently provides the following checkers:

This package currently provides the following normalizers. They can be mixed with the checkers when defining the validation steps for user data.

- [lower](doc/normalizers/lower.md) maps all Unicode letters in the given value to their lower case.
- [upper](doc/normalizers/upper.md) maps all Unicode letters in the given value to their upper case.
- [trim](doc/normalizers/trim.md) removes the whitespaces at the beginning and at the end of the given value.
- [trim-left](doc/normalizers/trim_left.md) removes the whitespaces at the beginning of the given value.
- [trim-right](doc/normalizers/trim_right.md) removes the whitespaces at the end of the given value.
Expand Down
2 changes: 2 additions & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var makers = map[string]MakeFunc{
CheckerMinLength: makeMinLength,
CheckerRequired: makeRequired,
CheckerSame: makeSame,
NormalizerLower: makeLower,
NormalizerUpper: makeUpper,
NormalizerTrim: makeTrim,
NormalizerTrimLeft: makeTrimLeft,
NormalizerTrimRight: makeTrimRight,
Expand Down
15 changes: 15 additions & 0 deletions doc/normalizers/lower.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Lower Case Normalizer

The ```lower``` normalizer maps all Unicode letters in the given value to their lower case. It can be mixed with checkers and other normalizers when defining the validation steps for user data.

```golang
type User struct {
Username string `checkers:"lower"`
}

user := &User{
Username: "chECker",
}

Check(user)
```
15 changes: 15 additions & 0 deletions doc/normalizers/upper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Upper Case Normalizer

The ```upper``` normalizer maps all Unicode letters in the given value to their upper case. It can be mixed with checkers and other normalizers when defining the validation steps for user data.

```golang
type User struct {
Username string `checkers:"upper"`
}

user := &User{
Username: "chECker",
}

Check(user)
```
25 changes: 25 additions & 0 deletions lower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checker

import (
"reflect"
"strings"
)

// NormalizerLower is the name of the normalizer.
const NormalizerLower = "lower"

// makeLower makes a normalizer function for the lower normalizer.
func makeLower(_ string) CheckFunc {
return normalizeLower
}

// normalizeLower maps all Unicode letters in the given value to their lower case.
func normalizeLower(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

value.SetString(strings.ToLower(value.String()))

return ResultValid
}
46 changes: 46 additions & 0 deletions lower_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package checker

import "testing"

func TestNormalizeLowerNonString(t *testing.T) {
defer FailIfNoPanic(t)

type User struct {
Username int `checkers:"lower"`
}

user := &User{}

Check(user)
}

func TestNormalizeLowerResultValid(t *testing.T) {
type User struct {
Username string `checkers:"lower"`
}

user := &User{
Username: "chECker",
}

_, valid := Check(user)
if !valid {
t.Fail()
}
}

func TestNormalizeLower(t *testing.T) {
type User struct {
Username string `checkers:"lower"`
}

user := &User{
Username: "chECker",
}

Check(user)

if user.Username != "checker" {
t.Fail()
}
}
25 changes: 25 additions & 0 deletions upper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checker

import (
"reflect"
"strings"
)

// NormalizerUpper is the name of the normalizer.
const NormalizerUpper = "upper"

// makeUpper makes a normalizer function for the upper normalizer.
func makeUpper(_ string) CheckFunc {
return normalizeUpper
}

// normalizeUpper maps all Unicode letters in the given value to their upper case.
func normalizeUpper(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

value.SetString(strings.ToUpper(value.String()))

return ResultValid
}
46 changes: 46 additions & 0 deletions upper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package checker

import "testing"

func TestNormalizeUpperNonString(t *testing.T) {
defer FailIfNoPanic(t)

type User struct {
Username int `checkers:"upper"`
}

user := &User{}

Check(user)
}

func TestNormalizeUpperResultValid(t *testing.T) {
type User struct {
Username string `checkers:"upper"`
}

user := &User{
Username: "chECker",
}

_, valid := Check(user)
if !valid {
t.Fail()
}
}

func TestNormalizeUpper(t *testing.T) {
type User struct {
Username string `checkers:"upper"`
}

user := &User{
Username: "chECker",
}

Check(user)

if user.Username != "CHECKER" {
t.Fail()
}
}

0 comments on commit ef73947

Please sign in to comment.