]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/aws/aws-sdk-go/aws/request/validation.go
8630683f3174b3a0a35e33b1610485f695d88452
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / aws / aws-sdk-go / aws / request / validation.go
1 package request
2
3 import (
4 "bytes"
5 "fmt"
6
7 "github.com/aws/aws-sdk-go/aws/awserr"
8 )
9
10 const (
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 // ParamMaxLenErrCode is the error code for value being too long.
21 ParamMaxLenErrCode = "ParamMaxLenError"
22
23 // ParamFormatErrCode is the error code for a field with invalid
24 // format or characters.
25 ParamFormatErrCode = "ParamFormatInvalidError"
26 )
27
28 // Validator provides a way for types to perform validation logic on their
29 // input values that external code can use to determine if a type's values
30 // are valid.
31 type Validator interface {
32 Validate() error
33 }
34
35 // An ErrInvalidParams provides wrapping of invalid parameter errors found when
36 // validating API operation input parameters.
37 type ErrInvalidParams struct {
38 // Context is the base context of the invalid parameter group.
39 Context string
40 errs []ErrInvalidParam
41 }
42
43 // Add adds a new invalid parameter error to the collection of invalid
44 // parameters. The context of the invalid parameter will be updated to reflect
45 // this collection.
46 func (e *ErrInvalidParams) Add(err ErrInvalidParam) {
47 err.SetContext(e.Context)
48 e.errs = append(e.errs, err)
49 }
50
51 // AddNested adds the invalid parameter errors from another ErrInvalidParams
52 // value into this collection. The nested errors will have their nested context
53 // updated and base context to reflect the merging.
54 //
55 // Use for nested validations errors.
56 func (e *ErrInvalidParams) AddNested(nestedCtx string, nested ErrInvalidParams) {
57 for _, err := range nested.errs {
58 err.SetContext(e.Context)
59 err.AddNestedContext(nestedCtx)
60 e.errs = append(e.errs, err)
61 }
62 }
63
64 // Len returns the number of invalid parameter errors
65 func (e ErrInvalidParams) Len() int {
66 return len(e.errs)
67 }
68
69 // Code returns the code of the error
70 func (e ErrInvalidParams) Code() string {
71 return InvalidParameterErrCode
72 }
73
74 // Message returns the message of the error
75 func (e ErrInvalidParams) Message() string {
76 return fmt.Sprintf("%d validation error(s) found.", len(e.errs))
77 }
78
79 // Error returns the string formatted form of the invalid parameters.
80 func (e ErrInvalidParams) Error() string {
81 w := &bytes.Buffer{}
82 fmt.Fprintf(w, "%s: %s\n", e.Code(), e.Message())
83
84 for _, err := range e.errs {
85 fmt.Fprintf(w, "- %s\n", err.Message())
86 }
87
88 return w.String()
89 }
90
91 // OrigErr returns the invalid parameters as a awserr.BatchedErrors value
92 func (e ErrInvalidParams) OrigErr() error {
93 return awserr.NewBatchError(
94 InvalidParameterErrCode, e.Message(), e.OrigErrs())
95 }
96
97 // OrigErrs returns a slice of the invalid parameters
98 func (e ErrInvalidParams) OrigErrs() []error {
99 errs := make([]error, len(e.errs))
100 for i := 0; i < len(errs); i++ {
101 errs[i] = e.errs[i]
102 }
103
104 return errs
105 }
106
107 // An ErrInvalidParam represents an invalid parameter error type.
108 type ErrInvalidParam interface {
109 awserr.Error
110
111 // Field name the error occurred on.
112 Field() string
113
114 // SetContext updates the context of the error.
115 SetContext(string)
116
117 // AddNestedContext updates the error's context to include a nested level.
118 AddNestedContext(string)
119 }
120
121 type errInvalidParam struct {
122 context string
123 nestedContext string
124 field string
125 code string
126 msg string
127 }
128
129 // Code returns the error code for the type of invalid parameter.
130 func (e *errInvalidParam) Code() string {
131 return e.code
132 }
133
134 // Message returns the reason the parameter was invalid, and its context.
135 func (e *errInvalidParam) Message() string {
136 return fmt.Sprintf("%s, %s.", e.msg, e.Field())
137 }
138
139 // Error returns the string version of the invalid parameter error.
140 func (e *errInvalidParam) Error() string {
141 return fmt.Sprintf("%s: %s", e.code, e.Message())
142 }
143
144 // OrigErr returns nil, Implemented for awserr.Error interface.
145 func (e *errInvalidParam) OrigErr() error {
146 return nil
147 }
148
149 // Field Returns the field and context the error occurred.
150 func (e *errInvalidParam) Field() string {
151 field := e.context
152 if len(field) > 0 {
153 field += "."
154 }
155 if len(e.nestedContext) > 0 {
156 field += fmt.Sprintf("%s.", e.nestedContext)
157 }
158 field += e.field
159
160 return field
161 }
162
163 // SetContext updates the base context of the error.
164 func (e *errInvalidParam) SetContext(ctx string) {
165 e.context = ctx
166 }
167
168 // AddNestedContext prepends a context to the field's path.
169 func (e *errInvalidParam) AddNestedContext(ctx string) {
170 if len(e.nestedContext) == 0 {
171 e.nestedContext = ctx
172 } else {
173 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
174 }
175
176 }
177
178 // An ErrParamRequired represents an required parameter error.
179 type ErrParamRequired struct {
180 errInvalidParam
181 }
182
183 // NewErrParamRequired creates a new required parameter error.
184 func NewErrParamRequired(field string) *ErrParamRequired {
185 return &ErrParamRequired{
186 errInvalidParam{
187 code: ParamRequiredErrCode,
188 field: field,
189 msg: fmt.Sprintf("missing required field"),
190 },
191 }
192 }
193
194 // An ErrParamMinValue represents a minimum value parameter error.
195 type ErrParamMinValue struct {
196 errInvalidParam
197 min float64
198 }
199
200 // NewErrParamMinValue creates a new minimum value parameter error.
201 func NewErrParamMinValue(field string, min float64) *ErrParamMinValue {
202 return &ErrParamMinValue{
203 errInvalidParam: errInvalidParam{
204 code: ParamMinValueErrCode,
205 field: field,
206 msg: fmt.Sprintf("minimum field value of %v", min),
207 },
208 min: min,
209 }
210 }
211
212 // MinValue returns the field's require minimum value.
213 //
214 // float64 is returned for both int and float min values.
215 func (e *ErrParamMinValue) MinValue() float64 {
216 return e.min
217 }
218
219 // An ErrParamMinLen represents a minimum length parameter error.
220 type ErrParamMinLen struct {
221 errInvalidParam
222 min int
223 }
224
225 // NewErrParamMinLen creates a new minimum length parameter error.
226 func NewErrParamMinLen(field string, min int) *ErrParamMinLen {
227 return &ErrParamMinLen{
228 errInvalidParam: errInvalidParam{
229 code: ParamMinLenErrCode,
230 field: field,
231 msg: fmt.Sprintf("minimum field size of %v", min),
232 },
233 min: min,
234 }
235 }
236
237 // MinLen returns the field's required minimum length.
238 func (e *ErrParamMinLen) MinLen() int {
239 return e.min
240 }
241
242 // An ErrParamMaxLen represents a maximum length parameter error.
243 type ErrParamMaxLen struct {
244 errInvalidParam
245 max int
246 }
247
248 // NewErrParamMaxLen creates a new maximum length parameter error.
249 func NewErrParamMaxLen(field string, max int, value string) *ErrParamMaxLen {
250 return &ErrParamMaxLen{
251 errInvalidParam: errInvalidParam{
252 code: ParamMaxLenErrCode,
253 field: field,
254 msg: fmt.Sprintf("maximum size of %v, %v", max, value),
255 },
256 max: max,
257 }
258 }
259
260 // MaxLen returns the field's required minimum length.
261 func (e *ErrParamMaxLen) MaxLen() int {
262 return e.max
263 }
264
265 // An ErrParamFormat represents a invalid format parameter error.
266 type ErrParamFormat struct {
267 errInvalidParam
268 format string
269 }
270
271 // NewErrParamFormat creates a new invalid format parameter error.
272 func NewErrParamFormat(field string, format, value string) *ErrParamFormat {
273 return &ErrParamFormat{
274 errInvalidParam: errInvalidParam{
275 code: ParamFormatErrCode,
276 field: field,
277 msg: fmt.Sprintf("format %v, %v", format, value),
278 },
279 format: format,
280 }
281 }
282
283 // Format returns the field's required format.
284 func (e *ErrParamFormat) Format() string {
285 return e.format
286 }