validation.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package request
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. )
  7. const (
  8. // InvalidParameterErrCode is the error code for invalid parameters errors
  9. InvalidParameterErrCode = "InvalidParameter"
  10. // ParamRequiredErrCode is the error code for required parameter errors
  11. ParamRequiredErrCode = "ParamRequiredError"
  12. // ParamMinValueErrCode is the error code for fields with too low of a
  13. // number value.
  14. ParamMinValueErrCode = "ParamMinValueError"
  15. // ParamMinLenErrCode is the error code for fields without enough elements.
  16. ParamMinLenErrCode = "ParamMinLenError"
  17. )
  18. // Validator provides a way for types to perform validation logic on their
  19. // input values that external code can use to determine if a type's values
  20. // are valid.
  21. type Validator interface {
  22. Validate() error
  23. }
  24. // An ErrInvalidParams provides wrapping of invalid parameter errors found when
  25. // validating API operation input parameters.
  26. type ErrInvalidParams struct {
  27. // Context is the base context of the invalid parameter group.
  28. Context string
  29. errs []ErrInvalidParam
  30. }
  31. // Add adds a new invalid parameter error to the collection of invalid
  32. // parameters. The context of the invalid parameter will be updated to reflect
  33. // this collection.
  34. func (e *ErrInvalidParams) Add(err ErrInvalidParam) {
  35. err.SetContext(e.Context)
  36. e.errs = append(e.errs, err)
  37. }
  38. // AddNested adds the invalid parameter errors from another ErrInvalidParams
  39. // value into this collection. The nested errors will have their nested context
  40. // updated and base context to reflect the merging.
  41. //
  42. // Use for nested validations errors.
  43. func (e *ErrInvalidParams) AddNested(nestedCtx string, nested ErrInvalidParams) {
  44. for _, err := range nested.errs {
  45. err.SetContext(e.Context)
  46. err.AddNestedContext(nestedCtx)
  47. e.errs = append(e.errs, err)
  48. }
  49. }
  50. // Len returns the number of invalid parameter errors
  51. func (e ErrInvalidParams) Len() int {
  52. return len(e.errs)
  53. }
  54. // Code returns the code of the error
  55. func (e ErrInvalidParams) Code() string {
  56. return InvalidParameterErrCode
  57. }
  58. // Message returns the message of the error
  59. func (e ErrInvalidParams) Message() string {
  60. return fmt.Sprintf("%d validation error(s) found.", len(e.errs))
  61. }
  62. // Error returns the string formatted form of the invalid parameters.
  63. func (e ErrInvalidParams) Error() string {
  64. w := &bytes.Buffer{}
  65. fmt.Fprintf(w, "%s: %s\n", e.Code(), e.Message())
  66. for _, err := range e.errs {
  67. fmt.Fprintf(w, "- %s\n", err.Message())
  68. }
  69. return w.String()
  70. }
  71. // OrigErr returns the invalid parameters as a awserr.BatchedErrors value
  72. func (e ErrInvalidParams) OrigErr() error {
  73. return awserr.NewBatchError(
  74. InvalidParameterErrCode, e.Message(), e.OrigErrs())
  75. }
  76. // OrigErrs returns a slice of the invalid parameters
  77. func (e ErrInvalidParams) OrigErrs() []error {
  78. errs := make([]error, len(e.errs))
  79. for i := 0; i < len(errs); i++ {
  80. errs[i] = e.errs[i]
  81. }
  82. return errs
  83. }
  84. // An ErrInvalidParam represents an invalid parameter error type.
  85. type ErrInvalidParam interface {
  86. awserr.Error
  87. // Field name the error occurred on.
  88. Field() string
  89. // SetContext updates the context of the error.
  90. SetContext(string)
  91. // AddNestedContext updates the error's context to include a nested level.
  92. AddNestedContext(string)
  93. }
  94. type errInvalidParam struct {
  95. context string
  96. nestedContext string
  97. field string
  98. code string
  99. msg string
  100. }
  101. // Code returns the error code for the type of invalid parameter.
  102. func (e *errInvalidParam) Code() string {
  103. return e.code
  104. }
  105. // Message returns the reason the parameter was invalid, and its context.
  106. func (e *errInvalidParam) Message() string {
  107. return fmt.Sprintf("%s, %s.", e.msg, e.Field())
  108. }
  109. // Error returns the string version of the invalid parameter error.
  110. func (e *errInvalidParam) Error() string {
  111. return fmt.Sprintf("%s: %s", e.code, e.Message())
  112. }
  113. // OrigErr returns nil, Implemented for awserr.Error interface.
  114. func (e *errInvalidParam) OrigErr() error {
  115. return nil
  116. }
  117. // Field Returns the field and context the error occurred.
  118. func (e *errInvalidParam) Field() string {
  119. field := e.context
  120. if len(field) > 0 {
  121. field += "."
  122. }
  123. if len(e.nestedContext) > 0 {
  124. field += fmt.Sprintf("%s.", e.nestedContext)
  125. }
  126. field += e.field
  127. return field
  128. }
  129. // SetContext updates the base context of the error.
  130. func (e *errInvalidParam) SetContext(ctx string) {
  131. e.context = ctx
  132. }
  133. // AddNestedContext prepends a context to the field's path.
  134. func (e *errInvalidParam) AddNestedContext(ctx string) {
  135. if len(e.nestedContext) == 0 {
  136. e.nestedContext = ctx
  137. } else {
  138. e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
  139. }
  140. }
  141. // An ErrParamRequired represents an required parameter error.
  142. type ErrParamRequired struct {
  143. errInvalidParam
  144. }
  145. // NewErrParamRequired creates a new required parameter error.
  146. func NewErrParamRequired(field string) *ErrParamRequired {
  147. return &ErrParamRequired{
  148. errInvalidParam{
  149. code: ParamRequiredErrCode,
  150. field: field,
  151. msg: fmt.Sprintf("missing required field"),
  152. },
  153. }
  154. }
  155. // An ErrParamMinValue represents a minimum value parameter error.
  156. type ErrParamMinValue struct {
  157. errInvalidParam
  158. min float64
  159. }
  160. // NewErrParamMinValue creates a new minimum value parameter error.
  161. func NewErrParamMinValue(field string, min float64) *ErrParamMinValue {
  162. return &ErrParamMinValue{
  163. errInvalidParam: errInvalidParam{
  164. code: ParamMinValueErrCode,
  165. field: field,
  166. msg: fmt.Sprintf("minimum field value of %v", min),
  167. },
  168. min: min,
  169. }
  170. }
  171. // MinValue returns the field's require minimum value.
  172. //
  173. // float64 is returned for both int and float min values.
  174. func (e *ErrParamMinValue) MinValue() float64 {
  175. return e.min
  176. }
  177. // An ErrParamMinLen represents a minimum length parameter error.
  178. type ErrParamMinLen struct {
  179. errInvalidParam
  180. min int
  181. }
  182. // NewErrParamMinLen creates a new minimum length parameter error.
  183. func NewErrParamMinLen(field string, min int) *ErrParamMinLen {
  184. return &ErrParamMinLen{
  185. errInvalidParam: errInvalidParam{
  186. code: ParamMinLenErrCode,
  187. field: field,
  188. msg: fmt.Sprintf("minimum field size of %v", min),
  189. },
  190. min: min,
  191. }
  192. }
  193. // MinLen returns the field's required minimum length.
  194. func (e *ErrParamMinLen) MinLen() int {
  195. return e.min
  196. }