Skip to content

Commit 70a2c71

Browse files
husnimunFs02
authored andcommitted
Fix validateRequired (#66)
1 parent 11169eb commit 70a2c71

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

changeset/validate_required.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import (
77
// ValidateRequiredErrorMessage is the default error message for ValidateRequired.
88
var ValidateRequiredErrorMessage = "{field} is required"
99

10+
// isZeroer is the interface that wraps the basic isZero method.
11+
type isZeroer interface {
12+
IsZero() bool
13+
}
14+
1015
// ValidateRequired validates that one or more fields are present in the changeset.
11-
// It'll add error to changeset if field in the changes is nil or string made only of whitespace,
16+
// It'll add error to changeset if field in the changes is nil or string made only of whitespace.
1217
func ValidateRequired(ch *Changeset, fields []string, opts ...Option) {
1318
options := Options{
1419
message: ValidateRequiredErrorMessage,
@@ -24,7 +29,17 @@ func ValidateRequired(ch *Changeset, fields []string, opts ...Option) {
2429
}
2530

2631
str, isStr := val.(string)
27-
if exist && (isStr && strings.TrimSpace(str) != "") || (!isStr && val != nil) {
32+
if exist && isStr && strings.TrimSpace(str) != "" {
33+
continue
34+
}
35+
36+
zero, isZeroer := val.(isZeroer)
37+
if exist && isZeroer && !zero.IsZero() {
38+
continue
39+
}
40+
41+
// Only check zero value with isZero if val is not string since it has been checked before.
42+
if exist && !isStr && !isZero(val) {
2843
continue
2944
}
3045

changeset/validate_required_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package changeset
22

33
import (
4+
"fmt"
45
"testing"
6+
"time"
57

8+
"github.com/Fs02/grimoire/params"
69
"github.com/stretchr/testify/assert"
710
)
811

@@ -21,6 +24,22 @@ func TestValidateRequired(t *testing.T) {
2124
assert.Nil(t, ch.Errors())
2225
}
2326

27+
func TestValidateRequired_cast(t *testing.T) {
28+
type customString string
29+
type customType struct {
30+
Field1 customString
31+
Field2 customString
32+
Field3 customString
33+
FieldTime time.Time
34+
}
35+
36+
ct := customType{Field1: "field1", FieldTime: time.Now()}
37+
ch := Cast(ct, params.Map{"field2": "field2", "field3": "field3"}, []string{"field1", "field2", "field3", "field_time"})
38+
ValidateRequired(ch, []string{"field1", "field2", "field3", "field_time"})
39+
40+
assert.Nil(t, ch.Errors())
41+
}
42+
2443
func TestValidateRequired_error(t *testing.T) {
2544
ch := &Changeset{
2645
changes: map[string]interface{}{
@@ -34,6 +53,7 @@ func TestValidateRequired_error(t *testing.T) {
3453
assert.Equal(t, 3, len(ch.Errors()))
3554
assert.Equal(t, "field1 is required", ch.Errors()[0].Error())
3655
assert.Equal(t, "field2 is required", ch.Errors()[1].Error())
56+
assert.Equal(t, "field3 is required", ch.Errors()[2].Error())
3757

3858
// empty struct
3959
ch = &Changeset{
@@ -53,3 +73,23 @@ func TestValidateRequired_error(t *testing.T) {
5373
assert.Equal(t, "field2 is required", ch.Errors()[0].Error())
5474
assert.Equal(t, "field3 is required", ch.Errors()[1].Error())
5575
}
76+
77+
func TestValidateRequired_cast_error(t *testing.T) {
78+
type customString string
79+
type customType struct {
80+
Field1 customString
81+
Field2 customString
82+
Field3 customString
83+
FieldTime time.Time
84+
}
85+
86+
ct := customType{Field1: "field1"}
87+
fmt.Println(ct.FieldTime)
88+
ch := Cast(ct, params.Map{"field2": "field2"}, []string{"field1", "field2", "field3", "field_time"})
89+
ValidateRequired(ch, []string{"field1", "field2", "field3", "field_time"})
90+
91+
assert.NotNil(t, ch.Errors())
92+
assert.Equal(t, 2, len(ch.Errors()))
93+
assert.Equal(t, "field3 is required", ch.Errors()[0].Error())
94+
assert.Equal(t, "field_time is required", ch.Errors()[1].Error())
95+
}

0 commit comments

Comments
 (0)