Skip to content

Commit

Permalink
check min max
Browse files Browse the repository at this point in the history
  • Loading branch information
cadyrov committed Jan 30, 2020
1 parent d5689b1 commit 2c1da2e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
16 changes: 8 additions & 8 deletions minmax.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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{}) ExternalError {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand All @@ -70,7 +70,7 @@ func (r *ThresholdRule) Validate(value interface{}) error {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := ToInt(value)
if err != nil {
return err
return NewExternalError(err, 1000)
}
if r.compareInt(rv.Int(), v) {
return nil
Expand All @@ -79,7 +79,7 @@ func (r *ThresholdRule) Validate(value interface{}) error {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v, err := ToUint(value)
if err != nil {
return err
return NewExternalError(err, 1000)
}
if r.compareUint(rv.Uint(), v) {
return nil
Expand All @@ -88,7 +88,7 @@ func (r *ThresholdRule) Validate(value interface{}) error {
case reflect.Float32, reflect.Float64:
v, err := ToFloat(value)
if err != nil {
return err
return NewExternalError(err, 1000)
}
if r.compareFloat(rv.Float(), v) {
return nil
Expand All @@ -97,21 +97,21 @@ func (r *ThresholdRule) Validate(value interface{}) error {
case reflect.Struct:
t, ok := r.threshold.(time.Time)
if !ok {
return fmt.Errorf("type not supported: %v", rv.Type())
return NewExternalError(fmt.Errorf("type not supported: %v", rv.Type()), 1000)
}
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value))
return NewExternalError(fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value)), 1000)
}
if v.IsZero() || r.compareTime(t, v) {
return nil
}

default:
return fmt.Errorf("type not supported: %v", rv.Type())
return NewExternalError(fmt.Errorf("type not supported: %v", rv.Type()), 1000)
}

return errors.New(r.message)
return NewExternalError(errors.New(r.message), 1000)
}

// Error sets the error message for the rule.
Expand Down
28 changes: 14 additions & 14 deletions minmax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ func TestMin(t *testing.T) {
// int cases
{"t1.1", 1, false, 1, ""},
{"t1.2", 1, false, 2, ""},
{"t1.3", 1, false, -1, "must be no less than 1"},
{"t1.3", 1, false, -1, "must be no less than 1, ErrCode: 1000"},
{"t1.4", 1, false, 0, ""},
{"t1.5", 1, true, 1, "must be greater than 1"},
{"t1.6", 1, false, "1", "cannot convert string to int64"},
{"t1.7", "1", false, 1, "type not supported: string"},
{"t1.5", 1, true, 1, "must be greater than 1, ErrCode: 1000"},
{"t1.6", 1, false, "1", "cannot convert string to int64, ErrCode: 1000"},
{"t1.7", "1", false, 1, "type not supported: string, ErrCode: 1000"},
// uint cases
{"t2.1", uint(2), false, uint(2), ""},
{"t2.2", uint(2), false, uint(3), ""},
{"t2.3", uint(2), false, uint(1), "must be no less than 2"},
{"t2.3", uint(2), false, uint(1), "must be no less than 2, ErrCode: 1000"},
{"t2.4", uint(2), false, uint(0), ""},
{"t2.5", uint(2), true, uint(2), "must be greater than 2"},
{"t2.6", uint(2), false, "1", "cannot convert string to uint64"},
{"t2.5", uint(2), true, uint(2), "must be greater than 2, ErrCode: 1000"},
{"t2.6", uint(2), false, "1", "cannot convert string to uint64, ErrCode: 1000"},
// float cases
{"t3.1", float64(2), false, float64(2), ""},
{"t3.2", float64(2), false, float64(3), ""},
{"t3.3", float64(2), false, float64(1), "must be no less than 2"},
{"t3.3", float64(2), false, float64(1), "must be no less than 2, ErrCode: 1000"},
{"t3.4", float64(2), false, float64(0), ""},
{"t3.5", float64(2), true, float64(2), "must be greater than 2"},
{"t3.6", float64(2), false, "1", "cannot convert string to float64"},
{"t3.5", float64(2), true, float64(2), "must be greater than 2, ErrCode: 1000"},
{"t3.6", float64(2), false, "1", "cannot convert string to float64, ErrCode: 1000"},
// Time cases
{"t4.1", date20000601, false, date20000601, ""},
{"t4.2", date20000601, false, date20001201, ""},
{"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC, ErrCode: 1000"},
{"t4.4", date20000601, false, date0, ""},
{"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.6", date20000601, true, 1, "cannot convert int to time.Time"},
{"t4.7", struct{}{}, false, 1, "type not supported: struct {}"},
{"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC, ErrCode: 1000"},
{"t4.6", date20000601, true, 1, "cannot convert int to time.Time, ErrCode: 1000"},
{"t4.7", struct{}{}, false, 1, "type not supported: struct {}, ErrCode: 1000"},
{"t4.8", date0, false, date20000601, ""},
}

Expand Down

0 comments on commit 2c1da2e

Please sign in to comment.