Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make proto validation errors public #17

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pkg/util/proto/validation/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,48 @@ func (e *errors) AppendErrors(err ...error) {
e.errors = append(e.errors, err...)
}

type validationError struct {
type ValidationError struct {
Path string
Err error
}

func (e validationError) Error() string {
return fmt.Sprintf("validationError(%s): %v", e.Path, e.Err)
func (e ValidationError) Error() string {
return fmt.Sprintf("ValidationError(%s): %v", e.Path, e.Err)
}

type invalidTypeError struct {
type InvalidTypeError struct {
Path string
Expected string
Actual string
}

func (e invalidTypeError) Error() string {
func (e InvalidTypeError) Error() string {
return fmt.Sprintf("invalid type for %s: got %q, expected %q", e.Path, e.Actual, e.Expected)
}

type missingRequiredFieldError struct {
type MissingRequiredFieldError struct {
Path string
Field string
}

func (e missingRequiredFieldError) Error() string {
func (e MissingRequiredFieldError) Error() string {
return fmt.Sprintf("missing required field %q in %s", e.Field, e.Path)
}

type unknownFieldError struct {
type UnknownFieldError struct {
Path string
Field string
}

func (e unknownFieldError) Error() string {
func (e UnknownFieldError) Error() string {
return fmt.Sprintf("unknown field %q in %s", e.Field, e.Path)
}

type invalidObjectTypeError struct {
type InvalidObjectTypeError struct {
Path string
Type string
}

func (e invalidObjectTypeError) Error() string {
func (e InvalidObjectTypeError) Error() string {
return fmt.Sprintf("unknown object type %q in %s", e.Type, e.Path)
}
30 changes: 15 additions & 15 deletions pkg/util/proto/validation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (item *baseItem) Errors() []error {
// AddValidationError wraps the given error into a ValidationError and
// attaches it to this item.
func (item *baseItem) AddValidationError(err error) {
item.errors.AppendErrors(validationError{Path: item.path.String(), Err: err})
item.errors.AppendErrors(ValidationError{Path: item.path.String(), Err: err})
}

// AddError adds a regular (non-validation related) error to the list.
Expand Down Expand Up @@ -81,11 +81,11 @@ func (item *mapItem) sortedKeys() []string {
var _ validationItem = &mapItem{}

func (item *mapItem) VisitPrimitive(schema *proto.Primitive) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "map"})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "map"})
}

func (item *mapItem) VisitArray(schema *proto.Array) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
}

func (item *mapItem) VisitMap(schema *proto.Map) {
Expand All @@ -112,7 +112,7 @@ func (item *mapItem) VisitKind(schema *proto.Kind) {
continue
}
if _, ok := schema.Fields[key]; !ok {
item.AddValidationError(unknownFieldError{Path: schema.GetPath().String(), Field: key})
item.AddValidationError(UnknownFieldError{Path: schema.GetPath().String(), Field: key})
continue
}
schema.Fields[key].Accept(subItem)
Expand All @@ -122,7 +122,7 @@ func (item *mapItem) VisitKind(schema *proto.Kind) {
// Verify that all required fields are present.
for _, required := range schema.RequiredFields {
if v, ok := item.Map[required]; !ok || v == nil {
item.AddValidationError(missingRequiredFieldError{Path: schema.GetPath().String(), Field: required})
item.AddValidationError(MissingRequiredFieldError{Path: schema.GetPath().String(), Field: required})
}
}
}
Expand All @@ -142,14 +142,14 @@ type arrayItem struct {
var _ validationItem = &arrayItem{}

func (item *arrayItem) VisitPrimitive(schema *proto.Primitive) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "array"})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "array"})
}

func (item *arrayItem) VisitArray(schema *proto.Array) {
for i, v := range item.Array {
path := item.Path().ArrayPath(i)
if v == nil {
item.AddValidationError(invalidObjectTypeError{Type: "nil", Path: path.String()})
item.AddValidationError(InvalidObjectTypeError{Type: "nil", Path: path.String()})
continue
}
subItem, err := itemFactory(path, v)
Expand All @@ -163,11 +163,11 @@ func (item *arrayItem) VisitArray(schema *proto.Array) {
}

func (item *arrayItem) VisitMap(schema *proto.Map) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
}

func (item *arrayItem) VisitKind(schema *proto.Kind) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
}

func (item *arrayItem) VisitReference(schema proto.Reference) {
Expand Down Expand Up @@ -211,19 +211,19 @@ func (item *primitiveItem) VisitPrimitive(schema *proto.Primitive) {
return
}

item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: item.Kind})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: item.Kind})
}

func (item *primitiveItem) VisitArray(schema *proto.Array) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: item.Kind})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: item.Kind})
}

func (item *primitiveItem) VisitMap(schema *proto.Map) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
}

func (item *primitiveItem) VisitKind(schema *proto.Kind) {
item.AddValidationError(invalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
}

func (item *primitiveItem) VisitReference(schema proto.Reference) {
Expand All @@ -235,7 +235,7 @@ func (item *primitiveItem) VisitReference(schema proto.Reference) {
func itemFactory(path proto.Path, v interface{}) (validationItem, error) {
// We need to special case for no-type fields in yaml (e.g. empty item in list)
if v == nil {
return nil, invalidObjectTypeError{Type: "nil", Path: path.String()}
return nil, InvalidObjectTypeError{Type: "nil", Path: path.String()}
}
kind := reflect.TypeOf(v).Kind()
switch kind {
Expand Down Expand Up @@ -285,5 +285,5 @@ func itemFactory(path proto.Path, v interface{}) (validationItem, error) {
Map: v.(map[string]interface{}),
}, nil
}
return nil, invalidObjectTypeError{Type: kind.String(), Path: path.String()}
return nil, InvalidObjectTypeError{Type: kind.String(), Path: path.String()}
}