Make the validation types match the pending ReasonCauses

This commit is contained in:
Clayton Coleman
2014-08-19 22:57:48 -04:00
parent 7332241c0d
commit 60126bfe64
3 changed files with 84 additions and 42 deletions

View File

@@ -19,53 +19,85 @@ package errors
import (
"fmt"
"strings"
"github.com/golang/glog"
)
// ValidationErrorEnum is a type of validation error.
type ValidationErrorEnum string
// ValidationErrorType is a machine readable value providing more detail about why
// a field is invalid. These values are expected to match 1-1 with
// CauseReasonType in api/types.go.
type ValidationErrorType string
// These are known errors of validation.
const (
Invalid ValidationErrorEnum = "invalid value"
NotSupported ValidationErrorEnum = "unsupported value"
Duplicate ValidationErrorEnum = "duplicate value"
NotFound ValidationErrorEnum = "not found"
// ValidationErrorTypeNotFound is used to report failure to find a requested value
// (e.g. looking up an ID).
ValidationErrorTypeNotFound ValidationErrorType = "fieldValueNotFound"
// ValidationErrorTypeRequired is used to report required values that are not
// provided (e.g. empty strings, null values, or empty arrays).
ValidationErrorTypeRequired ValidationErrorType = "fieldValueRequired"
// ValidationErrorTypeDuplicate is used to report collisions of values that must be
// unique (e.g. unique IDs).
ValidationErrorTypeDuplicate ValidationErrorType = "fieldValueDuplicate"
// ValidationErrorTypeInvalid is used to report malformed values (e.g. failed regex
// match).
ValidationErrorTypeInvalid ValidationErrorType = "fieldValueInvalid"
// ValidationErrorTypeNotSupported is used to report valid (as per formatting rules)
// values that can not be handled (e.g. an enumerated string).
ValidationErrorTypeNotSupported ValidationErrorType = "fieldValueNotSupported"
)
func ValueOf(t ValidationErrorType) string {
switch t {
case ValidationErrorTypeNotFound:
return "not found"
case ValidationErrorTypeRequired:
return "required value"
case ValidationErrorTypeDuplicate:
return "duplicate value"
case ValidationErrorTypeInvalid:
return "invalid value"
case ValidationErrorTypeNotSupported:
return "unsupported value"
default:
glog.Errorf("unrecognized validation type: %#v", t)
return ""
}
}
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
type ValidationError struct {
Type ValidationErrorEnum
Type ValidationErrorType
Field string
BadValue interface{}
}
func (v ValidationError) Error() string {
return fmt.Sprintf("%s: %v '%v'", v.Field, v.Type, v.BadValue)
return fmt.Sprintf("%s: %v '%v'", v.Field, ValueOf(v.Type), v.BadValue)
}
// NewInvalid returns a ValidationError indicating "invalid value". Use this to
// report malformed values (e.g. failed regex match) or missing "required" fields.
// NewInvalid returns a ValidationError indicating "value required"
func NewRequired(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeRequired, field, value}
}
// NewInvalid returns a ValidationError indicating "invalid value"
func NewInvalid(field string, value interface{}) ValidationError {
return ValidationError{Invalid, field, value}
return ValidationError{ValidationErrorTypeInvalid, field, value}
}
// NewNotSupported returns a ValidationError indicating "unsuported value". Use
// this to report valid (as per formatting rules) values that can not be handled
// (e.g. an enumerated string).
// NewNotSupported returns a ValidationError indicating "unsupported value"
func NewNotSupported(field string, value interface{}) ValidationError {
return ValidationError{NotSupported, field, value}
return ValidationError{ValidationErrorTypeNotSupported, field, value}
}
// NewDuplicate returns a ValidationError indicating "duplicate value". Use this
// to report collisions of values that must be unique (e.g. unique IDs).
// NewDuplicate returns a ValidationError indicating "duplicate value"
func NewDuplicate(field string, value interface{}) ValidationError {
return ValidationError{Duplicate, field, value}
return ValidationError{ValidationErrorTypeDuplicate, field, value}
}
// NewNotFound returns a ValidationError indicating "value not found". Use this
// to report failure to find a requested value (e.g. looking up an ID).
// NewNotFound returns a ValidationError indicating "value not found"
func NewNotFound(field string, value interface{}) ValidationError {
return ValidationError{NotFound, field, value}
return ValidationError{ValidationErrorTypeNotFound, field, value}
}
// ErrorList is a collection of errors. This does not implement the error