-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |