diff --git a/date.go b/date.go index 23087fc..15d94ab 100644 --- a/date.go +++ b/date.go @@ -28,8 +28,8 @@ 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", @@ -37,31 +37,31 @@ func Date(layout string) *DateRule { } // 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 diff --git a/date_test.go b/date_test.go index 342f1ad..5e77bd5 100644 --- a/date_test.go +++ b/date_test.go @@ -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) } @@ -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)) diff --git a/each.go b/each.go index 9de3ee3..2e4ff03 100644 --- a/each.go +++ b/each.go @@ -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, } } @@ -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) @@ -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() { @@ -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() { diff --git a/error.go b/error.go index d89d628..d102bb2 100644 --- a/error.go +++ b/error.go @@ -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 } diff --git a/in.go b/in.go index 8253042..7adf282 100644 --- a/in.go +++ b/in.go @@ -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", } @@ -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 @@ -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 } diff --git a/in_test.go b/in_test.go index 24201f1..26aae6b 100644 --- a/in_test.go +++ b/in_test.go @@ -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) } diff --git a/length.go b/length.go index a20431a..eb4cf0b 100644 --- a/length.go +++ b/length.go @@ -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) @@ -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, @@ -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 @@ -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 @@ -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 } diff --git a/length_test.go b/length_test.go index 2f1bd1f..936a5f4 100644 --- a/length_test.go +++ b/length_test.go @@ -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) } diff --git a/match.go b/match.go index cc7beb3..7e5939b 100644 --- a/match.go +++ b/match.go @@ -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", } @@ -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 @@ -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 } diff --git a/match_test.go b/match_test.go index e336303..09b2887 100644 --- a/match_test.go +++ b/match_test.go @@ -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) } diff --git a/minmax.go b/minmax.go index eb7a45c..9bbf3d8 100644 --- a/minmax.go +++ b/minmax.go @@ -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), @@ -43,8 +43,8 @@ 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), @@ -52,7 +52,7 @@ func Max(max interface{}) *ThresholdRule { } // 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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/minmax_test.go b/minmax_test.go index a4bc729..f07dffc 100644 --- a/minmax_test.go +++ b/minmax_test.go @@ -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) @@ -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) } @@ -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) @@ -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) } diff --git a/multipleof.go b/multipleof.go index eb322ee..3c9d0a7 100644 --- a/multipleof.go +++ b/multipleof.go @@ -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), } @@ -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: diff --git a/multipleof_test.go b/multipleof_test.go index 32a15cf..feef00e 100644 --- a/multipleof_test.go +++ b/multipleof_test.go @@ -30,6 +30,6 @@ func Test_Multipleof_Error(t *testing.T) { r := MultipleOf(10) assert.Equal(t, "must be multiple of 10", r.message) - r.Error("some error string ...") + r = r.Error("some error string ...") assert.Equal(t, "some error string ...", r.message) } diff --git a/not_in.go b/not_in.go index 983737d..14194af 100644 --- a/not_in.go +++ b/not_in.go @@ -11,8 +11,8 @@ import ( // NotIn returns a validation rule that checks if a value is absent from 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 NotIn(values ...interface{}) *NotInRule { - return &NotInRule{ +func NotIn(values ...interface{}) NotInRule { + return NotInRule{ elements: values, message: "must not be in list", } @@ -25,7 +25,7 @@ type NotInRule struct { } // Validate checks if the given value is valid or not. -func (r *NotInRule) Validate(value interface{}) error { +func (r NotInRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil @@ -40,7 +40,7 @@ func (r *NotInRule) Validate(value interface{}) error { } // Error sets the error message for the rule. -func (r *NotInRule) Error(message string) *NotInRule { +func (r NotInRule) Error(message string) NotInRule { r.message = message return r } diff --git a/not_in_test.go b/not_in_test.go index e57662e..719a070 100644 --- a/not_in_test.go +++ b/not_in_test.go @@ -39,6 +39,6 @@ func TestNotIn(t *testing.T) { func Test_NotInRule_Error(t *testing.T) { r := NotIn(1, 2, 3) assert.Equal(t, "must not be in list", r.message) - r.Error("123") + r = r.Error("123") assert.Equal(t, "123", r.message) } diff --git a/not_nil.go b/not_nil.go index 6cfca12..0048b3d 100644 --- a/not_nil.go +++ b/not_nil.go @@ -9,14 +9,14 @@ import "errors" // NotNil is a validation rule that checks if a value is not nil. // NotNil only handles types including interface, pointer, slice, and map. // All other types are considered valid. -var NotNil = ¬NilRule{message: "is required"} +var NotNil = notNilRule{message: "is required"} type notNilRule struct { message string } // Validate checks if the given value is valid or not. -func (r *notNilRule) Validate(value interface{}) error { +func (r notNilRule) Validate(value interface{}) error { _, isNil := Indirect(value) if isNil { return errors.New(r.message) @@ -25,8 +25,8 @@ func (r *notNilRule) Validate(value interface{}) error { } // Error sets the error message for the rule. -func (r *notNilRule) Error(message string) *notNilRule { - return ¬NilRule{ +func (r notNilRule) Error(message string) notNilRule { + return notNilRule{ message: message, } } diff --git a/required.go b/required.go index ef9558e..aa13903 100644 --- a/required.go +++ b/required.go @@ -13,11 +13,11 @@ import "errors" // - string, array, slice, map: len() > 0 // - interface, pointer: not nil and the referenced value is not empty // - any other types -var Required = &requiredRule{message: "cannot be blank", skipNil: false} +var Required = requiredRule{message: "cannot be blank", skipNil: false} // NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty. // NilOrNotEmpty differs from Required in that it treats a nil pointer as valid. -var NilOrNotEmpty = &requiredRule{message: "cannot be blank", skipNil: true} +var NilOrNotEmpty = requiredRule{message: "cannot be blank", skipNil: true} type requiredRule struct { message string @@ -25,7 +25,7 @@ type requiredRule struct { } // Validate checks if the given value is valid or not. -func (v *requiredRule) Validate(value interface{}) error { +func (v requiredRule) Validate(value interface{}) error { value, isNil := Indirect(value) if v.skipNil && !isNil && IsEmpty(value) || !v.skipNil && (isNil || IsEmpty(value)) { return errors.New(v.message) @@ -34,8 +34,8 @@ func (v *requiredRule) Validate(value interface{}) error { } // Error sets the error message for the rule. -func (v *requiredRule) Error(message string) *requiredRule { - return &requiredRule{ +func (v requiredRule) Error(message string) requiredRule { + return requiredRule{ message: message, skipNil: v.skipNil, } diff --git a/string.go b/string.go index e8f2a5e..bbb2fe9 100644 --- a/string.go +++ b/string.go @@ -17,20 +17,21 @@ type StringRule struct { // NewStringRule creates a new validation rule using a function that takes a string value and returns a bool. // The rule returned will use the function to check if a given string or byte slice is valid or not. // An empty value is considered to be valid. Please use the Required rule to make sure a value is not empty. -func NewStringRule(validator stringValidator, message string) *StringRule { - return &StringRule{ +func NewStringRule(validator stringValidator, message string) StringRule { + return StringRule{ validate: validator, message: message, } } // Error sets the error message for the rule. -func (v *StringRule) Error(message string) *StringRule { - return NewStringRule(v.validate, message) +func (v StringRule) Error(message string) StringRule { + v.message = message + return v } // Validate checks if the given value is valid or not. -func (v *StringRule) Validate(value interface{}) error { +func (v StringRule) Validate(value interface{}) error { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil