-
Notifications
You must be signed in to change notification settings - Fork 18
/
fielderrors.go
156 lines (134 loc) · 4.27 KB
/
fielderrors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2019 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package validation
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// Deprecated CurrentField is an empty string representing an empty path to the current field.
const CurrentField = ""
type FieldValidator interface {
Validate() FieldErrors
}
// Deprecated FieldErrors extends an ErrorList to compose helper methods to compose field paths.
type FieldErrors field.ErrorList
// Deprecated Also appends additional field errors to the current set of errors.
func (e FieldErrors) Also(errs ...FieldErrors) FieldErrors {
aggregate := e
for _, err := range errs {
aggregate = append(aggregate, err...)
}
return aggregate
}
// Deprecated ViaField prepends the path of each error with the field's key using a dot notation separator (e.g. '.foo')
func (e FieldErrors) ViaField(key string) FieldErrors {
errs := make(FieldErrors, len(e))
for i, err := range e {
newField := key
if !strings.HasPrefix(err.Field, "[") {
newField = newField + "."
}
if err.Field != "[]" {
newField = newField + err.Field
}
errs[i] = &field.Error{
Type: err.Type,
Field: newField,
BadValue: err.BadValue,
Detail: err.Detail,
}
}
return errs
}
// Deprecated ViaIndex prepends the path of each error with the field's index using square bracket separators (e.g. '[0]').
func (e FieldErrors) ViaIndex(index int) FieldErrors {
errs := make(FieldErrors, len(e))
for i, err := range e {
newField := fmt.Sprintf("[%d]", index)
if !strings.HasPrefix(err.Field, "[") {
newField = newField + "."
}
if err.Field != "[]" {
newField = newField + err.Field
}
errs[i] = &field.Error{
Type: err.Type,
Field: newField,
BadValue: err.BadValue,
Detail: err.Detail,
}
}
return errs
}
// Deprecated ViaFieldIndex prepends the path of each error with the fields key and index (e.g. '.foo[0]').
func (e FieldErrors) ViaFieldIndex(key string, index int) FieldErrors {
return e.ViaIndex(index).ViaField(key)
}
// Deprecated ErrorList converts a FieldErrors to an api machinery field ErrorList
func (e FieldErrors) ErrorList() field.ErrorList {
list := make(field.ErrorList, len(e))
for i := range e {
list[i] = e[i]
}
return list
}
// Deprecated ToAggregate combines the field errors into a single go error, or nil if there are no errors.
func (e FieldErrors) ToAggregate() error {
l := e.ErrorList()
if len(l) == 0 {
return nil
}
return l.ToAggregate()
}
// Deprecated
type Validatable = interface {
Validate(context.Context) FieldErrors
}
// Deprecated ErrDisallowedFields wraps a forbidden error as field errors
func ErrDisallowedFields(name string, detail string) FieldErrors {
return FieldErrors{
field.Forbidden(field.NewPath(name), detail),
}
}
// Deprecated ErrInvalidArrayValue wraps an invalid error for an array item as field errors
func ErrInvalidArrayValue(value interface{}, name string, index int) FieldErrors {
return FieldErrors{
field.Invalid(field.NewPath(name).Index(index), value, ""),
}
}
// Deprecated ErrInvalidValue wraps an invalid error as field errors
func ErrInvalidValue(value interface{}, name string) FieldErrors {
return FieldErrors{
field.Invalid(field.NewPath(name), value, ""),
}
}
// Deprecated ErrDuplicateValue wraps an duplicate error as field errors
func ErrDuplicateValue(value interface{}, names ...string) FieldErrors {
errs := FieldErrors{}
for _, name := range names {
errs = append(errs, field.Duplicate(field.NewPath(name), value))
}
return errs
}
// Deprecated ErrMissingField wraps an required error as field errors
func ErrMissingField(name string) FieldErrors {
return FieldErrors{
field.Required(field.NewPath(name), ""),
}
}
// Deprecated ErrMissingOneOf wraps an required error for the specified fields as field errors
func ErrMissingOneOf(names ...string) FieldErrors {
return FieldErrors{
field.Required(field.NewPath(fmt.Sprintf("[%s]", strings.Join(names, ", "))), "expected exactly one, got neither"),
}
}
// Deprecated ErrMultipleOneOf wraps an required error for the specified fields as field errors
func ErrMultipleOneOf(names ...string) FieldErrors {
return FieldErrors{
field.Required(field.NewPath(fmt.Sprintf("[%s]", strings.Join(names, ", "))), "expected exactly one, got both"),
}
}