-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package rules | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
var floatType = reflect.TypeFor[float64]() | ||
|
||
type Min struct{} | ||
|
||
func (self Min) Select(parent reflect.Value, value reflect.Value) bool { | ||
return value.CanFloat() || value.CanConvert(floatType) || value.Kind() == reflect.String | ||
} | ||
|
||
func (self Min) Validate(config string, parent reflect.Value, value reflect.Value) []error { | ||
errs := []error{} | ||
|
||
if config == "" { | ||
errs = append(errs, errors.New("empty config")) | ||
return errs | ||
} | ||
|
||
if value.Kind() == reflect.String { | ||
return self.validateString(config, value) | ||
} | ||
|
||
return self.validateNumber(config, value) | ||
} | ||
|
||
func (self Min) validateNumber(config string, value reflect.Value) []error { | ||
errs := []error{} | ||
min, err := strconv.ParseFloat(config, 64) | ||
|
||
if err != nil { | ||
errs = append(errs, err) | ||
return errs | ||
} | ||
|
||
if value.Kind() != reflect.Float64 && value.CanConvert(floatType) { | ||
value = value.Convert(floatType) | ||
} | ||
|
||
if min > value.Float() { | ||
errs = append(errs, errors.New(fmt.Sprintf( | ||
`%v is less than minimum %v`, | ||
value.Float(), | ||
min, | ||
))) | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func (self Min) validateString(config string, value reflect.Value) []error { | ||
errs := []error{} | ||
min, err := strconv.ParseInt(config, 10, 64) | ||
|
||
if err != nil { | ||
errs = append(errs, err) | ||
return errs | ||
} | ||
|
||
if min > int64(value.Len()) { | ||
errs = append(errs, errors.New(fmt.Sprintf( | ||
`"%s" has a length of %v, which is less than minimum %v`, | ||
value.String(), | ||
value.Len(), | ||
min, | ||
))) | ||
} | ||
|
||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package rules_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aacebo/owl" | ||
) | ||
|
||
func Test_Min(t *testing.T) { | ||
t.Run("float", func(t *testing.T) { | ||
t.Run("should error", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A float64 `json:"a" owl:"min=3"` | ||
}{2}) | ||
|
||
if len(errs) == 0 { | ||
t.Error("should have error") | ||
} | ||
}) | ||
|
||
t.Run("should succeed", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A float32 `json:"a" owl:"min=3"` | ||
}{3}) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("int", func(t *testing.T) { | ||
t.Run("should error", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A int `json:"a" owl:"min=3"` | ||
}{2}) | ||
|
||
if len(errs) == 0 { | ||
t.Error("should have error") | ||
} | ||
}) | ||
|
||
t.Run("should succeed", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A int32 `json:"a" owl:"min=3"` | ||
}{3}) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("string", func(t *testing.T) { | ||
t.Run("should error", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A string `json:"a" owl:"min=3"` | ||
}{"ab"}) | ||
|
||
if len(errs) == 0 { | ||
t.Error("should have error") | ||
} | ||
}) | ||
|
||
t.Run("should succeed", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A string `json:"a" owl:"min=3"` | ||
}{"abc"}) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters