@@ -13,13 +13,20 @@ import (
13
13
"testing"
14
14
"time"
15
15
16
+ // "github.com/bytedance/go-tagexpr/v2/binding/gjson"
17
+ vd "github.com/bytedance/go-tagexpr/v2/validator"
18
+ "github.com/davecgh/go-spew/spew"
16
19
"github.com/henrylee2cn/ameda"
17
20
"github.com/henrylee2cn/goutil/httpbody"
18
21
"github.com/stretchr/testify/assert"
19
22
20
23
"github.com/bytedance/go-tagexpr/v2/binding"
21
24
)
22
25
26
+ func init () {
27
+ // gjson.UseJSONUnmarshaler()
28
+ }
29
+
23
30
func TestRawBody (t * testing.T ) {
24
31
type Recv struct {
25
32
S []byte `raw_body:""`
@@ -1107,7 +1114,7 @@ func TestIssue25(t *testing.T) {
1107
1114
1108
1115
func TestIssue26 (t * testing.T ) {
1109
1116
type Recv struct {
1110
- Type string `json:"type,required" vd:"($=='update_target_threshold' && (target_threshold )$!='-1') || ($=='update_status' && (status )$!='-1')"`
1117
+ Type string `json:"type,required" vd:"($=='update_target_threshold' && (TargetThreshold )$!='-1') || ($=='update_status' && (Status )$!='-1')"`
1111
1118
RuleName string `json:"rule_name,required" vd:"regexp('^rule[0-9]+$')"`
1112
1119
TargetThreshold string `json:"target_threshold" vd:"regexp('^-?[0-9]+(\\.[0-9]+)?$')"`
1113
1120
Status string `json:"status" vd:"$=='0' || $=='1'"`
@@ -1133,18 +1140,24 @@ func TestIssue26(t *testing.T) {
1133
1140
1134
1141
recv := new (Recv )
1135
1142
err := json .Unmarshal (b , recv )
1143
+ assert .NoError (t , err )
1144
+ err = vd .Validate (& recv , true )
1145
+ assert .NoError (t , err )
1146
+ t .Log (spew .Sdump (recv ))
1147
+
1136
1148
header := make (http.Header )
1137
1149
header .Set ("Content-Type" , "application/json" )
1138
1150
header .Set ("A" , "from header" )
1139
1151
cookies := []* http.Cookie {
1140
1152
{Name : "A" , Value : "from cookie" },
1141
1153
}
1142
- // gjson.UseJSONUnmarshaler()
1154
+
1143
1155
req := newRequest ("/1" , header , cookies , bytes .NewReader (b ))
1144
1156
binder := binding .New (nil )
1145
1157
recv2 := new (Recv )
1146
1158
err = binder .BindAndValidate (& recv2 , req , nil )
1147
1159
assert .NoError (t , err )
1160
+ t .Log (spew .Sdump (recv2 ))
1148
1161
assert .Equal (t , recv , recv2 )
1149
1162
}
1150
1163
0 commit comments