From c5ea90fdc3a21aeec875ed51605cada9a9be2b26 Mon Sep 17 00:00:00 2001 From: qiangxue Date: Mon, 1 Aug 2016 11:31:07 -0400 Subject: [PATCH] removed context parameter from Rule.Validate() method --- README.md | 28 +++++++++++++--------------- example_test.go | 2 +- in.go | 2 +- in_test.go | 2 +- is/rules_test.go | 10 +++++----- length.go | 2 +- length_test.go | 2 +- match.go | 2 +- match_test.go | 2 +- not_nil.go | 2 +- not_nil_test.go | 2 +- required.go | 2 +- required_test.go | 2 +- string.go | 2 +- string_test.go | 20 ++++++++++---------- validation.go | 16 +++++++--------- validation_test.go | 22 +++++++++++----------- 17 files changed, 58 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index b6ab593..1401990 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ go get github.com/go-ozzo/ozzo-validation You may also get specified release of the package by: ``` -go get gopkg.in/go-ozzo/ozzo-validation.v1 +go get gopkg.in/go-ozzo/ozzo-validation.v2 ``` @@ -157,7 +157,7 @@ fields should be validated by passing the field names to the method. For example validate the `Name` and `Email` fields even though more fields have associated validation rules: ```go -rules := validation.StructRules{}. +err := validation.StructRules{}. // Name cannot be empty, and the length must be between 5 and 20. Add("Name", validation.Required, validation.Length(5, 20)). // Gender is optional, and should be either "Female" or "Male". @@ -165,9 +165,9 @@ rules := validation.StructRules{}. // Email cannot be empty and should be in a valid email format. Add("Email", validation.Required, is.Email). // Validate Address using its own validation rules - Add("Address") - -err := rules.Validate(customer, "Name", "Email") + Add("Address"). + // only validate Name and Email + Validate(customer, "Name", "Email") ``` ## Validating Simple Values @@ -183,7 +183,7 @@ rules := validation.Rules{ } data := "example" -err := rules.Validate(data, nil) +err := rules.Validate(data) fmt.Println(err) // Output: // must be a valid URL @@ -224,7 +224,7 @@ will report a validation error. ## Validating `sql.Valuer` If a data type implements the `sql.Valuer` interface (e.g. `sql.NullString`), the built-in validation rules will handle -it properly. In particular, when a rule is validating such data, it will cause the `Value()` method and validate +it properly. In particular, when a rule is validating such data, it will call the `Value()` method and validate the returned value instead. @@ -248,8 +248,8 @@ type Address struct { This could be useful if you are using snake case or camelCase in your JSON responses. -You may even customize the tag name by changing `validation.ErrorTag` so that you can reuse other tags you set -for struct fields (e.g. `json`) without adding a new tag for every needed field. +You may customize the tag name by changing `validation.ErrorTag`. For example, you may set `validation.ErrorTag` +to be `"json"` if you want to reuse the JSON field names as error field names. ## Required vs. Not Nil @@ -344,7 +344,7 @@ rules := validation.Rules{ } data := "2123" -err := rules.Validate(data, nil) +err := rules.Validate(data) fmt.Println(err) // Output: // must be a string with five digits @@ -353,13 +353,11 @@ fmt.Println(err) ## Creating Custom Rules Creating a custom rule is as simple as implementing the `validation.Rule` interface. The interface contains a single -method +method as shown below, which should validate the value and return the validation error, if any. ```go -// Validate validates a value and returns a value if validation fails. -// The validation can be performed with an optional context. In the case of validating -// a struct, the context would be the struct. -Validate(value interface{}, context interface{}) error +// Validate validates a value and returns an error if validation fails. +Validate(value interface{}) error ``` ## Credits diff --git a/example_test.go b/example_test.go index 5fe40a2..a6e0470 100644 --- a/example_test.go +++ b/example_test.go @@ -74,7 +74,7 @@ func Example_second() { } data := "example" - err := rules.Validate(data, nil) + err := rules.Validate(data) fmt.Println(err) // Output: // must be a valid URL diff --git a/in.go b/in.go index 5a8eda3..9673dd0 100644 --- a/in.go +++ b/in.go @@ -22,7 +22,7 @@ type inRule struct { } // Validate checks if the given value is valid or not. -func (r *inRule) Validate(value interface{}, context interface{}) error { +func (r *inRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil diff --git a/in_test.go b/in_test.go index bbeae00..ad0c87b 100644 --- a/in_test.go +++ b/in_test.go @@ -31,7 +31,7 @@ func TestIn(t *testing.T) { for _, test := range tests { r := In(test.values...) - err := r.Validate(test.value, nil) + err := r.Validate(test.value) assertError(t, test.err, err, test.tag) } } diff --git a/is/rules_test.go b/is/rules_test.go index 3ae0381..7b332bb 100644 --- a/is/rules_test.go +++ b/is/rules_test.go @@ -72,15 +72,15 @@ func TestAll(t *testing.T) { } for _, test := range tests { - err := test.rule.Validate("", nil) + err := test.rule.Validate("") assert.Nil(t, err, test.tag) - err = test.rule.Validate(test.valid, nil) + err = test.rule.Validate(test.valid) assert.Nil(t, err, test.tag) - err = test.rule.Validate(&test.valid, nil) + err = test.rule.Validate(&test.valid) assert.Nil(t, err, test.tag) - err = test.rule.Validate(test.invalid, nil) + err = test.rule.Validate(test.invalid) assertError(t, test.err, err, test.tag) - err = test.rule.Validate(&test.invalid, nil) + err = test.rule.Validate(&test.invalid) assertError(t, test.err, err, test.tag) } } diff --git a/length.go b/length.go index 0f57bc1..f8d1db7 100644 --- a/length.go +++ b/length.go @@ -35,7 +35,7 @@ type lengthRule struct { } // Validate checks if the given value is valid or not. -func (v *lengthRule) Validate(value interface{}, context interface{}) error { +func (v *lengthRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil diff --git a/length_test.go b/length_test.go index 9a545ad..e8d0899 100644 --- a/length_test.go +++ b/length_test.go @@ -35,7 +35,7 @@ func TestLength(t *testing.T) { for _, test := range tests { r := Length(test.min, test.max) - err := r.Validate(test.value, nil) + err := r.Validate(test.value) assertError(t, test.err, err, test.tag) } } diff --git a/match.go b/match.go index 570842a..02aa4fb 100644 --- a/match.go +++ b/match.go @@ -25,7 +25,7 @@ type matchRule struct { } // Validate checks if the given value is valid or not. -func (v *matchRule) Validate(value interface{}, context interface{}) error { +func (v *matchRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil { return nil diff --git a/match_test.go b/match_test.go index 9cb0dff..98075e6 100644 --- a/match_test.go +++ b/match_test.go @@ -31,7 +31,7 @@ func TestMatch(t *testing.T) { for _, test := range tests { r := Match(regexp.MustCompile(test.re)) - err := r.Validate(test.value, nil) + err := r.Validate(test.value) assertError(t, test.err, err, test.tag) } } diff --git a/not_nil.go b/not_nil.go index 1a832e6..6cfca12 100644 --- a/not_nil.go +++ b/not_nil.go @@ -16,7 +16,7 @@ type notNilRule struct { } // Validate checks if the given value is valid or not. -func (r *notNilRule) Validate(value interface{}, context interface{}) error { +func (r *notNilRule) Validate(value interface{}) error { _, isNil := Indirect(value) if isNil { return errors.New(r.message) diff --git a/not_nil_test.go b/not_nil_test.go index fff0563..4821da5 100644 --- a/not_nil_test.go +++ b/not_nil_test.go @@ -36,7 +36,7 @@ func TestNotNil(t *testing.T) { for _, test := range tests { r := NotNil - err := r.Validate(test.value, nil) + err := r.Validate(test.value) assertError(t, test.err, err, test.tag) } } diff --git a/required.go b/required.go index 8d04f6a..1ef0f9b 100644 --- a/required.go +++ b/required.go @@ -20,7 +20,7 @@ type requiredRule struct { } // Validate checks if the given value is valid or not. -func (v *requiredRule) Validate(value interface{}, context interface{}) error { +func (v *requiredRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return errors.New(v.message) diff --git a/required_test.go b/required_test.go index d913572..80638f1 100644 --- a/required_test.go +++ b/required_test.go @@ -26,7 +26,7 @@ func TestRequired(t *testing.T) { for _, test := range tests { r := Required - err := r.Validate(test.value, nil) + err := r.Validate(test.value) assertError(t, test.err, err, test.tag) } } diff --git a/string.go b/string.go index ff7f35f..01a5b80 100644 --- a/string.go +++ b/string.go @@ -29,7 +29,7 @@ func (v *stringRule) Error(message string) *stringRule { } // Validate checks if the given value is valid or not. -func (v *stringRule) Validate(value interface{}, context interface{}) error { +func (v *stringRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil diff --git a/string_test.go b/string_test.go index a20d6eb..ca49e34 100644 --- a/string_test.go +++ b/string_test.go @@ -35,44 +35,44 @@ func TestStringValidator_Validate(t *testing.T) { value := "me" - err := v.Validate(value, nil) + err := v.Validate(value) assert.Nil(t, err) - err = v.Validate(&value, nil) + err = v.Validate(&value) assert.Nil(t, err) value = "" - err = v.Validate(value, nil) + err = v.Validate(value) assert.Nil(t, err) - err = v.Validate(&value, nil) + err = v.Validate(&value) assert.Nil(t, err) nullValue := sql.NullString{"me", true} - err = v.Validate(nullValue, nil) + err = v.Validate(nullValue) assert.Nil(t, err) nullValue = sql.NullString{"", true} - err = v.Validate(nullValue, nil) + err = v.Validate(nullValue) assert.Nil(t, err) var s *string - err = v.Validate(s, nil) + err = v.Validate(s) assert.Nil(t, err) - err = v.Validate("not me", nil) + err = v.Validate("not me") if assert.NotNil(t, err) { assert.Equal(t, "wrong", err.Error()) } - err = v.Validate(100, nil) + err = v.Validate(100) if assert.NotNil(t, err) { assert.NotEqual(t, "wrong", err.Error()) } v2 := v.Error("Wrong!") - err = v2.Validate("not me", nil) + err = v2.Validate("not me") if assert.NotNil(t, err) { assert.Equal(t, "Wrong!", err.Error()) } diff --git a/validation.go b/validation.go index bb39093..e6b98e2 100644 --- a/validation.go +++ b/validation.go @@ -22,9 +22,7 @@ type ( // Rule represents a validation rule. Rule interface { // Validate validates a value and returns a value if validation fails. - // The validation can be performed with an optional context. In the case of validating - // a struct, the context would be the struct. - Validate(value interface{}, context interface{}) error + Validate(value interface{}) error } // Rules represents a list of validation rules. @@ -104,12 +102,12 @@ func Validate(value interface{}) error { } // Validate validates the given value using the validation rules in Rules. -func (rules Rules) Validate(value interface{}, context interface{}) error { +func (rules Rules) Validate(value interface{}) error { for _, rule := range rules { if _, ok := rule.(*skipRule); ok { return nil } - if err := rule.Validate(value, context); err != nil { + if err := rule.Validate(value); err != nil { return err } } @@ -164,7 +162,7 @@ func (r StructRules) Validate(object interface{}, attrs ...string) error { } } - if err := fieldRules.validate(value, object); err != nil { + if err := fieldRules.validate(value); err != nil { ft, _ := value.Type().FieldByName(fieldRules.Field) if tag := ft.Tag.Get(ErrorTag); tag != "" { errs[tag] = err @@ -185,7 +183,7 @@ func NewFieldRules(name string, rules ...Rule) FieldRules { return FieldRules{name, rules} } -func (rules FieldRules) validate(object reflect.Value, context interface{}) error { +func (rules FieldRules) validate(object reflect.Value) error { fname := rules.Field @@ -200,7 +198,7 @@ func (rules FieldRules) validate(object reflect.Value, context interface{}) erro field := object.FieldByName(fname) value := field.Interface() - if err := rules.Rules.Validate(value, context); err != nil { + if err := rules.Rules.Validate(value); err != nil { return err } @@ -214,6 +212,6 @@ func (rules FieldRules) validate(object reflect.Value, context interface{}) erro type skipRule struct{} -func (r *skipRule) Validate(interface{}, interface{}) error { +func (r *skipRule) Validate(interface{}) error { return nil } diff --git a/validation_test.go b/validation_test.go index ece1a0d..a1aae23 100644 --- a/validation_test.go +++ b/validation_test.go @@ -44,33 +44,33 @@ func TestRules_shouldSkip(t *testing.T) { func TestRules_Validate(t *testing.T) { rules := Rules{} - assert.Nil(t, rules.Validate(nil, nil)) - assert.Nil(t, rules.Validate("abc", nil)) + assert.Nil(t, rules.Validate(nil)) + assert.Nil(t, rules.Validate("abc")) rules = Rules{ &validateAbc{}, &validateXyz{}, } - err := rules.Validate("123", nil) + err := rules.Validate("123") if assert.NotNil(t, err) { assert.Equal(t, "error abc", err.Error()) } - err = rules.Validate("abc", nil) + err = rules.Validate("abc") if assert.NotNil(t, err) { assert.Equal(t, "error xyz", err.Error()) } - assert.Nil(t, rules.Validate("abcxyz", nil)) + assert.Nil(t, rules.Validate("abcxyz")) rules = Rules{ &validateAbc{}, Skip, &validateXyz{}, } - err = rules.Validate("123", nil) + err = rules.Validate("123") if assert.NotNil(t, err) { assert.Equal(t, "error abc", err.Error()) } - assert.Nil(t, rules.Validate("abc", nil)) + assert.Nil(t, rules.Validate("abc")) } func TestStructRules_Validate(t *testing.T) { @@ -134,7 +134,7 @@ func TestFieldRules_validate(t *testing.T) { {"t15", Model2{Model3: Model3{A: "abc"}}, NewFieldRules("Model3"), ""}, } for _, test := range tests { - err := test.rules.validate(reflect.ValueOf(test.model), test.model) + err := test.rules.validate(reflect.ValueOf(test.model)) assertError(t, test.err, err, test.tag) } } @@ -164,7 +164,7 @@ func TestValidate(t *testing.T) { } func Test_skipRule_Validate(t *testing.T) { - assert.Nil(t, Skip.Validate(100, nil)) + assert.Nil(t, Skip.Validate(100)) } func assertError(t *testing.T, expected string, err error, tag string) { @@ -177,7 +177,7 @@ func assertError(t *testing.T, expected string, err error, tag string) { type validateAbc struct{} -func (v *validateAbc) Validate(obj interface{}, context interface{}) error { +func (v *validateAbc) Validate(obj interface{}) error { if !strings.Contains(obj.(string), "abc") { return errors.New("error abc") } @@ -186,7 +186,7 @@ func (v *validateAbc) Validate(obj interface{}, context interface{}) error { type validateXyz struct{} -func (v *validateXyz) Validate(obj interface{}, context interface{}) error { +func (v *validateXyz) Validate(obj interface{}) error { if !strings.Contains(obj.(string), "xyz") { return errors.New("error xyz") }