Skip to content

Commit

Permalink
Fixes #63: returning rules as structs instead of pointers (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue authored Dec 4, 2019
1 parent 38fdfa1 commit d621af5
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 73 deletions.
14 changes: 7 additions & 7 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,40 @@ type DateRule struct {
// the specified date range.
//
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Date(layout string) *DateRule {
return &DateRule{
func Date(layout string) DateRule {
return DateRule{
layout: layout,
message: "must be a valid date",
rangeMessage: "the data is out of range",
}
}

// Error sets the error message that is used when the value being validated is not a valid date.
func (r *DateRule) Error(message string) *DateRule {
func (r DateRule) Error(message string) DateRule {
r.message = message
return r
}

// RangeError sets the error message that is used when the value being validated is out of the specified Min/Max date range.
func (r *DateRule) RangeError(message string) *DateRule {
func (r DateRule) RangeError(message string) DateRule {
r.rangeMessage = message
return r
}

// Min sets the minimum date range. A zero value means skipping the minimum range validation.
func (r *DateRule) Min(min time.Time) *DateRule {
func (r DateRule) Min(min time.Time) DateRule {
r.min = min
return r
}

// Max sets the maximum date range. A zero value means skipping the maximum range validation.
func (r *DateRule) Max(max time.Time) *DateRule {
func (r DateRule) Max(max time.Time) DateRule {
r.max = max
return r
}

// Validate checks if the given value is a valid date.
func (r *DateRule) Validate(value interface{}) error {
func (r DateRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand Down
8 changes: 4 additions & 4 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestDateRule_Error(t *testing.T) {
r := Date(time.ANSIC)
assert.Equal(t, "must be a valid date", r.message)
assert.Equal(t, "the data is out of range", r.rangeMessage)
r.Error("123")
r.RangeError("456")
r = r.Error("123")
r = r.RangeError("456")
assert.Equal(t, "123", r.message)
assert.Equal(t, "456", r.rangeMessage)
}
Expand All @@ -50,10 +50,10 @@ func TestDateRule_MinMax(t *testing.T) {
r := Date(time.ANSIC)
assert.True(t, r.min.IsZero())
assert.True(t, r.max.IsZero())
r.Min(time.Now())
r = r.Min(time.Now())
assert.False(t, r.min.IsZero())
assert.True(t, r.max.IsZero())
r.Max(time.Now())
r = r.Max(time.Now())
assert.False(t, r.max.IsZero())

r2 := Date("2006-01-02").Min(time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)).Max(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC))
Expand Down
10 changes: 5 additions & 5 deletions each.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
// Each returns a validation rule that loops through an iterable (map, slice or array)
// and validates each value inside with the provided rules.
// An empty iterable is considered valid. Use the Required rule to make sure the iterable is not empty.
func Each(rules ...Rule) *EachRule {
return &EachRule{
func Each(rules ...Rule) EachRule {
return EachRule{
rules: rules,
}
}
Expand All @@ -25,7 +25,7 @@ type EachRule struct {
}

// Validate loops through the given iterable and calls the Ozzo Validate() method for each value.
func (r *EachRule) Validate(value interface{}) error {
func (r EachRule) Validate(value interface{}) error {
errs := Errors{}

v := reflect.ValueOf(value)
Expand Down Expand Up @@ -54,7 +54,7 @@ func (r *EachRule) Validate(value interface{}) error {
return nil
}

func (r *EachRule) getInterface(value reflect.Value) interface{} {
func (r EachRule) getInterface(value reflect.Value) interface{} {
switch value.Kind() {
case reflect.Ptr, reflect.Interface:
if value.IsNil() {
Expand All @@ -66,7 +66,7 @@ func (r *EachRule) getInterface(value reflect.Value) interface{} {
}
}

func (r *EachRule) getString(value reflect.Value) string {
func (r EachRule) getString(value reflect.Value) string {
switch value.Kind() {
case reflect.Ptr, reflect.Interface:
if value.IsNil() {
Expand Down
4 changes: 2 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type (

// NewInternalError wraps a given error into an InternalError.
func NewInternalError(err error) InternalError {
return &internalError{error: err}
return internalError{error: err}
}

// InternalError returns the actual error that it wraps around.
func (e *internalError) InternalError() error {
func (e internalError) InternalError() error {
return e.error
}

Expand Down
8 changes: 4 additions & 4 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import "errors"
// In returns a validation rule that checks if a value can be found in the given list of values.
// Note that the value being checked and the possible range of values must be of the same type.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func In(values ...interface{}) *InRule {
return &InRule{
func In(values ...interface{}) InRule {
return InRule{
elements: values,
message: "must be a valid value",
}
Expand All @@ -23,7 +23,7 @@ type InRule struct {
}

// Validate checks if the given value is valid or not.
func (r *InRule) Validate(value interface{}) error {
func (r InRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand All @@ -38,7 +38,7 @@ func (r *InRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (r *InRule) Error(message string) *InRule {
func (r InRule) Error(message string) InRule {
r.message = message
return r
}
2 changes: 1 addition & 1 deletion in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func TestIn(t *testing.T) {
func Test_InRule_Error(t *testing.T) {
r := In(1, 2, 3)
assert.Equal(t, "must be a valid value", r.message)
r.Error("123")
r = r.Error("123")
assert.Equal(t, "123", r.message)
}
10 changes: 5 additions & 5 deletions length.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// If max is 0, it means there is no upper bound for the length.
// This rule should only be used for validating strings, slices, maps, and arrays.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Length(min, max int) *LengthRule {
func Length(min, max int) LengthRule {
message := "the value must be empty"
if min == 0 && max > 0 {
message = fmt.Sprintf("the length must be no more than %v", max)
Expand All @@ -27,7 +27,7 @@ func Length(min, max int) *LengthRule {
message = fmt.Sprintf("the length must be between %v and %v", min, max)
}
}
return &LengthRule{
return LengthRule{
min: min,
max: max,
message: message,
Expand All @@ -39,7 +39,7 @@ func Length(min, max int) *LengthRule {
// This rule should only be used for validating strings, slices, maps, and arrays.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
// If the value being validated is not a string, the rule works the same as Length.
func RuneLength(min, max int) *LengthRule {
func RuneLength(min, max int) LengthRule {
r := Length(min, max)
r.rune = true
return r
Expand All @@ -53,7 +53,7 @@ type LengthRule struct {
}

// Validate checks if the given value is valid or not.
func (v *LengthRule) Validate(value interface{}) error {
func (v LengthRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand All @@ -76,7 +76,7 @@ func (v *LengthRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (v *LengthRule) Error(message string) *LengthRule {
func (v LengthRule) Error(message string) LengthRule {
v.message = message
return v
}
2 changes: 1 addition & 1 deletion length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ func Test_LengthRule_Error(t *testing.T) {
r = Length(10, 0)
assert.Equal(t, "the length must be no less than 10", r.message)

r.Error("123")
r = r.Error("123")
assert.Equal(t, "123", r.message)
}
8 changes: 4 additions & 4 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
// Match returns a validation rule that checks if a value matches the specified regular expression.
// This rule should only be used for validating strings and byte slices, or a validation error will be reported.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Match(re *regexp.Regexp) *MatchRule {
return &MatchRule{
func Match(re *regexp.Regexp) MatchRule {
return MatchRule{
re: re,
message: "must be in a valid format",
}
Expand All @@ -26,7 +26,7 @@ type MatchRule struct {
}

// Validate checks if the given value is valid or not.
func (v *MatchRule) Validate(value interface{}) error {
func (v MatchRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil {
return nil
Expand All @@ -42,7 +42,7 @@ func (v *MatchRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (v *MatchRule) Error(message string) *MatchRule {
func (v MatchRule) Error(message string) MatchRule {
v.message = message
return v
}
2 changes: 1 addition & 1 deletion match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func TestMatch(t *testing.T) {
func Test_MatchRule_Error(t *testing.T) {
r := Match(regexp.MustCompile("[a-z]+"))
assert.Equal(t, "must be in a valid format", r.message)
r.Error("123")
r = r.Error("123")
assert.Equal(t, "123", r.message)
}
22 changes: 11 additions & 11 deletions minmax.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const (
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Min(min interface{}) *ThresholdRule {
return &ThresholdRule{
func Min(min interface{}) ThresholdRule {
return ThresholdRule{
threshold: min,
operator: greaterEqualThan,
message: fmt.Sprintf("must be no less than %v", min),
Expand All @@ -43,16 +43,16 @@ func Min(min interface{}) *ThresholdRule {
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Max(max interface{}) *ThresholdRule {
return &ThresholdRule{
func Max(max interface{}) ThresholdRule {
return ThresholdRule{
threshold: max,
operator: lessEqualThan,
message: fmt.Sprintf("must be no greater than %v", max),
}
}

// Exclusive sets the comparison to exclude the boundary value.
func (r *ThresholdRule) Exclusive() *ThresholdRule {
func (r ThresholdRule) Exclusive() ThresholdRule {
if r.operator == greaterEqualThan {
r.operator = greaterThan
r.message = fmt.Sprintf("must be greater than %v", r.threshold)
Expand All @@ -64,7 +64,7 @@ func (r *ThresholdRule) Exclusive() *ThresholdRule {
}

// Validate checks if the given value is valid or not.
func (r *ThresholdRule) Validate(value interface{}) error {
func (r ThresholdRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand Down Expand Up @@ -120,12 +120,12 @@ func (r *ThresholdRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (r *ThresholdRule) Error(message string) *ThresholdRule {
func (r ThresholdRule) Error(message string) ThresholdRule {
r.message = message
return r
}

func (r *ThresholdRule) compareInt(threshold, value int64) bool {
func (r ThresholdRule) compareInt(threshold, value int64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -138,7 +138,7 @@ func (r *ThresholdRule) compareInt(threshold, value int64) bool {
}
}

func (r *ThresholdRule) compareUint(threshold, value uint64) bool {
func (r ThresholdRule) compareUint(threshold, value uint64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -151,7 +151,7 @@ func (r *ThresholdRule) compareUint(threshold, value uint64) bool {
}
}

func (r *ThresholdRule) compareFloat(threshold, value float64) bool {
func (r ThresholdRule) compareFloat(threshold, value float64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -164,7 +164,7 @@ func (r *ThresholdRule) compareFloat(threshold, value float64) bool {
}
}

func (r *ThresholdRule) compareTime(threshold, value time.Time) bool {
func (r ThresholdRule) compareTime(threshold, value time.Time) bool {
switch r.operator {
case greaterThan:
return value.After(threshold)
Expand Down
8 changes: 4 additions & 4 deletions minmax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestMin(t *testing.T) {
for _, test := range tests {
r := Min(test.threshold)
if test.exclusive {
r.Exclusive()
r = r.Exclusive()
}
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
Expand All @@ -71,7 +71,7 @@ func TestMinError(t *testing.T) {
r := Min(10)
assert.Equal(t, "must be no less than 10", r.message)

r.Error("123")
r = r.Error("123")
assert.Equal(t, "123", r.message)
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func TestMax(t *testing.T) {
for _, test := range tests {
r := Max(test.threshold)
if test.exclusive {
r.Exclusive()
r = r.Exclusive()
}
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
Expand All @@ -133,6 +133,6 @@ func TestMaxError(t *testing.T) {
r := Max(10)
assert.Equal(t, "must be no greater than 10", r.message)

r.Error("123")
r = r.Error("123")
assert.Equal(t, "123", r.message)
}
8 changes: 4 additions & 4 deletions multipleof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// MultipleOf returns a validation rule that checks if a value is a multiple of the "base" value.
// Note that "base" should be of integer type.
func MultipleOf(base interface{}) *MultipleOfRule {
return &MultipleOfRule{
func MultipleOf(base interface{}) MultipleOfRule {
return MultipleOfRule{
base,
fmt.Sprintf("must be multiple of %v", base),
}
Expand All @@ -22,13 +22,13 @@ type MultipleOfRule struct {
}

// Error sets the error message for the rule.
func (r *MultipleOfRule) Error(message string) *MultipleOfRule {
func (r MultipleOfRule) Error(message string) MultipleOfRule {
r.message = message
return r
}

// Validate checks if the value is a multiple of the "base" value.
func (r *MultipleOfRule) Validate(value interface{}) error {
func (r MultipleOfRule) Validate(value interface{}) error {
rv := reflect.ValueOf(r.base)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand Down
Loading

0 comments on commit d621af5

Please sign in to comment.