Skip to content

Commit

Permalink
feat(*): Added refine method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass committed Jan 31, 2024
1 parent 4ee2561 commit 6ff35c6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 45 deletions.
17 changes: 17 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ func main() {
Age: 10,
}

// 1
s1 := schema.Object(map[string]schema.ISchema{
"Firstname": schema.String().Min(5),
"Lastname": schema.Int(),
"Age": schema.Int().Lt(10),
}).Parse(user)

fmt.Printf("(1): is valid: %t\n", s1.IsValid())

// 2
s2 := schema.String().Refine(func(value string) bool {
return value == "john"
}).Parse("john")

fmt.Printf("(2): is valid: %t\n", s2.IsValid())

// 3
s3 := schema.Object(map[string]schema.ISchema{
"Firstname": schema.String(),
}).Refine(func(value map[string]interface{}) bool {
return value["Firstname"] == "john"
}).Parse(user)

fmt.Printf("(3): is valid: %t\n", s3.IsValid())
}
24 changes: 1 addition & 23 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,4 @@ func (s *IntSchema) Gt(min int) *IntSchema {
s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Parse(value any) *ValidationResult {
val, ok := value.(int)
if !ok {
return &ValidationResult{Errors: []ValidationError{{Path: "", Message: fmt.Sprintf("Expected int, received %T", value)}}}
}

res := &ValidationResult{}

for _, validator := range s.validators {
if !validator.ValidateFunc(val) {
err := ValidationError{
Path: "",
Message: validator.MessageFunc(val),
}

res.Errors = append(res.Errors, err)
}
}

return res
}
}
43 changes: 43 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type ObjectSchema struct {
value map[string]ISchema
Schema[map[string]interface{}]
}

var _ ISchema = (*ObjectSchema)(nil)
Expand All @@ -15,6 +16,19 @@ func Object(obj map[string]ISchema) *ObjectSchema {
return &ObjectSchema{value: obj}
}

func (s *ObjectSchema) Refine(predicate func(map[string]interface{}) bool) *ObjectSchema {
validator := Validator[map[string]interface{}]{
MessageFunc: func(value map[string]interface{}) string {
return "Invalid input"
},
ValidateFunc: predicate,
}

s.validators = append(s.validators, validator)

return s
}

func (s *ObjectSchema) Parse(value any) *ValidationResult {
t := reflect.TypeOf(value)
val := reflect.ValueOf(value)
Expand Down Expand Up @@ -51,5 +65,34 @@ func (s *ObjectSchema) Parse(value any) *ValidationResult {
}
}

valueMap := StructToMap(value)

for _, validator := range s.validators {
if !validator.ValidateFunc(valueMap) {
err := ValidationError{
Path: "",
Message: validator.MessageFunc(valueMap),
}

res.Errors = append(res.Errors, err)
}
}

return res
}

func StructToMap(item interface{}) map[string]interface{} {
result := map[string]interface{}{}

val := reflect.ValueOf(item)
typ := reflect.TypeOf(item)

for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
value := val.Field(i).Interface()

result[field.Name] = value
}

return result
}
39 changes: 39 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package schema

import (
"fmt"
)

type ValidationResult struct {
Errors []ValidationError
}
Expand Down Expand Up @@ -29,3 +33,38 @@ type Validator[T any] struct {
MessageFunc func(T) string
ValidateFunc func(T) bool
}

func (s *Schema[T]) Refine(predicate func(T) bool) *Schema[T] {
validator := Validator[T]{
MessageFunc: func(value T) string {
return "Invalid input"
},
ValidateFunc: predicate,
}

s.validators = append(s.validators, validator)

return s
}

func (s *Schema[T]) Parse(value any) *ValidationResult {
val, ok := value.(T)
if !ok {
return &ValidationResult{Errors: []ValidationError{{Path: "", Message: fmt.Sprintf("Expected string, received %T", value)}}}
}

res := &ValidationResult{}

for _, validator := range s.validators {
if !validator.ValidateFunc(val) {
err := ValidationError{
Path: "",
Message: validator.MessageFunc(val),
}

res.Errors = append(res.Errors, err)
}
}

return res
}
22 changes: 0 additions & 22 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,3 @@ func (s *StringSchema) EndsWith(str string) *StringSchema {

return s
}

func (s *StringSchema) Parse(value any) *ValidationResult {
val, ok := value.(string)
if !ok {
return &ValidationResult{Errors: []ValidationError{{Path: "", Message: fmt.Sprintf("Expected string, received %T", value)}}}
}

res := &ValidationResult{}

for _, validator := range s.validators {
if !validator.ValidateFunc(val) {
err := ValidationError{
Path: "",
Message: validator.MessageFunc(val),
}

res.Errors = append(res.Errors, err)
}
}

return res
}

0 comments on commit 6ff35c6

Please sign in to comment.