Codebase list golang-github-go-playground-validator-v10 / 622107d
issue-#18 add “structonly” tag for struct only and not field validation Dean Karn 9 years ago
3 changed file(s) with 41 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
148148 accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
149149 colors to be accepted. This can also be combined with 'and' for example
150150 ( 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.
151158
152159 omitempty
153160 Allows conitional validation, for example if a field is not set with
2121 orSeparator = "|"
2222 noValidationTag = "-"
2323 tagKeySeparator = "="
24 structOnlyTag = "structonly"
2425 omitempty = "omitempty"
2526 validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
2627 validationStructErrMsg = "Struct:%s\n"
215216
216217 } else {
217218
219 if strings.Contains(tag, structOnlyTag) {
220 continue
221 }
222
218223 if structErrors := v.validateStructRecursive(top, valueField.Interface()); structErrors != nil {
219224 validationErrors.StructErrors[typeField.Name] = structErrors
220225 // free up memory map no longer needed
134134 func isEqualFunc(val interface{}, field interface{}, param string) bool {
135135
136136 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)
137166 }
138167
139168 func (ms *MySuite) TestGtField(c *C) {