This repository was archived by the owner on Jul 22, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdynamic_fields_test.go
More file actions
158 lines (136 loc) · 4.07 KB
/
Copy pathdynamic_fields_test.go
File metadata and controls
158 lines (136 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package godantic
import (
"github.com/stretchr/testify/assert"
"testing"
)
type MyDynamicField struct {
Value any `json:"value"`
ValueType string `json:"valueType" enums:"numeric,string,float,boolean"`
Attribute string `json:"attribute"`
}
func (mdf MyDynamicField) GetValue() interface{} {
return mdf.Value
}
func (mdf MyDynamicField) GetValueType() string {
return mdf.ValueType
}
func (mdf MyDynamicField) GetAttribute() string {
return mdf.Attribute
}
func TestDynamicField(t *testing.T) {
g := &Validate{}
t.Run("should validate dynamic numeric type", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: 18,
ValueType: "numeric",
Attribute: "age",
})
assert.Nil(t, err)
})
t.Run("should validate dynamic string type", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: "John Doe",
ValueType: "string",
Attribute: "name",
})
assert.Nil(t, err)
})
t.Run("should validate dynamic float type", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: 3.14,
ValueType: "float",
Attribute: "pi",
})
assert.Nil(t, err)
})
t.Run("should validate dynamic boolean type", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: true,
ValueType: "boolean",
Attribute: "is_active",
})
assert.Nil(t, err)
})
t.Run("should handle invalid numeric value", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: "invalid_numeric",
ValueType: "numeric",
Attribute: "age",
})
assert.NotNil(t, err)
e := err.(*Error)
assert.Equal(t, "INVALID_VALUE_TYPE_ERR", e.ErrType)
assert.Contains(t, e.Message, "Expected numeric value")
})
t.Run("should handle invalid string value", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: 123, // invalid type for string
ValueType: "string",
Attribute: "name",
})
assert.NotNil(t, err)
//e := err.(*Error)
//assert.Equal(t, "INVALID_VALUE_TYPE_ERR", e.ErrType)
//assert.Contains(t, e.Message, "Expected string value")
})
t.Run("should handle invalid float value", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: "invalid_float", // invalid type for float
ValueType: "float",
Attribute: "pi",
})
assert.NotNil(t, err)
e := err.(*Error)
assert.Equal(t, "INVALID_VALUE_TYPE_ERR", e.ErrType)
assert.Contains(t, e.Message, "Expected float value")
})
t.Run("should handle invalid boolean value", func(t *testing.T) {
err := g.InspectStruct(MyDynamicField{
Value: "invalid_boolean", // invalid type for boolean
ValueType: "boolean",
Attribute: "is_active",
})
assert.NotNil(t, err)
e := err.(*Error)
assert.Equal(t, "INVALID_VALUE_TYPE_ERR", e.ErrType)
assert.Contains(t, e.Message, "Expected boolean value")
})
}
type Address struct {
Street string `json:"street" binding:"required" min:"3"`
City string `json:"city" binding:"required"`
Zip string `json:"zip" format:"postal_code"`
}
type Metadata struct {
Key string `json:"key" binding:"required"`
Value string `json:"value" binding:"required"`
}
type ComplexUser struct {
Name string `json:"name" binding:"required" min:"2"`
Email string `json:"email" format:"email"`
Age int `json:"age" min:"18" max:"99"`
Role string `json:"role" enum:"admin,user,guest"`
Active bool `json:"active"`
Reason *string `json:"reason" when:"active=false;binding=required"`
Tags []string `json:"tags" min:"1"`
Addresses []Address `json:"addresses" binding:"required"`
Metadata []Metadata `json:"metadata"`
Dynamic MyDynamicField `json:"dynamic"`
}
type NestedDynamic struct {
Dynamic *MyDynamicField `json:"dynamic"`
}
func TestComplexValidation(t *testing.T) {
g := &Validate{}
t.Run("should not pass with non boolean attribute", func(t *testing.T) {
validUser := NestedDynamic{
Dynamic: &MyDynamicField{
Value: "sup",
ValueType: "boolean",
Attribute: "is_confirmed",
},
}
err := g.InspectStruct(validUser)
assert.Error(t, err)
})
}