-
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.
Fixes #14
- Loading branch information
Showing
7 changed files
with
134 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,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.
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,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 | ||
} |
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,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) | ||
} |