Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass committed Jan 30, 2024
1 parent d3076c3 commit 15305f6
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 19 deletions.
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func main() {
rules := validator.Rules(
validator.RuleFor(
user.Firstname,
validator.Required(),
validator.Required[string](),
validator.Min(4),
Custom(),
Custom[string](),
).WithName("firstname"),
validator.RuleFor(
user.Lastname,
validator.Required(),
validator.Required[string](),
validator.Min(3),
).WithName("lastname"),
validator.RuleFor(user.Age, validator.Min(0)),

Check failure on line 34 in examples/main.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

type *validator.RuleConfig[string] of validator.Min(0) does not match inferred type *validator.RuleConfig[int] for *validator.RuleConfig[T]

Check failure on line 34 in examples/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

type *validator.RuleConfig[string] of validator.Min(0) does not match inferred type *validator.RuleConfig[int] for *validator.RuleConfig[T]
Expand All @@ -45,7 +45,7 @@ func Custom[T any]() *validator.RuleConfig[T] {
return fmt.Sprintf("'%s' must equal 'jane'", r.Value)
},
ValidateFunc: func(r validator.RuleConfig[T]) bool {
val, ok := r.Value.(string)
val, ok := any(r.Value).(string)
if !ok {
return false
}
Expand Down
7 changes: 5 additions & 2 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ func Must[T any](predicate func() bool) *RuleConfig[T] {
}
}

func Required[T string]() *RuleConfig[T] {
func Required[T any]() *RuleConfig[T] {
return &RuleConfig[T]{
MessageFunc: func(r RuleConfig[T]) string {
return "Required"
},
ValidateFunc: func(r RuleConfig[T]) bool {
return len(r.Value) > 0
// if needed
// if val is string, check against nil, empty string
// if val is int, check against nil, 0 etc.
return any(r.Value) != nil
},
}
}
Expand Down
32 changes: 20 additions & 12 deletions test-01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,49 @@ type Validator struct {
Errors []error
}

type Rule func(*Validator) *Validator
type Rule[T any] func(*T) *T

type StringValidator struct {
Validator
*Validator
}

func NewStringValidator(value string, opts ...Rule) *StringValidator {
v := &StringValidator{Validator{value: value}}
func NewStringValidator(value string, opts ...StringRule) *StringValidator {

Check failure on line 16 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

undefined: StringRule

Check failure on line 16 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

undefined: StringRule
v := &StringValidator{&Validator{value: value}}
for _, opt := range opts {
opt(v)
}

return v
}

func NewIntValidator[T any](value T, opts ...Rule) *Validator {
v := &Validator{value: value}
type IntRule func(*IntValidator) *IntValidator

type IntValidator struct {
*Validator
}

func NewIntValidator(value int, opts ...IntRule) *IntValidator {
v := &IntValidator{&Validator{value: value}}
for _, opt := range opts {
opt(v)
}

return v
}

func Min(min int) Rule {
func Min(min int) StringRule {

Check failure on line 40 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

undefined: StringRule

Check failure on line 40 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

undefined: StringRule
return func(v *StringValidator) *StringValidator {
if len(v.value) < min {
v.Errors = append(v.Errors, fmt.Errorf("min length not met."))
if str, ok := any(v.value).(string); ok {
if len(str) < min {
v.Errors = append(v.Errors, fmt.Errorf("min length not met."))
}
}
return v
}
}

func Test() Rule {
return func(v *Validator) *Validator {
func Required[T any]() Rule[T] {
return func(v *T) *T {
if str, ok := any(v.value).(string); ok {

Check failure on line 53 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

v.value undefined (type *T is pointer to type parameter, not type parameter)

Check failure on line 53 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

v.value undefined (type *T is pointer to type parameter, not type parameter)
if str != "test" {
v.Errors = append(v.Errors, fmt.Errorf("value does not equal 'test'"))

Check failure on line 55 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

v.Errors undefined (type *T is pointer to type parameter, not type parameter)

Check failure on line 55 in test-01/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

v.Errors undefined (type *T is pointer to type parameter, not type parameter)
Expand All @@ -52,7 +60,7 @@ func Test() Rule {
}

func main() {
v := NewStringValidator("john", Test(), Min(5))
v := NewStringValidator("john", Required[StringValidator](), Min(5))
// NewValidator(10, Min(5), Test[string]())

fmt.Printf("error: %v\n", v.Errors)
Expand Down
95 changes: 95 additions & 0 deletions test-02/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// zod

package main

import "fmt"

type Schema struct{}

func NewSchema() *Schema {
s := &Schema{}

return s
}

// string
type StringSchema struct {
checks []func(string) bool
}

func (s *Schema) String() *StringSchema {
v := &StringSchema{}

return v
}

func (s *StringSchema) Min(min int) *StringSchema {
s.checks = append(s.checks, func(value string) bool {
if len(value) < min {
return false
}
return true
})

return s
}

func (s *StringSchema) Parse(value any) bool {
val, ok := value.(string)
if !ok {
return false
}

for _, check := range s.checks {
if !check(val) {
return false
}
}

return true
}

// int
type IntSchema struct {
checks []func(int) bool
}

func (s *Schema) Int() *IntSchema {
v := &IntSchema{}

return v
}

func (s *IntSchema) LessThan(min int) *IntSchema {
s.checks = append(s.checks, func(value int) bool {
if value < min {
return true
}
return false
})

return s
}

func (s *IntSchema) Parse(value any) bool {
val, ok := value.(int)
if !ok {
return false
}

for _, check := range s.checks {
if !check(val) {
return false
}
}

return true
}

func main() {
v := NewSchema().String().Min(4).Parse("john")
s := NewSchema().Int().LessThan(4).Parse(1)

fmt.Printf("(1) is valid: %t\n", v)
fmt.Printf("(2) is valid: %t\n", s)
}
1 change: 1 addition & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// generics (simple)
package main

import "fmt"
Expand Down
2 changes: 1 addition & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_GreaterThanValid_ReturnsTrue(t *testing.T) {

rule := validator.RuleFor(
user.Age,
validator.Required(),
validator.Required[int](),
validator.GreaterThan(17),
)

Expand Down

0 comments on commit 15305f6

Please sign in to comment.