Codebase list golang-github-go-playground-validator-v10 / 0650f73
Merge pull request #166 from flowonyx/v7-development Added checking for nil receiver on Validator methods Dean Karn 8 years ago
3 changed file(s) with 49 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
224224 There will always be a development branch for each version i.e. `v1-development`. In order to contribute,
225225 please make your pull requests against those branches.
226226
227 If the changes being proposed or requested are breaking changes, please create an issue, for discussion
228 or create a pull request against the highest development branch for example this package has a
229 v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet.
227 If the changes being proposed or requested are breaking changes, please create an issue, for discussion
228 or create a pull request against the highest development branch for example this package has a
229 v1 and v1-development branch however, there will also be a v2-development branch even though v2 doesn't exist yet.
230230
231231 I strongly encourage everyone whom creates a custom validation function to contribute them and
232232 help make this package even better.
145145 // NOTE: if the key already exists, the previous validation function will be replaced.
146146 // NOTE: this method is not thread-safe
147147 func (v *Validate) RegisterValidation(key string, f Func) error {
148
148 if v == nil {
149 panic("Validate.RegisterValidation called with nil receiver")
150 }
149151 if len(key) == 0 {
150152 return errors.New("Function Key cannot be empty")
151153 }
161163
162164 // RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
163165 func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
166 if v == nil {
167 panic("Validate.RegisterCustomTypeFunc called with nil receiver")
168 }
164169
165170 if v.config.CustomTypeFuncs == nil {
166171 v.config.CustomTypeFuncs = map[reflect.Type]CustomTypeFunc{}
177182 // NOTE: it returns ValidationErrors instead of a single FieldError because this can also
178183 // validate Array, Slice and maps fields which may contain more than one error
179184 func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
180
185 if v == nil {
186 panic("Validate.Field called with nil receiver")
187 }
181188 errs := errsPool.Get().(ValidationErrors)
182189 fieldVal := reflect.ValueOf(field)
183190
195202 // NOTE: it returns ValidationErrors instead of a single FieldError because this can also
196203 // validate Array, Slice and maps fields which may contain more than one error
197204 func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string) ValidationErrors {
205 if v == nil {
206 panic("Validate.FieldWithValue called with nil receiver")
207 }
198208
199209 errs := errsPool.Get().(ValidationErrors)
200210 topVal := reflect.ValueOf(val)
215225 // NOTE: This is normally not needed, however in some specific cases such as: tied to a
216226 // legacy data structure, it will be useful
217227 func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
218
228 if v == nil {
229 panic("Validate.StructPartial called with nil receiver")
230 }
219231 sv, _ := v.extractType(reflect.ValueOf(current))
220232 name := sv.Type().Name()
221233 m := map[string]*struct{}{}
273285 // NOTE: This is normally not needed, however in some specific cases such as: tied to a
274286 // legacy data structure, it will be useful
275287 func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
276
288 if v == nil {
289 panic("Validate.StructExcept called with nil receiver")
290 }
277291 sv, _ := v.extractType(reflect.ValueOf(current))
278292 name := sv.Type().Name()
279293 m := map[string]*struct{}{}
296310
297311 // Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
298312 func (v *Validate) Struct(current interface{}) ValidationErrors {
299
313 if v == nil {
314 panic("Validate.Struct called with nil receiver")
315 }
300316 errs := errsPool.Get().(ValidationErrors)
301317 sv := reflect.ValueOf(current)
302318
207207 OtherTest string `validate:"required"`
208208 } `validate:"required,dive"`
209209 }
210 }
211
212 func TestNilValidator(t *testing.T) {
213
214 type TestStruct struct {
215 Test string `validate:"required"`
216 }
217
218 ts := TestStruct{}
219
220 var val *Validate
221
222 fn := func(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
223
224 return current.String() == field.String()
225 }
226
227 PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, "Validate.RegisterCustomTypeFunc called with nil receiver")
228 PanicMatches(t, func() { val.RegisterValidation("something", fn) }, "Validate.RegisterValidation called with nil receiver")
229 PanicMatches(t, func() { val.Field(ts.Test, "required") }, "Validate.Field called with nil receiver")
230 PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, "Validate.FieldWithValue called with nil receiver")
231 PanicMatches(t, func() { val.Struct(ts) }, "Validate.Struct called with nil receiver")
232 PanicMatches(t, func() { val.StructExcept(ts, "Test") }, "Validate.StructExcept called with nil receiver")
233 PanicMatches(t, func() { val.StructPartial(ts, "Test") }, "Validate.StructPartial called with nil receiver")
234
210235 }
211236
212237 func TestStructPartial(t *testing.T) {