Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Oct 6, 2024
1 parent 2268c9b commit 7d9fb0e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 101 deletions.
14 changes: 6 additions & 8 deletions any.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ func (self *AnySchema) Rule(key string, value any, rule RuleFn) *AnySchema {
func (self *AnySchema) Required() *AnySchema {
return self.Rule("required", true, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return value, errors.New("required")
return nil, errors.New("required")
}

return value, nil
return value.Interface(), nil
})
}

func (self *AnySchema) Enum(values ...any) *AnySchema {
return self.Rule("enum", values, func(value reflect.Value) (any, error) {
for _, v := range values {
if value.Equal(reflect.Indirect(reflect.ValueOf(v))) {
return value, nil
return value.Interface(), nil
}
}

return value, fmt.Errorf("must be one of %v", values)
return nil, fmt.Errorf("must be one of %v", values)
})
}

Expand All @@ -71,7 +71,7 @@ func (self AnySchema) MarshalJSON() ([]byte, error) {
}

func (self AnySchema) Validate(value any) error {
return self.validate("<root>", reflect.Indirect(reflect.ValueOf(value)))
return self.validate("", reflect.Indirect(reflect.ValueOf(value)))
}

func (self AnySchema) validate(key string, value reflect.Value) error {
Expand All @@ -85,9 +85,7 @@ func (self AnySchema) validate(key string, value reflect.Value) error {
continue
}

if value.CanSet() {
value.Set(reflect.ValueOf(v))
}
value = reflect.ValueOf(v)
}

if len(err.Errors) > 0 {
Expand Down
24 changes: 10 additions & 14 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func Float() *FloatSchema {
}

if value.CanConvert(reflect.TypeFor[float64]()) {
value.Set(value.Convert(reflect.TypeFor[float64]()))
value = value.Convert(reflect.TypeFor[float64]())
}

if value.Kind() != reflect.Float64 {
return nil, errors.New("must be a float")
return value.Interface(), errors.New("must be a float")
}

return value.Interface(), nil
Expand Down Expand Up @@ -60,28 +60,28 @@ func (self *FloatSchema) Enum(values ...float64) *FloatSchema {
func (self *FloatSchema) Min(min float64) *FloatSchema {
return self.Rule("min", min, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return value, nil
return nil, nil
}

if value.Float() < min {
return value, fmt.Errorf("must have value of at least %f", min)
return value.Interface(), fmt.Errorf("must have value of at least %f", min)
}

return value, nil
return value.Interface(), nil
})
}

func (self *FloatSchema) Max(max float64) *FloatSchema {
return self.Rule("max", max, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return value, nil
return nil, nil
}

if value.Float() > max {
return value, fmt.Errorf("must have value of at most %f", max)
return value.Interface(), fmt.Errorf("must have value of at most %f", max)
}

return value, nil
return value.Interface(), nil
})
}

Expand All @@ -90,13 +90,9 @@ func (self FloatSchema) MarshalJSON() ([]byte, error) {
}

func (self FloatSchema) Validate(value any) error {
return self.validate("<root>", reflect.Indirect(reflect.ValueOf(value)))
return self.validate("", reflect.ValueOf(value))
}

func (self FloatSchema) validate(key string, value reflect.Value) error {
if err := self.schema.validate(key, value); err != nil {
return err
}

return nil
return self.schema.validate(key, value)
}
48 changes: 27 additions & 21 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package owl

import (
"encoding/json"
"errors"
"fmt"
"reflect"
)
Expand All @@ -11,7 +12,24 @@ type IntSchema struct {
}

func Int() *IntSchema {
return &IntSchema{Any()}
self := &IntSchema{Any()}
self.Rule("type", self.Type(), func(value reflect.Value) (any, error) {
if !value.IsValid() {
return nil, nil
}

if value.CanConvert(reflect.TypeFor[int]()) {
value = value.Convert(reflect.TypeFor[int]())
}

if value.Kind() != reflect.Int {
return value.Interface(), errors.New("must be an int")
}

return value.Interface(), nil
})

return self
}

func (self IntSchema) Type() string {
Expand Down Expand Up @@ -42,28 +60,28 @@ func (self *IntSchema) Enum(values ...int) *IntSchema {
func (self *IntSchema) Min(min int) *IntSchema {
return self.Rule("min", min, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return value, nil
return nil, nil
}

if value.Int() < int64(min) {
return value, fmt.Errorf("must have value of at least %d", min)
return value.Interface(), fmt.Errorf("must have value of at least %d", min)
}

return value, nil
return value.Interface(), nil
})
}

func (self *IntSchema) Max(max int) *IntSchema {
return self.Rule("max", max, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return value, nil
return nil, nil
}

if value.Int() > int64(max) {
return value, fmt.Errorf("must have value of at most %d", max)
return value.Interface(), fmt.Errorf("must have value of at most %d", max)
}

return value, nil
return value.Interface(), nil
})
}

Expand All @@ -72,21 +90,9 @@ func (self IntSchema) MarshalJSON() ([]byte, error) {
}

func (self IntSchema) Validate(value any) error {
return self.validate("<root>", reflect.Indirect(reflect.ValueOf(value)))
return self.validate("", reflect.ValueOf(value))
}

func (self IntSchema) validate(key string, value reflect.Value) error {
if value.IsValid() && value.CanConvert(reflect.TypeFor[int]()) {
value = value.Convert(reflect.TypeFor[int]())
}

if err := self.schema.validate(key, value); err != nil {
return err
}

if value.IsValid() && value.Kind() != reflect.Int {
return newError(key, "must be an integer")
}

return nil
return self.schema.validate(key, value)
}
24 changes: 17 additions & 7 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ type ObjectSchema struct {
}

func Object() *ObjectSchema {
return &ObjectSchema{Any(), map[string]Schema{}}
self := &ObjectSchema{Any(), map[string]Schema{}}
self.Rule("type", self.Type(), func(value reflect.Value) (any, error) {
if !value.IsValid() {
return nil, nil
}

if value.Kind() != reflect.Struct && value.Kind() != reflect.Map {
return value.Interface(), errors.New("must be an object")
}

return value.Interface(), nil
})

return self
}

func (self ObjectSchema) Type() string {
Expand All @@ -39,7 +52,7 @@ func (self ObjectSchema) MarshalJSON() ([]byte, error) {
}

func (self ObjectSchema) Validate(value any) error {
return self.validate("<root>", reflect.Indirect(reflect.ValueOf(value)))
return self.validate("", reflect.Indirect(reflect.ValueOf(value)))
}

func (self ObjectSchema) validate(key string, value reflect.Value) error {
Expand All @@ -55,14 +68,11 @@ func (self ObjectSchema) validate(key string, value reflect.Value) error {
value = value.Elem()
}

switch value.Kind() {
case reflect.Map:
if value.Kind() == reflect.Map {
return self.validateMap(key, value)
case reflect.Struct:
return self.validateStruct(key, value)
}

return newError(key, "must be an object")
return self.validateStruct(key, value)
}

func (self ObjectSchema) validateMap(key string, value reflect.Value) error {
Expand Down
Loading

0 comments on commit 7d9fb0e

Please sign in to comment.