Skip to content

Commit 27f5911

Browse files
committed
validation - ensure all validators return the failing value
1 parent 74be825 commit 27f5911

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

helper/validation/meta.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ func NoZeroValues(i interface{}, k string) (s []string, es []error) {
1414
if reflect.ValueOf(i).Interface() == reflect.Zero(reflect.TypeOf(i)).Interface() {
1515
switch reflect.TypeOf(i).Kind() {
1616
case reflect.String:
17-
es = append(es, fmt.Errorf("%s must not be empty", k))
17+
es = append(es, fmt.Errorf("%s must not be empty, got %v", k, i))
1818
case reflect.Int, reflect.Float64:
19-
es = append(es, fmt.Errorf("%s must not be zero", k))
19+
es = append(es, fmt.Errorf("%s must not be zero, got %v", k, i))
2020
default:
2121
// this validator should only ever be applied to TypeString, TypeInt and TypeFloat
2222
panic(fmt.Errorf("can't use NoZeroValues with %T attribute %s", i, k))

helper/validation/strings.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func StringIsNotEmpty(i interface{}, k string) ([]string, []error) {
1818
}
1919

2020
if v == "" {
21-
return nil, []error{fmt.Errorf("expected %q to not be an empty string", k)}
21+
return nil, []error{fmt.Errorf("expected %q to not be an empty string, got %v", k, i)}
2222
}
2323

2424
return nil, nil
@@ -97,7 +97,7 @@ func StringMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc {
9797
return nil, []error{fmt.Errorf("invalid value for %s (%s)", k, message)}
9898

9999
}
100-
return nil, []error{fmt.Errorf("expected value of %s to match regular expression %q", k, r)}
100+
return nil, []error{fmt.Errorf("expected value of %s to match regular expression %q, got %v", k, r, i)}
101101
}
102102
return nil, nil
103103
}
@@ -118,7 +118,7 @@ func StringDoesNotMatch(r *regexp.Regexp, message string) schema.SchemaValidateF
118118
return nil, []error{fmt.Errorf("invalid value for %s (%s)", k, message)}
119119

120120
}
121-
return nil, []error{fmt.Errorf("expected value of %s to not match regular expression %q", k, r)}
121+
return nil, []error{fmt.Errorf("expected value of %s to not match regular expression %q, got %v", k, r, i)}
122122
}
123123
return nil, nil
124124
}
@@ -157,7 +157,7 @@ func StringDoesNotContainAny(chars string) schema.SchemaValidateFunc {
157157
}
158158

159159
if strings.ContainsAny(v, chars) {
160-
es = append(es, fmt.Errorf("expected value of %s to not contain any of %q", k, chars))
160+
es = append(es, fmt.Errorf("expected value of %s to not contain any of %q, got %v", k, chars, i))
161161
return
162162
}
163163

helper/validation/web.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ func IsURLWithScheme(validSchemes []string) schema.SchemaValidateFunc {
2828
}
2929

3030
if v == "" {
31-
errors = append(errors, fmt.Errorf("expected %q url to not be empty", k))
31+
errors = append(errors, fmt.Errorf("expected %q url to not be empty, got %v", k, i))
3232
return
3333
}
3434

3535
u, err := url.Parse(v)
3636
if err != nil {
37-
errors = append(errors, fmt.Errorf("%q url is in an invalid format: %q (%+v)", k, v, err))
37+
errors = append(errors, fmt.Errorf("expected %q to be a valid url, got %v: %+v", k, v, err))
3838
return
3939
}
4040

4141
if u.Host == "" {
42-
errors = append(errors, fmt.Errorf("%q url has no host: %q", k, v))
42+
errors = append(errors, fmt.Errorf("expected %q to have a host, got %v", k, v))
4343
return
4444
}
4545

@@ -49,7 +49,7 @@ func IsURLWithScheme(validSchemes []string) schema.SchemaValidateFunc {
4949
}
5050
}
5151

52-
errors = append(errors, fmt.Errorf("expected %q url %q to have a schema of: %q", k, v, strings.Join(validSchemes, ",")))
52+
errors = append(errors, fmt.Errorf("expected %q to have a url with schema of: %q, got %v", k, strings.Join(validSchemes, ","), v))
5353
return
5454
}
5555
}

0 commit comments

Comments
 (0)