issue-#18
add “structonly” tag for struct only and not field validation
Dean Karn
9 years ago
148 | 148 |
accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
|
149 | 149 |
colors to be accepted. This can also be combined with 'and' for example
|
150 | 150 |
( Usage: omitempty,rgb|rgba)
|
|
151 |
|
|
152 |
structonly
|
|
153 |
When a field that is a nest struct in encountered and contains this flag
|
|
154 |
any validation on the nested struct such as "required" will be run, but
|
|
155 |
none of the nested struct fields will be validated. This is usefull if
|
|
156 |
inside of you program you know the struct will be valid, but need to
|
|
157 |
verify it has been assigned.
|
151 | 158 |
|
152 | 159 |
omitempty
|
153 | 160 |
Allows conitional validation, for example if a field is not set with
|
21 | 21 |
orSeparator = "|"
|
22 | 22 |
noValidationTag = "-"
|
23 | 23 |
tagKeySeparator = "="
|
|
24 |
structOnlyTag = "structonly"
|
24 | 25 |
omitempty = "omitempty"
|
25 | 26 |
validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
|
26 | 27 |
validationStructErrMsg = "Struct:%s\n"
|
|
215 | 216 |
|
216 | 217 |
} else {
|
217 | 218 |
|
|
219 |
if strings.Contains(tag, structOnlyTag) {
|
|
220 |
continue
|
|
221 |
}
|
|
222 |
|
218 | 223 |
if structErrors := v.validateStructRecursive(top, valueField.Interface()); structErrors != nil {
|
219 | 224 |
validationErrors.StructErrors[typeField.Name] = structErrors
|
220 | 225 |
// free up memory map no longer needed
|
134 | 134 |
func isEqualFunc(val interface{}, field interface{}, param string) bool {
|
135 | 135 |
|
136 | 136 |
return val.(string) == field.(string)
|
|
137 |
}
|
|
138 |
|
|
139 |
func (ms *MySuite) TestStructOnlyValidation(c *C) {
|
|
140 |
|
|
141 |
type Inner struct {
|
|
142 |
Test string `validate:"len=5"`
|
|
143 |
}
|
|
144 |
|
|
145 |
type Outer struct {
|
|
146 |
InnerStruct *Inner `validate:"required,structonly"`
|
|
147 |
}
|
|
148 |
|
|
149 |
outer := &Outer{
|
|
150 |
InnerStruct: nil,
|
|
151 |
}
|
|
152 |
|
|
153 |
errs := myValidator.ValidateStruct(outer).Flatten()
|
|
154 |
c.Assert(errs, NotNil)
|
|
155 |
|
|
156 |
inner := &Inner{
|
|
157 |
Test: "1234",
|
|
158 |
}
|
|
159 |
|
|
160 |
outer = &Outer{
|
|
161 |
InnerStruct: inner,
|
|
162 |
}
|
|
163 |
|
|
164 |
errs = myValidator.ValidateStruct(outer).Flatten()
|
|
165 |
c.Assert(errs, IsNil)
|
137 | 166 |
}
|
138 | 167 |
|
139 | 168 |
func (ms *MySuite) TestGtField(c *C) {
|