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"
20 // ParamMaxLenErrCode is the error code for value being too long.
21 ParamMaxLenErrCode = "ParamMaxLenError"
23 // ParamFormatErrCode is the error code for a field with invalid
24 // format or characters.
25 ParamFormatErrCode = "ParamFormatInvalidError"
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
31 type Validator interface {
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.
40 errs []ErrInvalidParam
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
46 func (e *ErrInvalidParams) Add(err ErrInvalidParam) {
47 err.SetContext(e.Context)
48 e.errs = append(e.errs, err)
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.
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)
64 // Len returns the number of invalid parameter errors
65 func (e ErrInvalidParams) Len() int {
69 // Code returns the code of the error
70 func (e ErrInvalidParams) Code() string {
71 return InvalidParameterErrCode
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))
79 // Error returns the string formatted form of the invalid parameters.
80 func (e ErrInvalidParams) Error() string {
82 fmt.Fprintf(w, "%s: %s\n", e.Code(), e.Message())
84 for _, err := range e.errs {
85 fmt.Fprintf(w, "- %s\n", err.Message())
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())
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++ {
107 // An ErrInvalidParam represents an invalid parameter error type.
108 type ErrInvalidParam interface {
111 // Field name the error occurred on.
114 // SetContext updates the context of the error.
117 // AddNestedContext updates the error's context to include a nested level.
118 AddNestedContext(string)
121 type errInvalidParam struct {
129 // Code returns the error code for the type of invalid parameter.
130 func (e *errInvalidParam) Code() string {
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())
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())
144 // OrigErr returns nil, Implemented for awserr.Error interface.
145 func (e *errInvalidParam) OrigErr() error {
149 // Field Returns the field and context the error occurred.
150 func (e *errInvalidParam) Field() string {
155 if len(e.nestedContext) > 0 {
156 field += fmt.Sprintf("%s.", e.nestedContext)
163 // SetContext updates the base context of the error.
164 func (e *errInvalidParam) SetContext(ctx string) {
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
173 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
178 // An ErrParamRequired represents an required parameter error.
179 type ErrParamRequired struct {
183 // NewErrParamRequired creates a new required parameter error.
184 func NewErrParamRequired(field string) *ErrParamRequired {
185 return &ErrParamRequired{
187 code: ParamRequiredErrCode,
189 msg: fmt.Sprintf("missing required field"),
194 // An ErrParamMinValue represents a minimum value parameter error.
195 type ErrParamMinValue struct {
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,
206 msg: fmt.Sprintf("minimum field value of %v", min),
212 // MinValue returns the field's require minimum value.
214 // float64 is returned for both int and float min values.
215 func (e *ErrParamMinValue) MinValue() float64 {
219 // An ErrParamMinLen represents a minimum length parameter error.
220 type ErrParamMinLen struct {
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,
231 msg: fmt.Sprintf("minimum field size of %v", min),
237 // MinLen returns the field's required minimum length.
238 func (e *ErrParamMinLen) MinLen() int {
242 // An ErrParamMaxLen represents a maximum length parameter error.
243 type ErrParamMaxLen struct {
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,
254 msg: fmt.Sprintf("maximum size of %v, %v", max, value),
260 // MaxLen returns the field's required minimum length.
261 func (e *ErrParamMaxLen) MaxLen() int {
265 // An ErrParamFormat represents a invalid format parameter error.
266 type ErrParamFormat struct {
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,
277 msg: fmt.Sprintf("format %v, %v", format, value),
283 // Format returns the field's required format.
284 func (e *ErrParamFormat) Format() string {