validation.go 7.4 KB

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