Skip to content

Commit cc8a498

Browse files
committed
Make If return one validator to make usage easier
1 parent a90fc4d commit cc8a498

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ func (v Violation) Error() string {
6060
return fmt.Sprintf("violation code: %s, args: %v", v.Code, v.Args)
6161
}
6262

63+
type Violations []Violation
64+
65+
func (v Violations) Error() string {
66+
var errs []string
67+
68+
for _, err := range v {
69+
errs = append(errs, err.Error())
70+
}
71+
72+
return fmt.Sprintf("violations: %v", errs)
73+
}
74+
6375
type Args map[string]any
6476

6577
func (e Args) Add(key string, value any) Args {

examples_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func ExampleCollect() {
4141

4242
func ExampleIf() {
4343
err := validate.Join(
44-
validate.Field("email", "test", validate.If(true, validate.Email)...),
45-
validate.Field("iban", "invalid", validate.If(false, validate.IBAN)...),
44+
validate.Field("email", "test", validate.If(true, validate.Email)),
45+
validate.Field("iban", "invalid", validate.If(false, validate.IBAN)),
4646
)
4747
printError(err)
4848

validate.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,25 @@ func Collect(err error) []Error {
8282
}
8383

8484
// If will run the validators only if the shouldRun is true.
85-
func If[T any](shouldRun bool, validators ...Validator[T]) []Validator[T] {
85+
func If[T any](shouldRun bool, validators ...Validator[T]) Validator[T] {
8686
if !shouldRun {
87-
return nil
87+
return func(value T) error {
88+
return nil
89+
}
8890
}
8991

90-
return validators
92+
return func(v T) error {
93+
violations, err := validate(v, validators...)
94+
if err != nil {
95+
return err
96+
}
97+
98+
if violations == nil {
99+
return nil
100+
}
101+
102+
return Violations(violations)
103+
}
91104
}
92105

93106
// FailFirst will run the validators in order and return the first error.
@@ -151,9 +164,12 @@ func validate[T any](
151164
continue
152165
}
153166

154-
if violation, ok := err.(*Violation); ok {
155-
violations = append(violations, *violation)
156-
} else {
167+
switch err := err.(type) {
168+
case Violations:
169+
violations = append(violations, err...)
170+
case *Violation:
171+
violations = append(violations, *err)
172+
default:
157173
return nil, err
158174
}
159175
}

validate_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,31 @@ func TestReturnError(t *testing.T) {
3131
}
3232

3333
func TestIf(t *testing.T) {
34-
// Validate true condition should run the validator.
34+
// Validate true condition should run the validators.
3535
err := validate.Field(
3636
"first_name",
3737
"John",
38-
validate.If(true, failValidator[string])...,
38+
validate.If(
39+
true,
40+
failValidatorWithCode[string]("first"),
41+
failValidatorWithCode[string]("second"),
42+
successValidator[string],
43+
),
3944
)
4045
require.NotNil(t, err)
4146

4247
errs := validate.Collect(err)
4348
require.Equal(t, 1, len(errs))
49+
require.Equal(t, 2, len(errs[0].Violations))
4450
require.Equal(t, "first_name", errs[0].Path)
45-
require.Equal(t, "fail", errs[0].Violations[0].Code)
51+
require.Equal(t, "first", errs[0].Violations[0].Code)
52+
require.Equal(t, "second", errs[0].Violations[1].Code)
4653

4754
// Validate false condition should not run the validator.
4855
err = validate.Field[string](
4956
"first_name",
5057
"John",
51-
validate.If(false, failValidator[string])...,
58+
validate.If(false, failValidator[string]),
5259
)
5360
require.Nil(t, err)
5461
}
@@ -188,3 +195,7 @@ func failValidatorWithCode[T any](code string) validate.Validator[T] {
188195
func failValidator[T any](value T) error {
189196
return &validate.Violation{Code: "fail"}
190197
}
198+
199+
func successValidator[T any](value T) error {
200+
return nil
201+
}

0 commit comments

Comments
 (0)