Skip to content

Commit

Permalink
removed context parameter from Rule.Validate() method
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Aug 1, 2016
1 parent f2591eb commit c5ea90f
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 62 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down Expand Up @@ -157,17 +157,17 @@ 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".
Add("Gender", validation.In("Female", "Male")).
// 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
Expand All @@ -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
Expand Down Expand Up @@ -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.


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
10 changes: 5 additions & 5 deletions is/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion length.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion not_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion not_nil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion required.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
16 changes: 7 additions & 9 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
}

Expand All @@ -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
}
Loading

0 comments on commit c5ea90f

Please sign in to comment.