Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
d64d1fbb
Commit
d64d1fbb
authored
Nov 03, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
s/ValidationError/Error/
parent
2a127d08
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
70 additions
and
70 deletions
+70
-70
errors.go
pkg/api/errors/errors.go
+1
-1
errors_test.go
pkg/api/errors/errors_test.go
+1
-1
validation.go
pkg/api/validation/validation.go
+1
-1
validation_test.go
pkg/api/validation/validation_test.go
+19
-19
validation_test.go
pkg/apis/extensions/validation/validation_test.go
+4
-4
errors.go
pkg/util/validation/errors.go
+33
-33
errors_test.go
pkg/util/validation/errors_test.go
+11
-11
No files found.
pkg/api/errors/errors.go
View file @
d64d1fbb
...
@@ -162,7 +162,7 @@ func NewConflict(kind, name string, err error) error {
...
@@ -162,7 +162,7 @@ func NewConflict(kind, name string, err error) error {
func
NewInvalid
(
kind
,
name
string
,
errs
validation
.
ValidationErrorList
)
error
{
func
NewInvalid
(
kind
,
name
string
,
errs
validation
.
ValidationErrorList
)
error
{
causes
:=
make
([]
unversioned
.
StatusCause
,
0
,
len
(
errs
))
causes
:=
make
([]
unversioned
.
StatusCause
,
0
,
len
(
errs
))
for
i
:=
range
errs
{
for
i
:=
range
errs
{
if
err
,
ok
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
);
ok
{
if
err
,
ok
:=
errs
[
i
]
.
(
*
validation
.
Error
);
ok
{
causes
=
append
(
causes
,
unversioned
.
StatusCause
{
causes
=
append
(
causes
,
unversioned
.
StatusCause
{
Type
:
unversioned
.
CauseType
(
err
.
Type
),
Type
:
unversioned
.
CauseType
(
err
.
Type
),
Message
:
err
.
ErrorBody
(),
Message
:
err
.
ErrorBody
(),
...
...
pkg/api/errors/errors_test.go
View file @
d64d1fbb
...
@@ -88,7 +88,7 @@ func TestErrorNew(t *testing.T) {
...
@@ -88,7 +88,7 @@ func TestErrorNew(t *testing.T) {
func
TestNewInvalid
(
t
*
testing
.
T
)
{
func
TestNewInvalid
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
Err
*
validation
.
Validation
Error
Err
*
validation
.
Error
Details
*
unversioned
.
StatusDetails
Details
*
unversioned
.
StatusDetails
}{
}{
{
{
...
...
pkg/api/validation/validation.go
View file @
d64d1fbb
...
@@ -1497,7 +1497,7 @@ func ValidateNodeUpdate(node, oldNode *api.Node) validation.ValidationErrorList
...
@@ -1497,7 +1497,7 @@ func ValidateNodeUpdate(node, oldNode *api.Node) validation.ValidationErrorList
// Clear status
// Clear status
oldNode
.
Status
=
node
.
Status
oldNode
.
Status
=
node
.
Status
// TODO: Add a 'real'
ValidationE
rror type for this error and provide print actual diffs.
// TODO: Add a 'real'
e
rror type for this error and provide print actual diffs.
if
!
api
.
Semantic
.
DeepEqual
(
oldNode
,
node
)
{
if
!
api
.
Semantic
.
DeepEqual
(
oldNode
,
node
)
{
glog
.
V
(
4
)
.
Infof
(
"Update failed validation %#v vs %#v"
,
oldNode
,
node
)
glog
.
V
(
4
)
.
Infof
(
"Update failed validation %#v vs %#v"
,
oldNode
,
node
)
allErrs
=
append
(
allErrs
,
fmt
.
Errorf
(
"update contains more than labels or capacity changes"
))
allErrs
=
append
(
allErrs
,
fmt
.
Errorf
(
"update contains more than labels or capacity changes"
))
...
...
pkg/api/validation/validation_test.go
View file @
d64d1fbb
...
@@ -36,7 +36,7 @@ import (
...
@@ -36,7 +36,7 @@ import (
func
expectPrefix
(
t
*
testing
.
T
,
prefix
string
,
errs
validation
.
ValidationErrorList
)
{
func
expectPrefix
(
t
*
testing
.
T
,
prefix
string
,
errs
validation
.
ValidationErrorList
)
{
for
i
:=
range
errs
{
for
i
:=
range
errs
{
if
f
,
p
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
,
prefix
;
!
strings
.
HasPrefix
(
f
,
p
)
{
if
f
,
p
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
,
prefix
;
!
strings
.
HasPrefix
(
f
,
p
)
{
t
.
Errorf
(
"expected prefix '%s' for field '%s' (%v)"
,
p
,
f
,
errs
[
i
])
t
.
Errorf
(
"expected prefix '%s' for field '%s' (%v)"
,
p
,
f
,
errs
[
i
])
}
}
}
}
...
@@ -150,7 +150,7 @@ func TestValidateLabels(t *testing.T) {
...
@@ -150,7 +150,7 @@ func TestValidateLabels(t *testing.T) {
if
len
(
errs
)
!=
1
{
if
len
(
errs
)
!=
1
{
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
}
else
{
}
else
{
detail
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
0
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
qualifiedNameErrorMsg
{
if
detail
!=
qualifiedNameErrorMsg
{
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
qualifiedNameErrorMsg
)
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
qualifiedNameErrorMsg
)
}
}
...
@@ -168,7 +168,7 @@ func TestValidateLabels(t *testing.T) {
...
@@ -168,7 +168,7 @@ func TestValidateLabels(t *testing.T) {
if
len
(
errs
)
!=
1
{
if
len
(
errs
)
!=
1
{
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
}
else
{
}
else
{
detail
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
0
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
labelValueErrorMsg
{
if
detail
!=
labelValueErrorMsg
{
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
labelValueErrorMsg
)
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
labelValueErrorMsg
)
}
}
...
@@ -215,7 +215,7 @@ func TestValidateAnnotations(t *testing.T) {
...
@@ -215,7 +215,7 @@ func TestValidateAnnotations(t *testing.T) {
if
len
(
errs
)
!=
1
{
if
len
(
errs
)
!=
1
{
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
}
}
detail
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
0
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
qualifiedNameErrorMsg
{
if
detail
!=
qualifiedNameErrorMsg
{
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
qualifiedNameErrorMsg
)
t
.
Errorf
(
"error detail %s should be equal %s"
,
detail
,
qualifiedNameErrorMsg
)
}
}
...
@@ -568,13 +568,13 @@ func TestValidateVolumes(t *testing.T) {
...
@@ -568,13 +568,13 @@ func TestValidateVolumes(t *testing.T) {
continue
continue
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
if
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Type
!=
v
.
T
{
if
errs
[
i
]
.
(
*
validation
.
Error
)
.
Type
!=
v
.
T
{
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
}
}
if
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
!=
v
.
F
{
if
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
!=
v
.
F
{
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
}
}
detail
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
v
.
D
{
if
detail
!=
v
.
D
{
t
.
Errorf
(
"%s: expected error detail
\"
%s
\"
, got
\"
%s
\"
"
,
k
,
v
.
D
,
detail
)
t
.
Errorf
(
"%s: expected error detail
\"
%s
\"
, got
\"
%s
\"
"
,
k
,
v
.
D
,
detail
)
}
}
...
@@ -627,13 +627,13 @@ func TestValidatePorts(t *testing.T) {
...
@@ -627,13 +627,13 @@ func TestValidatePorts(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
if
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Type
!=
v
.
T
{
if
errs
[
i
]
.
(
*
validation
.
Error
)
.
Type
!=
v
.
T
{
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
}
}
if
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
!=
v
.
F
{
if
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
!=
v
.
F
{
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
}
}
detail
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
v
.
D
{
if
detail
!=
v
.
D
{
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
k
,
v
.
D
,
detail
)
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
k
,
v
.
D
,
detail
)
}
}
...
@@ -772,7 +772,7 @@ func TestValidateEnv(t *testing.T) {
...
@@ -772,7 +772,7 @@ func TestValidateEnv(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
tc
.
name
)
t
.
Errorf
(
"expected failure for %s"
,
tc
.
name
)
}
else
{
}
else
{
for
i
:=
range
errs
{
for
i
:=
range
errs
{
str
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Error
()
str
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Error
()
if
str
!=
""
&&
str
!=
tc
.
expectedError
{
if
str
!=
""
&&
str
!=
tc
.
expectedError
{
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
tc
.
name
,
tc
.
expectedError
,
str
)
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
tc
.
name
,
tc
.
expectedError
,
str
)
}
}
...
@@ -2560,7 +2560,7 @@ func TestValidateReplicationController(t *testing.T) {
...
@@ -2560,7 +2560,7 @@ func TestValidateReplicationController(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
field
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
if
!
strings
.
HasPrefix
(
field
,
"spec.template."
)
&&
if
!
strings
.
HasPrefix
(
field
,
"spec.template."
)
&&
field
!=
"metadata.name"
&&
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
&&
field
!=
"metadata.namespace"
&&
...
@@ -2676,7 +2676,7 @@ func TestValidateNode(t *testing.T) {
...
@@ -2676,7 +2676,7 @@ func TestValidateNode(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
field
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
expectedFields
:=
map
[
string
]
bool
{
expectedFields
:=
map
[
string
]
bool
{
"metadata.name"
:
true
,
"metadata.name"
:
true
,
"metadata.labels"
:
true
,
"metadata.labels"
:
true
,
...
@@ -3008,7 +3008,7 @@ func TestValidateResourceNames(t *testing.T) {
...
@@ -3008,7 +3008,7 @@ func TestValidateResourceNames(t *testing.T) {
}
else
if
len
(
err
)
==
0
&&
!
item
.
success
{
}
else
if
len
(
err
)
==
0
&&
!
item
.
success
{
t
.
Errorf
(
"expected failure for input %q"
,
item
.
input
)
t
.
Errorf
(
"expected failure for input %q"
,
item
.
input
)
for
i
:=
range
err
{
for
i
:=
range
err
{
detail
:=
err
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
err
[
i
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
""
&&
detail
!=
qualifiedNameErrorMsg
{
if
detail
!=
""
&&
detail
!=
qualifiedNameErrorMsg
{
t
.
Errorf
(
"%d: expected error detail either empty or %s, got %s"
,
k
,
qualifiedNameErrorMsg
,
detail
)
t
.
Errorf
(
"%d: expected error detail either empty or %s, got %s"
,
k
,
qualifiedNameErrorMsg
,
detail
)
}
}
...
@@ -3224,7 +3224,7 @@ func TestValidateLimitRange(t *testing.T) {
...
@@ -3224,7 +3224,7 @@ func TestValidateLimitRange(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
detail
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Detail
if
detail
!=
v
.
D
{
if
detail
!=
v
.
D
{
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
k
,
v
.
D
,
detail
)
t
.
Errorf
(
"%s: expected error detail either empty or %s, got %s"
,
k
,
v
.
D
,
detail
)
}
}
...
@@ -3329,8 +3329,8 @@ func TestValidateResourceQuota(t *testing.T) {
...
@@ -3329,8 +3329,8 @@ func TestValidateResourceQuota(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
field
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
detail
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Detail
detail
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Detail
if
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
&&
!
api
.
IsStandardResourceName
(
field
)
{
if
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
&&
!
api
.
IsStandardResourceName
(
field
)
{
t
.
Errorf
(
"%s: missing prefix for: %v"
,
k
,
field
)
t
.
Errorf
(
"%s: missing prefix for: %v"
,
k
,
field
)
}
}
...
@@ -3937,7 +3937,7 @@ func TestValidateEndpoints(t *testing.T) {
...
@@ -3937,7 +3937,7 @@ func TestValidateEndpoints(t *testing.T) {
}
}
for
k
,
v
:=
range
errorCases
{
for
k
,
v
:=
range
errorCases
{
if
errs
:=
ValidateEndpoints
(
&
v
.
endpoints
);
len
(
errs
)
==
0
||
errs
[
0
]
.
(
*
validation
.
ValidationError
)
.
Type
!=
v
.
errorType
||
!
strings
.
Contains
(
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
.
Detail
,
v
.
errorDetail
)
{
if
errs
:=
ValidateEndpoints
(
&
v
.
endpoints
);
len
(
errs
)
==
0
||
errs
[
0
]
.
(
*
validation
.
Error
)
.
Type
!=
v
.
errorType
||
!
strings
.
Contains
(
errs
[
0
]
.
(
*
validation
.
Error
)
.
Detail
,
v
.
errorDetail
)
{
t
.
Errorf
(
"Expected error type %s with detail %s for %s, got %v"
,
v
.
errorType
,
v
.
errorDetail
,
k
,
errs
)
t
.
Errorf
(
"Expected error type %s with detail %s for %s, got %v"
,
v
.
errorType
,
v
.
errorDetail
,
k
,
errs
)
}
}
}
}
...
@@ -4017,7 +4017,7 @@ func TestValidateSecurityContext(t *testing.T) {
...
@@ -4017,7 +4017,7 @@ func TestValidateSecurityContext(t *testing.T) {
},
},
}
}
for
k
,
v
:=
range
errorCases
{
for
k
,
v
:=
range
errorCases
{
if
errs
:=
ValidateSecurityContext
(
v
.
sc
);
len
(
errs
)
==
0
||
errs
[
0
]
.
(
*
validation
.
ValidationError
)
.
Type
!=
v
.
errorType
||
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
.
Detail
!=
v
.
errorDetail
{
if
errs
:=
ValidateSecurityContext
(
v
.
sc
);
len
(
errs
)
==
0
||
errs
[
0
]
.
(
*
validation
.
Error
)
.
Type
!=
v
.
errorType
||
errs
[
0
]
.
(
*
validation
.
Error
)
.
Detail
!=
v
.
errorDetail
{
t
.
Errorf
(
"Expected error type %s with detail %s for %s, got %v"
,
v
.
errorType
,
v
.
errorDetail
,
k
,
errs
)
t
.
Errorf
(
"Expected error type %s with detail %s for %s, got %v"
,
v
.
errorType
,
v
.
errorDetail
,
k
,
errs
)
}
}
}
}
...
...
pkg/apis/extensions/validation/validation_test.go
View file @
d64d1fbb
...
@@ -675,7 +675,7 @@ func TestValidateDaemonSet(t *testing.T) {
...
@@ -675,7 +675,7 @@ func TestValidateDaemonSet(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
}
for
i
:=
range
errs
{
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
*
validation
.
Validation
Error
)
.
Field
field
:=
errs
[
i
]
.
(
*
validation
.
Error
)
.
Field
if
!
strings
.
HasPrefix
(
field
,
"spec.template."
)
&&
if
!
strings
.
HasPrefix
(
field
,
"spec.template."
)
&&
field
!=
"metadata.name"
&&
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
&&
field
!=
"metadata.namespace"
&&
...
@@ -918,7 +918,7 @@ func TestValidateJob(t *testing.T) {
...
@@ -918,7 +918,7 @@ func TestValidateJob(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
else
{
}
else
{
s
:=
strings
.
Split
(
k
,
":"
)
s
:=
strings
.
Split
(
k
,
":"
)
err
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
err
:=
errs
[
0
]
.
(
*
validation
.
Error
)
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
}
}
...
@@ -1019,7 +1019,7 @@ func TestValidateIngress(t *testing.T) {
...
@@ -1019,7 +1019,7 @@ func TestValidateIngress(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
else
{
}
else
{
s
:=
strings
.
Split
(
k
,
":"
)
s
:=
strings
.
Split
(
k
,
":"
)
err
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
err
:=
errs
[
0
]
.
(
*
validation
.
Error
)
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
}
}
...
@@ -1111,7 +1111,7 @@ func TestValidateIngressStatusUpdate(t *testing.T) {
...
@@ -1111,7 +1111,7 @@ func TestValidateIngressStatusUpdate(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
else
{
}
else
{
s
:=
strings
.
Split
(
k
,
":"
)
s
:=
strings
.
Split
(
k
,
":"
)
err
:=
errs
[
0
]
.
(
*
validation
.
Validation
Error
)
err
:=
errs
[
0
]
.
(
*
validation
.
Error
)
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
if
err
.
Field
!=
s
[
0
]
||
!
strings
.
Contains
(
err
.
Error
(),
s
[
1
])
{
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
t
.
Errorf
(
"unexpected error: %v, expected: %s"
,
errs
[
0
],
k
)
}
}
...
...
pkg/util/validation/errors.go
View file @
d64d1fbb
...
@@ -78,21 +78,21 @@ func (t ErrorType) String() string {
...
@@ -78,21 +78,21 @@ func (t ErrorType) String() string {
}
}
}
}
//
Validation
Error is an implementation of the 'error' interface, which represents an error of validation.
// Error is an implementation of the 'error' interface, which represents an error of validation.
type
Validation
Error
struct
{
type
Error
struct
{
Type
ErrorType
Type
ErrorType
Field
string
Field
string
BadValue
interface
{}
BadValue
interface
{}
Detail
string
Detail
string
}
}
var
_
error
=
&
Validation
Error
{}
var
_
error
=
&
Error
{}
func
(
v
*
Validation
Error
)
Error
()
string
{
func
(
v
*
Error
)
Error
()
string
{
return
fmt
.
Sprintf
(
"%s: %s"
,
v
.
Field
,
v
.
ErrorBody
())
return
fmt
.
Sprintf
(
"%s: %s"
,
v
.
Field
,
v
.
ErrorBody
())
}
}
func
(
v
*
Validation
Error
)
ErrorBody
()
string
{
func
(
v
*
Error
)
ErrorBody
()
string
{
var
s
string
var
s
string
switch
v
.
Type
{
switch
v
.
Type
{
case
ErrorTypeRequired
,
ErrorTypeTooLong
:
case
ErrorTypeRequired
,
ErrorTypeTooLong
:
...
@@ -106,51 +106,51 @@ func (v *ValidationError) ErrorBody() string {
...
@@ -106,51 +106,51 @@ func (v *ValidationError) ErrorBody() string {
return
s
return
s
}
}
// NewFieldRequired returns a *
Validation
Error indicating "value required"
// NewFieldRequired returns a *Error indicating "value required"
func
NewFieldRequired
(
field
string
)
*
Validation
Error
{
func
NewFieldRequired
(
field
string
)
*
Error
{
return
&
Validation
Error
{
ErrorTypeRequired
,
field
,
""
,
""
}
return
&
Error
{
ErrorTypeRequired
,
field
,
""
,
""
}
}
}
// NewFieldInvalid returns a *
Validation
Error indicating "invalid value"
// NewFieldInvalid returns a *Error indicating "invalid value"
func
NewFieldInvalid
(
field
string
,
value
interface
{},
detail
string
)
*
Validation
Error
{
func
NewFieldInvalid
(
field
string
,
value
interface
{},
detail
string
)
*
Error
{
return
&
Validation
Error
{
ErrorTypeInvalid
,
field
,
value
,
detail
}
return
&
Error
{
ErrorTypeInvalid
,
field
,
value
,
detail
}
}
}
// NewFieldValueNotSupported returns a *
Validation
Error indicating "unsupported value"
// NewFieldValueNotSupported returns a *Error indicating "unsupported value"
func
NewFieldValueNotSupported
(
field
string
,
value
interface
{},
validValues
[]
string
)
*
Validation
Error
{
func
NewFieldValueNotSupported
(
field
string
,
value
interface
{},
validValues
[]
string
)
*
Error
{
detail
:=
""
detail
:=
""
if
validValues
!=
nil
&&
len
(
validValues
)
>
0
{
if
validValues
!=
nil
&&
len
(
validValues
)
>
0
{
detail
=
"supported values: "
+
strings
.
Join
(
validValues
,
", "
)
detail
=
"supported values: "
+
strings
.
Join
(
validValues
,
", "
)
}
}
return
&
Validation
Error
{
ErrorTypeNotSupported
,
field
,
value
,
detail
}
return
&
Error
{
ErrorTypeNotSupported
,
field
,
value
,
detail
}
}
}
// NewFieldForbidden returns a *
Validation
Error indicating "forbidden"
// NewFieldForbidden returns a *Error indicating "forbidden"
func
NewFieldForbidden
(
field
string
,
value
interface
{})
*
Validation
Error
{
func
NewFieldForbidden
(
field
string
,
value
interface
{})
*
Error
{
return
&
Validation
Error
{
ErrorTypeForbidden
,
field
,
value
,
""
}
return
&
Error
{
ErrorTypeForbidden
,
field
,
value
,
""
}
}
}
// NewFieldDuplicate returns a *
Validation
Error indicating "duplicate value"
// NewFieldDuplicate returns a *Error indicating "duplicate value"
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
*
Validation
Error
{
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
*
Error
{
return
&
Validation
Error
{
ErrorTypeDuplicate
,
field
,
value
,
""
}
return
&
Error
{
ErrorTypeDuplicate
,
field
,
value
,
""
}
}
}
// NewFieldNotFound returns a *
Validation
Error indicating "value not found"
// NewFieldNotFound returns a *Error indicating "value not found"
func
NewFieldNotFound
(
field
string
,
value
interface
{})
*
Validation
Error
{
func
NewFieldNotFound
(
field
string
,
value
interface
{})
*
Error
{
return
&
Validation
Error
{
ErrorTypeNotFound
,
field
,
value
,
""
}
return
&
Error
{
ErrorTypeNotFound
,
field
,
value
,
""
}
}
}
func
NewFieldTooLong
(
field
string
,
value
interface
{},
maxLength
int
)
*
Validation
Error
{
func
NewFieldTooLong
(
field
string
,
value
interface
{},
maxLength
int
)
*
Error
{
return
&
Validation
Error
{
ErrorTypeTooLong
,
field
,
value
,
fmt
.
Sprintf
(
"must have at most %d characters"
,
maxLength
)}
return
&
Error
{
ErrorTypeTooLong
,
field
,
value
,
fmt
.
Sprintf
(
"must have at most %d characters"
,
maxLength
)}
}
}
type
ValidationErrorList
[]
error
type
ValidationErrorList
[]
error
// Prefix adds a prefix to the Field of every
Validation
Error in the list.
// Prefix adds a prefix to the Field of every Error in the list.
// Returns the list for convenience.
// Returns the list for convenience.
func
(
list
ValidationErrorList
)
Prefix
(
prefix
string
)
ValidationErrorList
{
func
(
list
ValidationErrorList
)
Prefix
(
prefix
string
)
ValidationErrorList
{
for
i
:=
range
list
{
for
i
:=
range
list
{
if
err
,
ok
:=
list
[
i
]
.
(
*
Validation
Error
);
ok
{
if
err
,
ok
:=
list
[
i
]
.
(
*
Error
);
ok
{
if
strings
.
HasPrefix
(
err
.
Field
,
"["
)
{
if
strings
.
HasPrefix
(
err
.
Field
,
"["
)
{
err
.
Field
=
prefix
+
err
.
Field
err
.
Field
=
prefix
+
err
.
Field
}
else
if
len
(
err
.
Field
)
!=
0
{
}
else
if
len
(
err
.
Field
)
!=
0
{
...
@@ -160,23 +160,23 @@ func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
...
@@ -160,23 +160,23 @@ func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
}
}
list
[
i
]
=
err
list
[
i
]
=
err
}
else
{
}
else
{
panic
(
fmt
.
Sprintf
(
"Programmer error: ValidationErrorList holds non-
Validation
Error: %#v"
,
list
[
i
]))
panic
(
fmt
.
Sprintf
(
"Programmer error: ValidationErrorList holds non-Error: %#v"
,
list
[
i
]))
}
}
}
}
return
list
return
list
}
}
// PrefixIndex adds an index to the Field of every
Validation
Error in the list.
// PrefixIndex adds an index to the Field of every Error in the list.
// Returns the list for convenience.
// Returns the list for convenience.
func
(
list
ValidationErrorList
)
PrefixIndex
(
index
int
)
ValidationErrorList
{
func
(
list
ValidationErrorList
)
PrefixIndex
(
index
int
)
ValidationErrorList
{
return
list
.
Prefix
(
fmt
.
Sprintf
(
"[%d]"
,
index
))
return
list
.
Prefix
(
fmt
.
Sprintf
(
"[%d]"
,
index
))
}
}
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// if the provided error is a
Validation
Error and has the provided ErrorType.
// if the provided error is a Error and has the provided ErrorType.
func
NewErrorTypeMatcher
(
t
ErrorType
)
utilerrors
.
Matcher
{
func
NewErrorTypeMatcher
(
t
ErrorType
)
utilerrors
.
Matcher
{
return
func
(
err
error
)
bool
{
return
func
(
err
error
)
bool
{
if
e
,
ok
:=
err
.
(
*
Validation
Error
);
ok
{
if
e
,
ok
:=
err
.
(
*
Error
);
ok
{
return
e
.
Type
==
t
return
e
.
Type
==
t
}
}
return
false
return
false
...
@@ -184,11 +184,11 @@ func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher {
...
@@ -184,11 +184,11 @@ func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher {
}
}
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// if the provided error is a
Validation
Error and has a field with the provided
// if the provided error is a Error and has a field with the provided
// prefix.
// prefix.
func
NewValidationErrorFieldPrefixMatcher
(
prefix
string
)
utilerrors
.
Matcher
{
func
NewValidationErrorFieldPrefixMatcher
(
prefix
string
)
utilerrors
.
Matcher
{
return
func
(
err
error
)
bool
{
return
func
(
err
error
)
bool
{
if
e
,
ok
:=
err
.
(
*
Validation
Error
);
ok
{
if
e
,
ok
:=
err
.
(
*
Error
);
ok
{
return
strings
.
HasPrefix
(
e
.
Field
,
prefix
)
return
strings
.
HasPrefix
(
e
.
Field
,
prefix
)
}
}
return
false
return
false
...
...
pkg/util/validation/errors_test.go
View file @
d64d1fbb
...
@@ -23,27 +23,27 @@ import (
...
@@ -23,27 +23,27 @@ import (
func
TestMakeFuncs
(
t
*
testing
.
T
)
{
func
TestMakeFuncs
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
fn
func
()
*
Validation
Error
fn
func
()
*
Error
expected
ErrorType
expected
ErrorType
}{
}{
{
{
func
()
*
Validation
Error
{
return
NewFieldInvalid
(
"f"
,
"v"
,
"d"
)
},
func
()
*
Error
{
return
NewFieldInvalid
(
"f"
,
"v"
,
"d"
)
},
ErrorTypeInvalid
,
ErrorTypeInvalid
,
},
},
{
{
func
()
*
Validation
Error
{
return
NewFieldValueNotSupported
(
"f"
,
"v"
,
nil
)
},
func
()
*
Error
{
return
NewFieldValueNotSupported
(
"f"
,
"v"
,
nil
)
},
ErrorTypeNotSupported
,
ErrorTypeNotSupported
,
},
},
{
{
func
()
*
Validation
Error
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
func
()
*
Error
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
ErrorTypeDuplicate
,
ErrorTypeDuplicate
,
},
},
{
{
func
()
*
Validation
Error
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
func
()
*
Error
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
ErrorTypeNotFound
,
ErrorTypeNotFound
,
},
},
{
{
func
()
*
Validation
Error
{
return
NewFieldRequired
(
"f"
)
},
func
()
*
Error
{
return
NewFieldRequired
(
"f"
)
},
ErrorTypeRequired
,
ErrorTypeRequired
,
},
},
}
}
...
@@ -56,7 +56,7 @@ func TestMakeFuncs(t *testing.T) {
...
@@ -56,7 +56,7 @@ func TestMakeFuncs(t *testing.T) {
}
}
}
}
func
Test
Validation
ErrorUsefulMessage
(
t
*
testing
.
T
)
{
func
TestErrorUsefulMessage
(
t
*
testing
.
T
)
{
s
:=
NewFieldInvalid
(
"foo"
,
"bar"
,
"deet"
)
.
Error
()
s
:=
NewFieldInvalid
(
"foo"
,
"bar"
,
"deet"
)
.
Error
()
t
.
Logf
(
"message: %v"
,
s
)
t
.
Logf
(
"message: %v"
,
s
)
for
_
,
part
:=
range
[]
string
{
"foo"
,
"bar"
,
"deet"
,
ErrorTypeInvalid
.
String
()}
{
for
_
,
part
:=
range
[]
string
{
"foo"
,
"bar"
,
"deet"
,
ErrorTypeInvalid
.
String
()}
{
...
@@ -121,7 +121,7 @@ func TestErrListFilter(t *testing.T) {
...
@@ -121,7 +121,7 @@ func TestErrListFilter(t *testing.T) {
func
TestErrListPrefix
(
t
*
testing
.
T
)
{
func
TestErrListPrefix
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
Err
*
Validation
Error
Err
*
Error
Expected
string
Expected
string
}{
}{
{
{
...
@@ -143,7 +143,7 @@ func TestErrListPrefix(t *testing.T) {
...
@@ -143,7 +143,7 @@ func TestErrListPrefix(t *testing.T) {
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
t
.
Errorf
(
"Prefix should return self"
)
t
.
Errorf
(
"Prefix should return self"
)
}
}
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
Validation
Error
)
.
Field
;
e
!=
a
{
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
Error
)
.
Field
;
e
!=
a
{
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
}
}
}
}
...
@@ -151,7 +151,7 @@ func TestErrListPrefix(t *testing.T) {
...
@@ -151,7 +151,7 @@ func TestErrListPrefix(t *testing.T) {
func
TestErrListPrefixIndex
(
t
*
testing
.
T
)
{
func
TestErrListPrefixIndex
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
Err
*
Validation
Error
Err
*
Error
Expected
string
Expected
string
}{
}{
{
{
...
@@ -173,7 +173,7 @@ func TestErrListPrefixIndex(t *testing.T) {
...
@@ -173,7 +173,7 @@ func TestErrListPrefixIndex(t *testing.T) {
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
t
.
Errorf
(
"PrefixIndex should return self"
)
t
.
Errorf
(
"PrefixIndex should return self"
)
}
}
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
Validation
Error
)
.
Field
;
e
!=
a
{
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
Error
)
.
Field
;
e
!=
a
{
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment