7 "github.com/aws/aws-sdk-go/aws/awserr"
11 // InvalidParameterErrCode is the error code for invalid parameters errors
12 InvalidParameterErrCode = "InvalidParameter"
13 // ParamRequiredErrCode is the error code for required parameter errors
14 ParamRequiredErrCode = "ParamRequiredError"
15 // ParamMinValueErrCode is the error code for fields with too low of a
17 ParamMinValueErrCode = "ParamMinValueError"
18 // ParamMinLenErrCode is the error code for fields without enough elements.
19 ParamMinLenErrCode = "ParamMinLenError"
22 // Validator provides a way for types to perform validation logic on their
23 // input values that external code can use to determine if a type's values
25 type Validator interface {
29 // An ErrInvalidParams provides wrapping of invalid parameter errors found when
30 // validating API operation input parameters.
31 type ErrInvalidParams struct {
32 // Context is the base context of the invalid parameter group.
34 errs []ErrInvalidParam
37 // Add adds a new invalid parameter error to the collection of invalid
38 // parameters. The context of the invalid parameter will be updated to reflect
40 func (e *ErrInvalidParams) Add(err ErrInvalidParam) {
41 err.SetContext(e.Context)
42 e.errs = append(e.errs, err)
45 // AddNested adds the invalid parameter errors from another ErrInvalidParams
46 // value into this collection. The nested errors will have their nested context
47 // updated and base context to reflect the merging.
49 // Use for nested validations errors.
50 func (e *ErrInvalidParams) AddNested(nestedCtx string, nested ErrInvalidParams) {
51 for _, err := range nested.errs {
52 err.SetContext(e.Context)
53 err.AddNestedContext(nestedCtx)
54 e.errs = append(e.errs, err)
58 // Len returns the number of invalid parameter errors
59 func (e ErrInvalidParams) Len() int {
63 // Code returns the code of the error
64 func (e ErrInvalidParams) Code() string {
65 return InvalidParameterErrCode
68 // Message returns the message of the error
69 func (e ErrInvalidParams) Message() string {
70 return fmt.Sprintf("%d validation error(s) found.", len(e.errs))
73 // Error returns the string formatted form of the invalid parameters.
74 func (e ErrInvalidParams) Error() string {
76 fmt.Fprintf(w, "%s: %s\n", e.Code(), e.Message())
78 for _, err := range e.errs {
79 fmt.Fprintf(w, "- %s\n", err.Message())
85 // OrigErr returns the invalid parameters as a awserr.BatchedErrors value
86 func (e ErrInvalidParams) OrigErr() error {
87 return awserr.NewBatchError(
88 InvalidParameterErrCode, e.Message(), e.OrigErrs())
91 // OrigErrs returns a slice of the invalid parameters
92 func (e ErrInvalidParams) OrigErrs() []error {
93 errs := make([]error, len(e.errs))
94 for i := 0; i < len(errs); i++ {
101 // An ErrInvalidParam represents an invalid parameter error type.
102 type ErrInvalidParam interface {
105 // Field name the error occurred on.
108 // SetContext updates the context of the error.
111 // AddNestedContext updates the error's context to include a nested level.
112 AddNestedContext(string)
115 type errInvalidParam struct {
123 // Code returns the error code for the type of invalid parameter.
124 func (e *errInvalidParam) Code() string {
128 // Message returns the reason the parameter was invalid, and its context.
129 func (e *errInvalidParam) Message() string {
130 return fmt.Sprintf("%s, %s.", e.msg, e.Field())
133 // Error returns the string version of the invalid parameter error.
134 func (e *errInvalidParam) Error() string {
135 return fmt.Sprintf("%s: %s", e.code, e.Message())
138 // OrigErr returns nil, Implemented for awserr.Error interface.
139 func (e *errInvalidParam) OrigErr() error {
143 // Field Returns the field and context the error occurred.
144 func (e *errInvalidParam) Field() string {
149 if len(e.nestedContext) > 0 {
150 field += fmt.Sprintf("%s.", e.nestedContext)
157 // SetContext updates the base context of the error.
158 func (e *errInvalidParam) SetContext(ctx string) {
162 // AddNestedContext prepends a context to the field's path.
163 func (e *errInvalidParam) AddNestedContext(ctx string) {
164 if len(e.nestedContext) == 0 {
165 e.nestedContext = ctx
167 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
172 // An ErrParamRequired represents an required parameter error.
173 type ErrParamRequired struct {
177 // NewErrParamRequired creates a new required parameter error.
178 func NewErrParamRequired(field string) *ErrParamRequired {
179 return &ErrParamRequired{
181 code: ParamRequiredErrCode,
183 msg: fmt.Sprintf("missing required field"),
188 // An ErrParamMinValue represents a minimum value parameter error.
189 type ErrParamMinValue struct {
194 // NewErrParamMinValue creates a new minimum value parameter error.
195 func NewErrParamMinValue(field string, min float64) *ErrParamMinValue {
196 return &ErrParamMinValue{
197 errInvalidParam: errInvalidParam{
198 code: ParamMinValueErrCode,
200 msg: fmt.Sprintf("minimum field value of %v", min),
206 // MinValue returns the field's require minimum value.
208 // float64 is returned for both int and float min values.
209 func (e *ErrParamMinValue) MinValue() float64 {
213 // An ErrParamMinLen represents a minimum length parameter error.
214 type ErrParamMinLen struct {
219 // NewErrParamMinLen creates a new minimum length parameter error.
220 func NewErrParamMinLen(field string, min int) *ErrParamMinLen {
221 return &ErrParamMinLen{
222 errInvalidParam: errInvalidParam{
223 code: ParamMinValueErrCode,
225 msg: fmt.Sprintf("minimum field size of %v", min),
231 // MinLen returns the field's required minimum length.
232 func (e *ErrParamMinLen) MinLen() int {