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
2a127d08
Commit
2a127d08
authored
Nov 03, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
s/ValidationErrorType/ErrorType/
parent
0ff66da3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
47 deletions
+47
-47
validation_test.go
pkg/api/validation/validation_test.go
+0
-0
errors.go
pkg/util/validation/errors.go
+36
-36
errors_test.go
pkg/util/validation/errors_test.go
+11
-11
No files found.
pkg/api/validation/validation_test.go
View file @
2a127d08
This diff is collapsed.
Click to expand it.
pkg/util/validation/errors.go
View file @
2a127d08
...
@@ -25,52 +25,52 @@ import (
...
@@ -25,52 +25,52 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/davecgh/go-spew/spew"
)
)
//
Validation
ErrorType is a machine readable value providing more detail about why
// ErrorType is a machine readable value providing more detail about why
// a field is invalid. These values are expected to match 1-1 with
// a field is invalid. These values are expected to match 1-1 with
// CauseType in api/types.go.
// CauseType in api/types.go.
type
Validation
ErrorType
string
type
ErrorType
string
// TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
// TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
const
(
const
(
//
ValidationErrorTypeNotFound
is used to report failure to find a requested value
//
ErrorType
is used to report failure to find a requested value
// (e.g. looking up an ID).
// (e.g. looking up an ID).
ValidationErrorTypeNotFound
Validation
ErrorType
=
"FieldValueNotFound"
ErrorTypeNotFound
ErrorType
=
"FieldValueNotFound"
//
Validation
ErrorTypeRequired is used to report required values that are not
// ErrorTypeRequired is used to report required values that are not
// provided (e.g. empty strings, null values, or empty arrays).
// provided (e.g. empty strings, null values, or empty arrays).
ValidationErrorTypeRequired
Validation
ErrorType
=
"FieldValueRequired"
ErrorTypeRequired
ErrorType
=
"FieldValueRequired"
//
Validation
ErrorTypeDuplicate is used to report collisions of values that must be
// ErrorTypeDuplicate is used to report collisions of values that must be
// unique (e.g. unique IDs).
// unique (e.g. unique IDs).
ValidationErrorTypeDuplicate
Validation
ErrorType
=
"FieldValueDuplicate"
ErrorTypeDuplicate
ErrorType
=
"FieldValueDuplicate"
//
Validation
ErrorTypeInvalid is used to report malformed values (e.g. failed regex
// ErrorTypeInvalid is used to report malformed values (e.g. failed regex
// match).
// match).
ValidationErrorTypeInvalid
Validation
ErrorType
=
"FieldValueInvalid"
ErrorTypeInvalid
ErrorType
=
"FieldValueInvalid"
//
Validation
ErrorTypeNotSupported is used to report valid (as per formatting rules)
// ErrorTypeNotSupported is used to report valid (as per formatting rules)
// values that can not be handled (e.g. an enumerated string).
// values that can not be handled (e.g. an enumerated string).
ValidationErrorTypeNotSupported
Validation
ErrorType
=
"FieldValueNotSupported"
ErrorTypeNotSupported
ErrorType
=
"FieldValueNotSupported"
//
Validation
ErrorTypeForbidden is used to report valid (as per formatting rules)
// ErrorTypeForbidden is used to report valid (as per formatting rules)
// values which would be accepted by some api instances, but which would invoke behavior
// values which would be accepted by some api instances, but which would invoke behavior
// not permitted by this api instance (such as due to stricter security policy).
// not permitted by this api instance (such as due to stricter security policy).
ValidationErrorTypeForbidden
Validation
ErrorType
=
"FieldValueForbidden"
ErrorTypeForbidden
ErrorType
=
"FieldValueForbidden"
//
Validation
ErrorTypeTooLong is used to report that given value is too long.
// ErrorTypeTooLong is used to report that given value is too long.
ValidationErrorTypeTooLong
Validation
ErrorType
=
"FieldValueTooLong"
ErrorTypeTooLong
ErrorType
=
"FieldValueTooLong"
)
)
// String converts a
Validation
ErrorType into its corresponding error message.
// String converts a ErrorType into its corresponding error message.
func
(
t
Validation
ErrorType
)
String
()
string
{
func
(
t
ErrorType
)
String
()
string
{
switch
t
{
switch
t
{
case
Validation
ErrorTypeNotFound
:
case
ErrorTypeNotFound
:
return
"not found"
return
"not found"
case
Validation
ErrorTypeRequired
:
case
ErrorTypeRequired
:
return
"required value"
return
"required value"
case
Validation
ErrorTypeDuplicate
:
case
ErrorTypeDuplicate
:
return
"duplicate value"
return
"duplicate value"
case
Validation
ErrorTypeInvalid
:
case
ErrorTypeInvalid
:
return
"invalid value"
return
"invalid value"
case
Validation
ErrorTypeNotSupported
:
case
ErrorTypeNotSupported
:
return
"unsupported value"
return
"unsupported value"
case
Validation
ErrorTypeForbidden
:
case
ErrorTypeForbidden
:
return
"forbidden"
return
"forbidden"
case
Validation
ErrorTypeTooLong
:
case
ErrorTypeTooLong
:
return
"too long"
return
"too long"
default
:
default
:
panic
(
fmt
.
Sprintf
(
"unrecognized validation type: %#v"
,
t
))
panic
(
fmt
.
Sprintf
(
"unrecognized validation type: %#v"
,
t
))
...
@@ -80,7 +80,7 @@ func (t ValidationErrorType) String() string {
...
@@ -80,7 +80,7 @@ func (t ValidationErrorType) String() string {
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
type
ValidationError
struct
{
type
ValidationError
struct
{
Type
Validation
ErrorType
Type
ErrorType
Field
string
Field
string
BadValue
interface
{}
BadValue
interface
{}
Detail
string
Detail
string
...
@@ -95,7 +95,7 @@ func (v *ValidationError) Error() string {
...
@@ -95,7 +95,7 @@ func (v *ValidationError) Error() string {
func
(
v
*
ValidationError
)
ErrorBody
()
string
{
func
(
v
*
ValidationError
)
ErrorBody
()
string
{
var
s
string
var
s
string
switch
v
.
Type
{
switch
v
.
Type
{
case
ValidationErrorTypeRequired
,
Validation
ErrorTypeTooLong
:
case
ErrorTypeRequired
,
ErrorTypeTooLong
:
s
=
spew
.
Sprintf
(
"%s"
,
v
.
Type
)
s
=
spew
.
Sprintf
(
"%s"
,
v
.
Type
)
default
:
default
:
s
=
spew
.
Sprintf
(
"%s '%+v'"
,
v
.
Type
,
v
.
BadValue
)
s
=
spew
.
Sprintf
(
"%s '%+v'"
,
v
.
Type
,
v
.
BadValue
)
...
@@ -108,12 +108,12 @@ func (v *ValidationError) ErrorBody() string {
...
@@ -108,12 +108,12 @@ func (v *ValidationError) ErrorBody() string {
// NewFieldRequired returns a *ValidationError indicating "value required"
// NewFieldRequired returns a *ValidationError indicating "value required"
func
NewFieldRequired
(
field
string
)
*
ValidationError
{
func
NewFieldRequired
(
field
string
)
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeRequired
,
field
,
""
,
""
}
return
&
ValidationError
{
ErrorTypeRequired
,
field
,
""
,
""
}
}
}
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
func
NewFieldInvalid
(
field
string
,
value
interface
{},
detail
string
)
*
ValidationError
{
func
NewFieldInvalid
(
field
string
,
value
interface
{},
detail
string
)
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeInvalid
,
field
,
value
,
detail
}
return
&
ValidationError
{
ErrorTypeInvalid
,
field
,
value
,
detail
}
}
}
// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value"
// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value"
...
@@ -122,26 +122,26 @@ func NewFieldValueNotSupported(field string, value interface{}, validValues []st
...
@@ -122,26 +122,26 @@ func NewFieldValueNotSupported(field string, value interface{}, validValues []st
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
&
ValidationError
{
Validation
ErrorTypeNotSupported
,
field
,
value
,
detail
}
return
&
ValidationError
{
ErrorTypeNotSupported
,
field
,
value
,
detail
}
}
}
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
func
NewFieldForbidden
(
field
string
,
value
interface
{})
*
ValidationError
{
func
NewFieldForbidden
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeForbidden
,
field
,
value
,
""
}
return
&
ValidationError
{
ErrorTypeForbidden
,
field
,
value
,
""
}
}
}
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
*
ValidationError
{
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeDuplicate
,
field
,
value
,
""
}
return
&
ValidationError
{
ErrorTypeDuplicate
,
field
,
value
,
""
}
}
}
// NewFieldNotFound returns a *ValidationError indicating "value not found"
// NewFieldNotFound returns a *ValidationError indicating "value not found"
func
NewFieldNotFound
(
field
string
,
value
interface
{})
*
ValidationError
{
func
NewFieldNotFound
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeNotFound
,
field
,
value
,
""
}
return
&
ValidationError
{
ErrorTypeNotFound
,
field
,
value
,
""
}
}
}
func
NewFieldTooLong
(
field
string
,
value
interface
{},
maxLength
int
)
*
ValidationError
{
func
NewFieldTooLong
(
field
string
,
value
interface
{},
maxLength
int
)
*
ValidationError
{
return
&
ValidationError
{
Validation
ErrorTypeTooLong
,
field
,
value
,
fmt
.
Sprintf
(
"must have at most %d characters"
,
maxLength
)}
return
&
ValidationError
{
ErrorTypeTooLong
,
field
,
value
,
fmt
.
Sprintf
(
"must have at most %d characters"
,
maxLength
)}
}
}
type
ValidationErrorList
[]
error
type
ValidationErrorList
[]
error
...
@@ -173,8 +173,8 @@ func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
...
@@ -173,8 +173,8 @@ func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
}
}
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
// if the provided error is a ValidationError and has the provided
Validation
ErrorType.
// if the provided error is a ValidationError and has the provided ErrorType.
func
New
ValidationErrorTypeMatcher
(
t
Validation
ErrorType
)
utilerrors
.
Matcher
{
func
New
ErrorTypeMatcher
(
t
ErrorType
)
utilerrors
.
Matcher
{
return
func
(
err
error
)
bool
{
return
func
(
err
error
)
bool
{
if
e
,
ok
:=
err
.
(
*
ValidationError
);
ok
{
if
e
,
ok
:=
err
.
(
*
ValidationError
);
ok
{
return
e
.
Type
==
t
return
e
.
Type
==
t
...
...
pkg/util/validation/errors_test.go
View file @
2a127d08
...
@@ -24,27 +24,27 @@ import (
...
@@ -24,27 +24,27 @@ import (
func
TestMakeFuncs
(
t
*
testing
.
T
)
{
func
TestMakeFuncs
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
fn
func
()
*
ValidationError
fn
func
()
*
ValidationError
expected
Validation
ErrorType
expected
ErrorType
}{
}{
{
{
func
()
*
ValidationError
{
return
NewFieldInvalid
(
"f"
,
"v"
,
"d"
)
},
func
()
*
ValidationError
{
return
NewFieldInvalid
(
"f"
,
"v"
,
"d"
)
},
Validation
ErrorTypeInvalid
,
ErrorTypeInvalid
,
},
},
{
{
func
()
*
ValidationError
{
return
NewFieldValueNotSupported
(
"f"
,
"v"
,
nil
)
},
func
()
*
ValidationError
{
return
NewFieldValueNotSupported
(
"f"
,
"v"
,
nil
)
},
Validation
ErrorTypeNotSupported
,
ErrorTypeNotSupported
,
},
},
{
{
func
()
*
ValidationError
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
Validation
ErrorTypeDuplicate
,
ErrorTypeDuplicate
,
},
},
{
{
func
()
*
ValidationError
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
Validation
ErrorTypeNotFound
,
ErrorTypeNotFound
,
},
},
{
{
func
()
*
ValidationError
{
return
NewFieldRequired
(
"f"
)
},
func
()
*
ValidationError
{
return
NewFieldRequired
(
"f"
)
},
Validation
ErrorTypeRequired
,
ErrorTypeRequired
,
},
},
}
}
...
@@ -59,7 +59,7 @@ func TestMakeFuncs(t *testing.T) {
...
@@ -59,7 +59,7 @@ func TestMakeFuncs(t *testing.T) {
func
TestValidationErrorUsefulMessage
(
t
*
testing
.
T
)
{
func
TestValidationErrorUsefulMessage
(
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"
,
Validation
ErrorTypeInvalid
.
String
()}
{
for
_
,
part
:=
range
[]
string
{
"foo"
,
"bar"
,
"deet"
,
ErrorTypeInvalid
.
String
()}
{
if
!
strings
.
Contains
(
s
,
part
)
{
if
!
strings
.
Contains
(
s
,
part
)
{
t
.
Errorf
(
"error message did not contain expected part '%v'"
,
part
)
t
.
Errorf
(
"error message did not contain expected part '%v'"
,
part
)
}
}
...
@@ -83,7 +83,7 @@ func TestValidationErrorUsefulMessage(t *testing.T) {
...
@@ -83,7 +83,7 @@ func TestValidationErrorUsefulMessage(t *testing.T) {
)
.
Error
()
)
.
Error
()
t
.
Logf
(
"message: %v"
,
s
)
t
.
Logf
(
"message: %v"
,
s
)
for
_
,
part
:=
range
[]
string
{
for
_
,
part
:=
range
[]
string
{
"foo"
,
Validation
ErrorTypeInvalid
.
String
(),
"foo"
,
ErrorTypeInvalid
.
String
(),
"Baz"
,
"Qux"
,
"Inner"
,
"KV"
,
"detail"
,
"Baz"
,
"Qux"
,
"Inner"
,
"KV"
,
"detail"
,
"1"
,
"aoeu"
,
"asdf"
,
"Billy"
,
"2"
,
"1"
,
"aoeu"
,
"asdf"
,
"Billy"
,
"2"
,
}
{
}
{
...
@@ -99,10 +99,10 @@ func TestErrListFilter(t *testing.T) {
...
@@ -99,10 +99,10 @@ func TestErrListFilter(t *testing.T) {
NewFieldInvalid
(
"field.test"
,
""
,
""
),
NewFieldInvalid
(
"field.test"
,
""
,
""
),
NewFieldDuplicate
(
"test"
,
"value"
),
NewFieldDuplicate
(
"test"
,
"value"
),
}
}
if
len
(
list
.
Filter
(
New
ValidationErrorTypeMatcher
(
Validation
ErrorTypeDuplicate
)))
!=
2
{
if
len
(
list
.
Filter
(
New
ErrorTypeMatcher
(
ErrorTypeDuplicate
)))
!=
2
{
t
.
Errorf
(
"should not filter"
)
t
.
Errorf
(
"should not filter"
)
}
}
if
len
(
list
.
Filter
(
New
ValidationErrorTypeMatcher
(
Validation
ErrorTypeInvalid
)))
!=
1
{
if
len
(
list
.
Filter
(
New
ErrorTypeMatcher
(
ErrorTypeInvalid
)))
!=
1
{
t
.
Errorf
(
"should filter"
)
t
.
Errorf
(
"should filter"
)
}
}
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
"test"
)))
!=
1
{
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
"test"
)))
!=
1
{
...
@@ -114,7 +114,7 @@ func TestErrListFilter(t *testing.T) {
...
@@ -114,7 +114,7 @@ func TestErrListFilter(t *testing.T) {
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
""
)))
!=
0
{
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
""
)))
!=
0
{
t
.
Errorf
(
"should filter"
)
t
.
Errorf
(
"should filter"
)
}
}
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
"field."
),
New
ValidationErrorTypeMatcher
(
Validation
ErrorTypeDuplicate
)))
!=
1
{
if
len
(
list
.
Filter
(
NewValidationErrorFieldPrefixMatcher
(
"field."
),
New
ErrorTypeMatcher
(
ErrorTypeDuplicate
)))
!=
1
{
t
.
Errorf
(
"should filter"
)
t
.
Errorf
(
"should filter"
)
}
}
}
}
...
...
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