Skip to content

Commit

Permalink
Same checker is added. (#30)
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
cinar authored Jun 15, 2023
1 parent b5eec29 commit 601d14d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ type Person struct {
This package currently provides the following checkers:

- [required](docs/checkers/required.md) checks if the required value is provided.
- [same](docs/checkers/same.md) checks if the given value is equal to the value of the field with the given name.
1 change: 1 addition & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const ResultValid Result = "VALID"
// makers provides mapping to maker function for the checkers.
var makers = map[string]MakeFunc{
"required": makeRequired,
"same": makeSame,
}

// Register registers the given checker name and the maker function.
Expand Down
20 changes: 20 additions & 0 deletions doc/checkers/same.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Same Checker

The ```same``` checker checks if the given value is equal to the value of the other field specified by its name. If they are not equal, the checker will return the ```NOT_SAME``` result. In the example below, the ```same``` checker ensures that the value in the ```Confirm``` field matches the value in the ```Password``` field.

```golang
type User struct {
Password string
Confirm string `checkers:"same:Password"`
}

user := &User{
Password: "1234",
Confirm: "1234",
}

mistakes, valid := checker.Check(user)
if !valid {
// Send the mistakes back to the user
}
```
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions same.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package checker

import (
"reflect"
)

// ResultNotSame indicates that the given two values are not equal to each other.
const ResultNotSame = "NOT_SAME"

// makeSame makes a checker function for the same checker.
func makeSame(config string) CheckFunc {
return func(value, parent reflect.Value) Result {
return checkSame(value, parent, config)
}
}

// checkSame checks if the given value is equal to the value of the field with the given name.
func checkSame(value, parent reflect.Value, name string) Result {
other := parent.FieldByName(name)

if !other.IsValid() {
panic("other field not found")
}

other = reflect.Indirect(other)

if !value.Equal(other) {
return ResultNotSame
}

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

import (
"reflect"
"testing"
)

func TestSameValid(t *testing.T) {
type User struct {
Password string
Confirm string `checkers:"same:Password"`
}

user := &User{
Password: "1234",
Confirm: "1234",
}

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

func TestSameInvalid(t *testing.T) {
type User struct {
Password string
Confirm string `checkers:"same:Password"`
}

user := &User{
Password: "1234",
Confirm: "12",
}

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

func TestSameWithoutParent(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fail()
}
}()

type User struct {
Password string
Confirm string `checkers:"same:Password"`
}

user := &User{
Password: "1234",
Confirm: "12",
}

checkSame(reflect.ValueOf(user.Confirm), reflect.ValueOf(nil), "Password")
}

func TestSameInvalidName(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fail()
}
}()

type User struct {
Password string
Confirm string `checkers:"same:Unknown"`
}

user := &User{
Password: "1234",
Confirm: "1234",
}

Check(user)
}

0 comments on commit 601d14d

Please sign in to comment.