Skip to content

Commit

Permalink
add min
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Apr 4, 2024
1 parent 63ea06e commit 68c3287
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
|---------------|-------------------------------------------------------|--------|
| pattern | match regular expression ||
| format | match format ||
| min | min length | |
| min | min length | |
| max | max length ||

### Numeric

| Name | Description | Status |
|---------------|-------------------------------------------------------|--------|
| min | minimum | |
| min | minimum | |
| max | maximum ||

## Formats
Expand Down
2 changes: 1 addition & 1 deletion rules/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (self Format) Validate(config string, parent reflect.Value, value reflect.V
errs := []error{}

if config == "" {
errs = append(errs, errors.New("invalid empty format"))
errs = append(errs, errors.New("empty config"))
return errs
}

Expand Down
76 changes: 76 additions & 0 deletions rules/min.go
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
}
75 changes: 75 additions & 0 deletions rules/min_test.go
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)
}
})
})
}
1 change: 1 addition & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func New() *owl {
rules: map[string]Rule{
"required": rules.Required{},
"pattern": rules.Pattern{},
"min": rules.Min{},
},
formats: map[string]Formatter{
"date_time": formats.DateTime,
Expand Down

0 comments on commit 68c3287

Please sign in to comment.