Skip to content

Commit 0650f73

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #166 from flowonyx/v7-development
Added checking for nil receiver on Validator methods
2 parents ca7face + 715aa55 commit 0650f73

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ How to Contribute
225225
There will always be a development branch for each version i.e. `v1-development`. In order to contribute,
226226
please make your pull requests against those branches.
227227

228-
If the changes being proposed or requested are breaking changes, please create an issue, for discussion
229-
or create a pull request against the highest development branch for example this package has a
230-
v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet.
228+
If the changes being proposed or requested are breaking changes, please create an issue, for discussion
229+
or create a pull request against the highest development branch for example this package has a
230+
v1 and v1-development branch however, there will also be a v2-development branch even though v2 doesn't exist yet.
231231

232232
I strongly encourage everyone whom creates a custom validation function to contribute them and
233233
help make this package even better.

validator.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ func New(config Config) *Validate {
146146
// NOTE: if the key already exists, the previous validation function will be replaced.
147147
// NOTE: this method is not thread-safe
148148
func (v *Validate) RegisterValidation(key string, f Func) error {
149-
149+
if v == nil {
150+
panic("Validate.RegisterValidation called with nil receiver")
151+
}
150152
if len(key) == 0 {
151153
return errors.New("Function Key cannot be empty")
152154
}
@@ -162,6 +164,9 @@ func (v *Validate) RegisterValidation(key string, f Func) error {
162164

163165
// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
164166
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
167+
if v == nil {
168+
panic("Validate.RegisterCustomTypeFunc called with nil receiver")
169+
}
165170

166171
if v.config.CustomTypeFuncs == nil {
167172
v.config.CustomTypeFuncs = map[reflect.Type]CustomTypeFunc{}
@@ -178,7 +183,9 @@ func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{
178183
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
179184
// validate Array, Slice and maps fields which may contain more than one error
180185
func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
181-
186+
if v == nil {
187+
panic("Validate.Field called with nil receiver")
188+
}
182189
errs := errsPool.Get().(ValidationErrors)
183190
fieldVal := reflect.ValueOf(field)
184191

@@ -196,6 +203,9 @@ func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
196203
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
197204
// validate Array, Slice and maps fields which may contain more than one error
198205
func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string) ValidationErrors {
206+
if v == nil {
207+
panic("Validate.FieldWithValue called with nil receiver")
208+
}
199209

200210
errs := errsPool.Get().(ValidationErrors)
201211
topVal := reflect.ValueOf(val)
@@ -216,7 +226,9 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
216226
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
217227
// legacy data structure, it will be useful
218228
func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
219-
229+
if v == nil {
230+
panic("Validate.StructPartial called with nil receiver")
231+
}
220232
sv, _ := v.extractType(reflect.ValueOf(current))
221233
name := sv.Type().Name()
222234
m := map[string]*struct{}{}
@@ -274,7 +286,9 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) Validati
274286
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
275287
// legacy data structure, it will be useful
276288
func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
277-
289+
if v == nil {
290+
panic("Validate.StructExcept called with nil receiver")
291+
}
278292
sv, _ := v.extractType(reflect.ValueOf(current))
279293
name := sv.Type().Name()
280294
m := map[string]*struct{}{}
@@ -297,7 +311,9 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) Validatio
297311

298312
// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
299313
func (v *Validate) Struct(current interface{}) ValidationErrors {
300-
314+
if v == nil {
315+
panic("Validate.Struct called with nil receiver")
316+
}
301317
errs := errsPool.Get().(ValidationErrors)
302318
sv := reflect.ValueOf(current)
303319

validator_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,31 @@ type TestPartial struct {
210210
}
211211
}
212212

213+
func TestNilValidator(t *testing.T) {
214+
215+
type TestStruct struct {
216+
Test string `validate:"required"`
217+
}
218+
219+
ts := TestStruct{}
220+
221+
var val *Validate
222+
223+
fn := func(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
224+
225+
return current.String() == field.String()
226+
}
227+
228+
PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, "Validate.RegisterCustomTypeFunc called with nil receiver")
229+
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, "Validate.RegisterValidation called with nil receiver")
230+
PanicMatches(t, func() { val.Field(ts.Test, "required") }, "Validate.Field called with nil receiver")
231+
PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, "Validate.FieldWithValue called with nil receiver")
232+
PanicMatches(t, func() { val.Struct(ts) }, "Validate.Struct called with nil receiver")
233+
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, "Validate.StructExcept called with nil receiver")
234+
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, "Validate.StructPartial called with nil receiver")
235+
236+
}
237+
213238
func TestStructPartial(t *testing.T) {
214239

215240
p1 := []string{

0 commit comments

Comments
 (0)