You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
19 KiB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validate
  15. import (
  16. "net/http"
  17. "github.com/go-openapi/errors"
  18. )
  19. // Error messages related to spec validation and returned as results.
  20. const (
  21. // ArrayRequiresItemsError ...
  22. ArrayRequiresItemsError = "%s for %q is a collection without an element type (array requires items definition)"
  23. // ArrayInParamRequiresItemsError ...
  24. ArrayInParamRequiresItemsError = "param %q for %q is a collection without an element type (array requires item definition)"
  25. // ArrayInHeaderRequiresItemsError ...
  26. ArrayInHeaderRequiresItemsError = "header %q for %q is a collection without an element type (array requires items definition)"
  27. // BothFormDataAndBodyError indicates that an operation specifies both a body and a formData parameter, which is forbidden
  28. BothFormDataAndBodyError = "operation %q has both formData and body parameters. Only one such In: type may be used for a given operation"
  29. // CannotResolveRefError when a $ref could not be resolved
  30. CannotResolveReferenceError = "could not resolve reference in %s to $ref %s: %v"
  31. // CircularAncestryDefinitionError ...
  32. CircularAncestryDefinitionError = "definition %q has circular ancestry: %v"
  33. // DefaultValueDoesNotValidateError results from an invalid default value provided
  34. DefaultValueDoesNotValidateError = "default value for %s in %s does not validate its schema"
  35. // DefaultValueItemsDoesNotValidateError results from an invalid default value provided for Items
  36. DefaultValueItemsDoesNotValidateError = "default value for %s.items in %s does not validate its schema"
  37. // DefaultValueHeaderDoesNotValidateError results from an invalid default value provided in header
  38. DefaultValueHeaderDoesNotValidateError = "in operation %q, default value in header %s for %s does not validate its schema"
  39. // DefaultValueHeaderItemsDoesNotValidateError results from an invalid default value provided in header.items
  40. DefaultValueHeaderItemsDoesNotValidateError = "in operation %q, default value in header.items %s for %s does not validate its schema"
  41. // DefaultValueInDoesNotValidateError ...
  42. DefaultValueInDoesNotValidateError = "in operation %q, default value in %s does not validate its schema"
  43. // DuplicateParamNameError ...
  44. DuplicateParamNameError = "duplicate parameter name %q for %q in operation %q"
  45. // DuplicatePropertiesError ...
  46. DuplicatePropertiesError = "definition %q contains duplicate properties: %v"
  47. // ExampleValueDoesNotValidateError results from an invalid example value provided
  48. ExampleValueDoesNotValidateError = "example value for %s in %s does not validate its schema"
  49. // ExampleValueItemsDoesNotValidateError results from an invalid example value provided for Items
  50. ExampleValueItemsDoesNotValidateError = "example value for %s.items in %s does not validate its schema"
  51. // ExampleValueHeaderDoesNotValidateError results from an invalid example value provided in header
  52. ExampleValueHeaderDoesNotValidateError = "in operation %q, example value in header %s for %s does not validate its schema"
  53. // ExampleValueHeaderItemsDoesNotValidateError results from an invalid example value provided in header.items
  54. ExampleValueHeaderItemsDoesNotValidateError = "in operation %q, example value in header.items %s for %s does not validate its schema"
  55. // ExampleValueInDoesNotValidateError ...
  56. ExampleValueInDoesNotValidateError = "in operation %q, example value in %s does not validate its schema"
  57. // EmptyPathParameterError means that a path parameter was found empty (e.g. "{}")
  58. EmptyPathParameterError = "%q contains an empty path parameter"
  59. // InvalidDocumentError states that spec validation only processes spec.Document objects
  60. InvalidDocumentError = "spec validator can only validate spec.Document objects"
  61. // InvalidItemsPatternError indicates an Items definition with invalid pattern
  62. InvalidItemsPatternError = "%s for %q has invalid items pattern: %q"
  63. // InvalidParameterDefinitionError indicates an error detected on a parameter definition
  64. InvalidParameterDefinitionError = "invalid definition for parameter %s in %s in operation %q"
  65. // InvalidParameterDefinitionAsSchemaError indicates an error detected on a parameter definition, which was mistaken with a schema definition.
  66. // Most likely, this situation is encountered whenever a $ref has been added as a sibling of the parameter definition.
  67. InvalidParameterDefinitionAsSchemaError = "invalid definition as Schema for parameter %s in %s in operation %q"
  68. // InvalidPatternError ...
  69. InvalidPatternError = "pattern %q is invalid in %s"
  70. // InvalidPatternInError indicates an invalid pattern in a schema or items definition
  71. InvalidPatternInError = "%s in %s has invalid pattern: %q"
  72. // InvalidPatternInHeaderError indicates a header definition with an invalid pattern
  73. InvalidPatternInHeaderError = "in operation %q, header %s for %s has invalid pattern %q: %v"
  74. // InvalidPatternInParamError ...
  75. InvalidPatternInParamError = "operation %q has invalid pattern in param %q: %q"
  76. // InvalidReferenceError indicates that a $ref property could not be resolved
  77. InvalidReferenceError = "invalid ref %q"
  78. // InvalidResponseDefinitionAsSchemaError indicates an error detected on a response definition, which was mistaken with a schema definition.
  79. // Most likely, this situation is encountered whenever a $ref has been added as a sibling of the response definition.
  80. InvalidResponseDefinitionAsSchemaError = "invalid definition as Schema for response %s in %s"
  81. // MultipleBodyParamError indicates that an operation specifies multiple parameter with in: body
  82. MultipleBodyParamError = "operation %q has more than 1 body param: %v"
  83. // NonUniqueOperationIDError indicates that the same operationId has been specified several times
  84. NonUniqueOperationIDError = "%q is defined %d times"
  85. // NoParameterInPathError indicates that a path was found without any parameter
  86. NoParameterInPathError = "path param %q has no parameter definition"
  87. // NoValidPathErrorOrWarning indicates that no single path could be validated. If Paths is empty, this message is only a warning.
  88. NoValidPathErrorOrWarning = "spec has no valid path defined"
  89. // NoValidResponseError indicates that no valid response description could be found for an operation
  90. NoValidResponseError = "operation %q has no valid response"
  91. // PathOverlapError ...
  92. PathOverlapError = "path %s overlaps with %s"
  93. // PathParamNotInPathError indicates that a parameter specified with in: path was not found in the path specification
  94. PathParamNotInPathError = "path param %q is not present in path %q"
  95. // PathParamNotUniqueError ...
  96. PathParamNotUniqueError = "params in path %q must be unique: %q conflicts with %q"
  97. // PathParamNotRequiredError ...
  98. PathParamRequiredError = "in operation %q,path param %q must be declared as required"
  99. // RefNotAllowedInHeaderError indicates a $ref was found in a header definition, which is not allowed by Swagger
  100. RefNotAllowedInHeaderError = "IMPORTANT!in %q: $ref are not allowed in headers. In context for header %q%s"
  101. // RequiredButNotDefinedError ...
  102. RequiredButNotDefinedError = "%q is present in required but not defined as property in definition %q"
  103. // SomeParametersBrokenError indicates that some parameters could not be resolved, which might result in partial checks to be carried on
  104. SomeParametersBrokenError = "some parameters definitions are broken in %q.%s. Cannot carry on full checks on parameters for operation %s"
  105. // UnresolvedReferencesError indicates that at least one $ref could not be resolved
  106. UnresolvedReferencesError = "some references could not be resolved in spec. First found: %v"
  107. )
  108. // Warning messages related to spec validation and returned as results
  109. const (
  110. // ExamplesWithoutSchemaWarning indicates that examples are provided for a response,but not schema to validate the example against
  111. ExamplesWithoutSchemaWarning = "Examples provided without schema in operation %q, %s"
  112. // ExamplesMimeNotSupportedWarning indicates that examples are provided with a mime type different than application/json, which
  113. // the validator dos not support yetl
  114. ExamplesMimeNotSupportedWarning = "No validation attempt for examples for media types other than application/json, in operation %q, %s"
  115. // PathParamGarbledWarning ...
  116. PathParamGarbledWarning = "in path %q, param %q contains {,} or white space. Albeit not stricly illegal, this is probably no what you want"
  117. // ParamValidationTypeMismatch indicates that parameter has validation which does not match its type
  118. ParamValidationTypeMismatch = "validation keywords of parameter %q in path %q don't match its type %s"
  119. // PathStrippedParamGarbledWarning ...
  120. PathStrippedParamGarbledWarning = "path stripped from path parameters %s contains {,} or white space. This is probably no what you want."
  121. // ReadOnlyAndRequiredWarning ...
  122. ReadOnlyAndRequiredWarning = "Required property %s in %q should not be marked as both required and readOnly"
  123. // RefShouldNotHaveSiblingsWarning indicates that a $ref was found with a sibling definition. This results in the $ref taking over its siblings,
  124. // which is most likely not wanted.
  125. RefShouldNotHaveSiblingsWarning = "$ref property should have no sibling in %q.%s"
  126. // RequiredHasDefaultWarning indicates that a required parameter property should not have a default
  127. RequiredHasDefaultWarning = "%s in %s has a default value and is required as parameter"
  128. // UnusedDefinitionWarning ...
  129. UnusedDefinitionWarning = "definition %q is not used anywhere"
  130. // UnusedParamWarning ...
  131. UnusedParamWarning = "parameter %q is not used anywhere"
  132. // UnusedResponseWarning ...
  133. UnusedResponseWarning = "response %q is not used anywhere"
  134. )
  135. // Additional error codes
  136. const (
  137. // InternalErrorCode reports an internal technical error
  138. InternalErrorCode = http.StatusInternalServerError
  139. // NotFoundErrorCode indicates that a resource (e.g. a $ref) could not be found
  140. NotFoundErrorCode = http.StatusNotFound
  141. )
  142. func invalidDocumentMsg() errors.Error {
  143. return errors.New(InternalErrorCode, InvalidDocumentError)
  144. }
  145. func invalidRefMsg(path string) errors.Error {
  146. return errors.New(NotFoundErrorCode, InvalidReferenceError, path)
  147. }
  148. func unresolvedReferencesMsg(err error) errors.Error {
  149. return errors.New(errors.CompositeErrorCode, UnresolvedReferencesError, err)
  150. }
  151. func noValidPathMsg() errors.Error {
  152. return errors.New(errors.CompositeErrorCode, NoValidPathErrorOrWarning)
  153. }
  154. func emptyPathParameterMsg(path string) errors.Error {
  155. return errors.New(errors.CompositeErrorCode, EmptyPathParameterError, path)
  156. }
  157. func nonUniqueOperationIDMsg(path string, i int) errors.Error {
  158. return errors.New(errors.CompositeErrorCode, NonUniqueOperationIDError, path, i)
  159. }
  160. func circularAncestryDefinitionMsg(path string, args interface{}) errors.Error {
  161. return errors.New(errors.CompositeErrorCode, CircularAncestryDefinitionError, path, args)
  162. }
  163. func duplicatePropertiesMsg(path string, args interface{}) errors.Error {
  164. return errors.New(errors.CompositeErrorCode, DuplicatePropertiesError, path, args)
  165. }
  166. func pathParamNotInPathMsg(path, param string) errors.Error {
  167. return errors.New(errors.CompositeErrorCode, PathParamNotInPathError, param, path)
  168. }
  169. func arrayRequiresItemsMsg(path, operation string) errors.Error {
  170. return errors.New(errors.CompositeErrorCode, ArrayRequiresItemsError, path, operation)
  171. }
  172. func arrayInParamRequiresItemsMsg(path, operation string) errors.Error {
  173. return errors.New(errors.CompositeErrorCode, ArrayInParamRequiresItemsError, path, operation)
  174. }
  175. func arrayInHeaderRequiresItemsMsg(path, operation string) errors.Error {
  176. return errors.New(errors.CompositeErrorCode, ArrayInHeaderRequiresItemsError, path, operation)
  177. }
  178. func invalidItemsPatternMsg(path, operation, pattern string) errors.Error {
  179. return errors.New(errors.CompositeErrorCode, InvalidItemsPatternError, path, operation, pattern)
  180. }
  181. func invalidPatternMsg(pattern, path string) errors.Error {
  182. return errors.New(errors.CompositeErrorCode, InvalidPatternError, pattern, path)
  183. }
  184. func requiredButNotDefinedMsg(path, definition string) errors.Error {
  185. return errors.New(errors.CompositeErrorCode, RequiredButNotDefinedError, path, definition)
  186. }
  187. func pathParamGarbledMsg(path, param string) errors.Error {
  188. return errors.New(errors.CompositeErrorCode, PathParamGarbledWarning, path, param)
  189. }
  190. func pathStrippedParamGarbledMsg(path string) errors.Error {
  191. return errors.New(errors.CompositeErrorCode, PathStrippedParamGarbledWarning, path)
  192. }
  193. func pathOverlapMsg(path, arg string) errors.Error {
  194. return errors.New(errors.CompositeErrorCode, PathOverlapError, path, arg)
  195. }
  196. func invalidPatternInParamMsg(operation, param, pattern string) errors.Error {
  197. return errors.New(errors.CompositeErrorCode, InvalidPatternInParamError, operation, param, pattern)
  198. }
  199. func pathParamRequiredMsg(operation, param string) errors.Error {
  200. return errors.New(errors.CompositeErrorCode, PathParamRequiredError, operation, param)
  201. }
  202. func bothFormDataAndBodyMsg(operation string) errors.Error {
  203. return errors.New(errors.CompositeErrorCode, BothFormDataAndBodyError, operation)
  204. }
  205. func multipleBodyParamMsg(operation string, args interface{}) errors.Error {
  206. return errors.New(errors.CompositeErrorCode, MultipleBodyParamError, operation, args)
  207. }
  208. func pathParamNotUniqueMsg(path, param, arg string) errors.Error {
  209. return errors.New(errors.CompositeErrorCode, PathParamNotUniqueError, path, param, arg)
  210. }
  211. func duplicateParamNameMsg(path, param, operation string) errors.Error {
  212. return errors.New(errors.CompositeErrorCode, DuplicateParamNameError, param, path, operation)
  213. }
  214. func unusedParamMsg(arg string) errors.Error {
  215. return errors.New(errors.CompositeErrorCode, UnusedParamWarning, arg)
  216. }
  217. func unusedDefinitionMsg(arg string) errors.Error {
  218. return errors.New(errors.CompositeErrorCode, UnusedDefinitionWarning, arg)
  219. }
  220. func unusedResponseMsg(arg string) errors.Error {
  221. return errors.New(errors.CompositeErrorCode, UnusedResponseWarning, arg)
  222. }
  223. func readOnlyAndRequiredMsg(path, param string) errors.Error {
  224. return errors.New(errors.CompositeErrorCode, ReadOnlyAndRequiredWarning, param, path)
  225. }
  226. func noParameterInPathMsg(param string) errors.Error {
  227. return errors.New(errors.CompositeErrorCode, NoParameterInPathError, param)
  228. }
  229. func requiredHasDefaultMsg(param, path string) errors.Error {
  230. return errors.New(errors.CompositeErrorCode, RequiredHasDefaultWarning, param, path)
  231. }
  232. func defaultValueDoesNotValidateMsg(param, path string) errors.Error {
  233. return errors.New(errors.CompositeErrorCode, DefaultValueDoesNotValidateError, param, path)
  234. }
  235. func defaultValueItemsDoesNotValidateMsg(param, path string) errors.Error {
  236. return errors.New(errors.CompositeErrorCode, DefaultValueItemsDoesNotValidateError, param, path)
  237. }
  238. func noValidResponseMsg(operation string) errors.Error {
  239. return errors.New(errors.CompositeErrorCode, NoValidResponseError, operation)
  240. }
  241. func defaultValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
  242. return errors.New(errors.CompositeErrorCode, DefaultValueHeaderDoesNotValidateError, operation, header, path)
  243. }
  244. func defaultValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
  245. return errors.New(errors.CompositeErrorCode, DefaultValueHeaderItemsDoesNotValidateError, operation, header, path)
  246. }
  247. func invalidPatternInHeaderMsg(operation, header, path, pattern string, args interface{}) errors.Error {
  248. return errors.New(errors.CompositeErrorCode, InvalidPatternInHeaderError, operation, header, path, pattern, args)
  249. }
  250. func invalidPatternInMsg(path, in, pattern string) errors.Error {
  251. return errors.New(errors.CompositeErrorCode, InvalidPatternInError, path, in, pattern)
  252. }
  253. func defaultValueInDoesNotValidateMsg(operation, path string) errors.Error {
  254. return errors.New(errors.CompositeErrorCode, DefaultValueInDoesNotValidateError, operation, path)
  255. }
  256. func exampleValueDoesNotValidateMsg(param, path string) errors.Error {
  257. return errors.New(errors.CompositeErrorCode, ExampleValueDoesNotValidateError, param, path)
  258. }
  259. func exampleValueItemsDoesNotValidateMsg(param, path string) errors.Error {
  260. return errors.New(errors.CompositeErrorCode, ExampleValueItemsDoesNotValidateError, param, path)
  261. }
  262. func exampleValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
  263. return errors.New(errors.CompositeErrorCode, ExampleValueHeaderDoesNotValidateError, operation, header, path)
  264. }
  265. func exampleValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
  266. return errors.New(errors.CompositeErrorCode, ExampleValueHeaderItemsDoesNotValidateError, operation, header, path)
  267. }
  268. func exampleValueInDoesNotValidateMsg(operation, path string) errors.Error {
  269. return errors.New(errors.CompositeErrorCode, ExampleValueInDoesNotValidateError, operation, path)
  270. }
  271. func examplesWithoutSchemaMsg(operation, response string) errors.Error {
  272. return errors.New(errors.CompositeErrorCode, ExamplesWithoutSchemaWarning, operation, response)
  273. }
  274. func examplesMimeNotSupportedMsg(operation, response string) errors.Error {
  275. return errors.New(errors.CompositeErrorCode, ExamplesMimeNotSupportedWarning, operation, response)
  276. }
  277. func refNotAllowedInHeaderMsg(path, header, ref string) errors.Error {
  278. return errors.New(errors.CompositeErrorCode, RefNotAllowedInHeaderError, path, header, ref)
  279. }
  280. func cannotResolveRefMsg(path, ref string, err error) errors.Error {
  281. return errors.New(errors.CompositeErrorCode, CannotResolveReferenceError, path, ref, err)
  282. }
  283. func invalidParameterDefinitionMsg(path, method, operationID string) errors.Error {
  284. return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionError, path, method, operationID)
  285. }
  286. func invalidParameterDefinitionAsSchemaMsg(path, method, operationID string) errors.Error {
  287. return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionAsSchemaError, path, method, operationID)
  288. }
  289. func parameterValidationTypeMismatchMsg(param, path, typ string) errors.Error {
  290. return errors.New(errors.CompositeErrorCode, ParamValidationTypeMismatch, param, path, typ)
  291. }
  292. // disabled
  293. //func invalidResponseDefinitionAsSchemaMsg(path, method string) errors.Error {
  294. // return errors.New(errors.CompositeErrorCode, InvalidResponseDefinitionAsSchemaError, path, method)
  295. //}
  296. func someParametersBrokenMsg(path, method, operationID string) errors.Error {
  297. return errors.New(errors.CompositeErrorCode, SomeParametersBrokenError, path, method, operationID)
  298. }
  299. func refShouldNotHaveSiblingsMsg(path, operationID string) errors.Error {
  300. return errors.New(errors.CompositeErrorCode, RefShouldNotHaveSiblingsWarning, operationID, path)
  301. }