aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/aws/aws-sdk-go/aws/request/validation.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/aws/request/validation.go')
-rw-r--r--vendor/github.com/aws/aws-sdk-go/aws/request/validation.go234
1 files changed, 234 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/aws/request/validation.go b/vendor/github.com/aws/aws-sdk-go/aws/request/validation.go
new file mode 100644
index 0000000..2520286
--- /dev/null
+++ b/vendor/github.com/aws/aws-sdk-go/aws/request/validation.go
@@ -0,0 +1,234 @@
1package request
2
3import (
4 "bytes"
5 "fmt"
6
7 "github.com/aws/aws-sdk-go/aws/awserr"
8)
9
10const (
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
16 // number value.
17 ParamMinValueErrCode = "ParamMinValueError"
18 // ParamMinLenErrCode is the error code for fields without enough elements.
19 ParamMinLenErrCode = "ParamMinLenError"
20)
21
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
24// are valid.
25type Validator interface {
26 Validate() error
27}
28
29// An ErrInvalidParams provides wrapping of invalid parameter errors found when
30// validating API operation input parameters.
31type ErrInvalidParams struct {
32 // Context is the base context of the invalid parameter group.
33 Context string
34 errs []ErrInvalidParam
35}
36
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
39// this collection.
40func (e *ErrInvalidParams) Add(err ErrInvalidParam) {
41 err.SetContext(e.Context)
42 e.errs = append(e.errs, err)
43}
44
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.
48//
49// Use for nested validations errors.
50func (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)
55 }
56}
57
58// Len returns the number of invalid parameter errors
59func (e ErrInvalidParams) Len() int {
60 return len(e.errs)
61}
62
63// Code returns the code of the error
64func (e ErrInvalidParams) Code() string {
65 return InvalidParameterErrCode
66}
67
68// Message returns the message of the error
69func (e ErrInvalidParams) Message() string {
70 return fmt.Sprintf("%d validation error(s) found.", len(e.errs))
71}
72
73// Error returns the string formatted form of the invalid parameters.
74func (e ErrInvalidParams) Error() string {
75 w := &bytes.Buffer{}
76 fmt.Fprintf(w, "%s: %s\n", e.Code(), e.Message())
77
78 for _, err := range e.errs {
79 fmt.Fprintf(w, "- %s\n", err.Message())
80 }
81
82 return w.String()
83}
84
85// OrigErr returns the invalid parameters as a awserr.BatchedErrors value
86func (e ErrInvalidParams) OrigErr() error {
87 return awserr.NewBatchError(
88 InvalidParameterErrCode, e.Message(), e.OrigErrs())
89}
90
91// OrigErrs returns a slice of the invalid parameters
92func (e ErrInvalidParams) OrigErrs() []error {
93 errs := make([]error, len(e.errs))
94 for i := 0; i < len(errs); i++ {
95 errs[i] = e.errs[i]
96 }
97
98 return errs
99}
100
101// An ErrInvalidParam represents an invalid parameter error type.
102type ErrInvalidParam interface {
103 awserr.Error
104
105 // Field name the error occurred on.
106 Field() string
107
108 // SetContext updates the context of the error.
109 SetContext(string)
110
111 // AddNestedContext updates the error's context to include a nested level.
112 AddNestedContext(string)
113}
114
115type errInvalidParam struct {
116 context string
117 nestedContext string
118 field string
119 code string
120 msg string
121}
122
123// Code returns the error code for the type of invalid parameter.
124func (e *errInvalidParam) Code() string {
125 return e.code
126}
127
128// Message returns the reason the parameter was invalid, and its context.
129func (e *errInvalidParam) Message() string {
130 return fmt.Sprintf("%s, %s.", e.msg, e.Field())
131}
132
133// Error returns the string version of the invalid parameter error.
134func (e *errInvalidParam) Error() string {
135 return fmt.Sprintf("%s: %s", e.code, e.Message())
136}
137
138// OrigErr returns nil, Implemented for awserr.Error interface.
139func (e *errInvalidParam) OrigErr() error {
140 return nil
141}
142
143// Field Returns the field and context the error occurred.
144func (e *errInvalidParam) Field() string {
145 field := e.context
146 if len(field) > 0 {
147 field += "."
148 }
149 if len(e.nestedContext) > 0 {
150 field += fmt.Sprintf("%s.", e.nestedContext)
151 }
152 field += e.field
153
154 return field
155}
156
157// SetContext updates the base context of the error.
158func (e *errInvalidParam) SetContext(ctx string) {
159 e.context = ctx
160}
161
162// AddNestedContext prepends a context to the field's path.
163func (e *errInvalidParam) AddNestedContext(ctx string) {
164 if len(e.nestedContext) == 0 {
165 e.nestedContext = ctx
166 } else {
167 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
168 }
169
170}
171
172// An ErrParamRequired represents an required parameter error.
173type ErrParamRequired struct {
174 errInvalidParam
175}
176
177// NewErrParamRequired creates a new required parameter error.
178func NewErrParamRequired(field string) *ErrParamRequired {
179 return &ErrParamRequired{
180 errInvalidParam{
181 code: ParamRequiredErrCode,
182 field: field,
183 msg: fmt.Sprintf("missing required field"),
184 },
185 }
186}
187
188// An ErrParamMinValue represents a minimum value parameter error.
189type ErrParamMinValue struct {
190 errInvalidParam
191 min float64
192}
193
194// NewErrParamMinValue creates a new minimum value parameter error.
195func NewErrParamMinValue(field string, min float64) *ErrParamMinValue {
196 return &ErrParamMinValue{
197 errInvalidParam: errInvalidParam{
198 code: ParamMinValueErrCode,
199 field: field,
200 msg: fmt.Sprintf("minimum field value of %v", min),
201 },
202 min: min,
203 }
204}
205
206// MinValue returns the field's require minimum value.
207//
208// float64 is returned for both int and float min values.
209func (e *ErrParamMinValue) MinValue() float64 {
210 return e.min
211}
212
213// An ErrParamMinLen represents a minimum length parameter error.
214type ErrParamMinLen struct {
215 errInvalidParam
216 min int
217}
218
219// NewErrParamMinLen creates a new minimum length parameter error.
220func NewErrParamMinLen(field string, min int) *ErrParamMinLen {
221 return &ErrParamMinLen{
222 errInvalidParam: errInvalidParam{
223 code: ParamMinValueErrCode,
224 field: field,
225 msg: fmt.Sprintf("minimum field size of %v", min),
226 },
227 min: min,
228 }
229}
230
231// MinLen returns the field's required minimum length.
232func (e *ErrParamMinLen) MinLen() int {
233 return e.min
234}