]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/aws/aws-sdk-go/aws/request/validation.go
Initial transfer of provider code
[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 )
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.
25 type Validator interface {
26 Validate() error
27 }
28
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.
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.
40 func (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.
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)
55 }
56 }
57
58 // Len returns the number of invalid parameter errors
59 func (e ErrInvalidParams) Len() int {
60 return len(e.errs)
61 }
62
63 // Code returns the code of the error
64 func (e ErrInvalidParams) Code() string {
65 return InvalidParameterErrCode
66 }
67
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))
71 }
72
73 // Error returns the string formatted form of the invalid parameters.
74 func (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
86 func (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
92 func (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.
102 type 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
115 type 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.
124 func (e *errInvalidParam) Code() string {
125 return e.code
126 }
127
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())
131 }
132
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())
136 }
137
138 // OrigErr returns nil, Implemented for awserr.Error interface.
139 func (e *errInvalidParam) OrigErr() error {
140 return nil
141 }
142
143 // Field Returns the field and context the error occurred.
144 func (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.
158 func (e *errInvalidParam) SetContext(ctx string) {
159 e.context = ctx
160 }
161
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
166 } else {
167 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
168 }
169
170 }
171
172 // An ErrParamRequired represents an required parameter error.
173 type ErrParamRequired struct {
174 errInvalidParam
175 }
176
177 // NewErrParamRequired creates a new required parameter error.
178 func 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.
189 type ErrParamMinValue struct {
190 errInvalidParam
191 min float64
192 }
193
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,
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.
209 func (e *ErrParamMinValue) MinValue() float64 {
210 return e.min
211 }
212
213 // An ErrParamMinLen represents a minimum length parameter error.
214 type ErrParamMinLen struct {
215 errInvalidParam
216 min int
217 }
218
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,
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.
232 func (e *ErrParamMinLen) MinLen() int {
233 return e.min
234 }