本站源代码
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.

355 lines
18KB

  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. // PathStrippedParamGarbledWarning ...
  118. PathStrippedParamGarbledWarning = "path stripped from path parameters %s contains {,} or white space. This is probably no what you want."
  119. // ReadOnlyAndRequiredWarning ...
  120. ReadOnlyAndRequiredWarning = "Required property %s in %q should not be marked as both required and readOnly"
  121. // RefShouldNotHaveSiblingsWarning indicates that a $ref was found with a sibling definition. This results in the $ref taking over its siblings,
  122. // which is most likely not wanted.
  123. RefShouldNotHaveSiblingsWarning = "$ref property should have no sibling in %q.%s"
  124. // RequiredHasDefaultWarning indicates that a required parameter property should not have a default
  125. RequiredHasDefaultWarning = "%s in %s has a default value and is required as parameter"
  126. // UnusedDefinitionWarning ...
  127. UnusedDefinitionWarning = "definition %q is not used anywhere"
  128. // UnusedParamWarning ...
  129. UnusedParamWarning = "parameter %q is not used anywhere"
  130. // UnusedResponseWarning ...
  131. UnusedResponseWarning = "response %q is not used anywhere"
  132. )
  133. // Additional error codes
  134. const (
  135. // InternalErrorCode reports an internal technical error
  136. InternalErrorCode = http.StatusInternalServerError
  137. // NotFoundErrorCode indicates that a resource (e.g. a $ref) could not be found
  138. NotFoundErrorCode = http.StatusNotFound
  139. )
  140. func invalidDocumentMsg() errors.Error {
  141. return errors.New(InternalErrorCode, InvalidDocumentError)
  142. }
  143. func invalidRefMsg(path string) errors.Error {
  144. return errors.New(NotFoundErrorCode, InvalidReferenceError, path)
  145. }
  146. func unresolvedReferencesMsg(err error) errors.Error {
  147. return errors.New(errors.CompositeErrorCode, UnresolvedReferencesError, err)
  148. }
  149. func noValidPathMsg() errors.Error {
  150. return errors.New(errors.CompositeErrorCode, NoValidPathErrorOrWarning)
  151. }
  152. func emptyPathParameterMsg(path string) errors.Error {
  153. return errors.New(errors.CompositeErrorCode, EmptyPathParameterError, path)
  154. }
  155. func nonUniqueOperationIDMsg(path string, i int) errors.Error {
  156. return errors.New(errors.CompositeErrorCode, NonUniqueOperationIDError, path, i)
  157. }
  158. func circularAncestryDefinitionMsg(path string, args interface{}) errors.Error {
  159. return errors.New(errors.CompositeErrorCode, CircularAncestryDefinitionError, path, args)
  160. }
  161. func duplicatePropertiesMsg(path string, args interface{}) errors.Error {
  162. return errors.New(errors.CompositeErrorCode, DuplicatePropertiesError, path, args)
  163. }
  164. func pathParamNotInPathMsg(path, param string) errors.Error {
  165. return errors.New(errors.CompositeErrorCode, PathParamNotInPathError, param, path)
  166. }
  167. func arrayRequiresItemsMsg(path, operation string) errors.Error {
  168. return errors.New(errors.CompositeErrorCode, ArrayRequiresItemsError, path, operation)
  169. }
  170. func arrayInParamRequiresItemsMsg(path, operation string) errors.Error {
  171. return errors.New(errors.CompositeErrorCode, ArrayInParamRequiresItemsError, path, operation)
  172. }
  173. func arrayInHeaderRequiresItemsMsg(path, operation string) errors.Error {
  174. return errors.New(errors.CompositeErrorCode, ArrayInHeaderRequiresItemsError, path, operation)
  175. }
  176. func invalidItemsPatternMsg(path, operation, pattern string) errors.Error {
  177. return errors.New(errors.CompositeErrorCode, InvalidItemsPatternError, path, operation, pattern)
  178. }
  179. func invalidPatternMsg(pattern, path string) errors.Error {
  180. return errors.New(errors.CompositeErrorCode, InvalidPatternError, pattern, path)
  181. }
  182. func requiredButNotDefinedMsg(path, definition string) errors.Error {
  183. return errors.New(errors.CompositeErrorCode, RequiredButNotDefinedError, path, definition)
  184. }
  185. func pathParamGarbledMsg(path, param string) errors.Error {
  186. return errors.New(errors.CompositeErrorCode, PathParamGarbledWarning, path, param)
  187. }
  188. func pathStrippedParamGarbledMsg(path string) errors.Error {
  189. return errors.New(errors.CompositeErrorCode, PathStrippedParamGarbledWarning, path)
  190. }
  191. func pathOverlapMsg(path, arg string) errors.Error {
  192. return errors.New(errors.CompositeErrorCode, PathOverlapError, path, arg)
  193. }
  194. func invalidPatternInParamMsg(operation, param, pattern string) errors.Error {
  195. return errors.New(errors.CompositeErrorCode, InvalidPatternInParamError, operation, param, pattern)
  196. }
  197. func pathParamRequiredMsg(operation, param string) errors.Error {
  198. return errors.New(errors.CompositeErrorCode, PathParamRequiredError, operation, param)
  199. }
  200. func bothFormDataAndBodyMsg(operation string) errors.Error {
  201. return errors.New(errors.CompositeErrorCode, BothFormDataAndBodyError, operation)
  202. }
  203. func multipleBodyParamMsg(operation string, args interface{}) errors.Error {
  204. return errors.New(errors.CompositeErrorCode, MultipleBodyParamError, operation, args)
  205. }
  206. func pathParamNotUniqueMsg(path, param, arg string) errors.Error {
  207. return errors.New(errors.CompositeErrorCode, PathParamNotUniqueError, path, param, arg)
  208. }
  209. func duplicateParamNameMsg(path, param, operation string) errors.Error {
  210. return errors.New(errors.CompositeErrorCode, DuplicateParamNameError, param, path, operation)
  211. }
  212. func unusedParamMsg(arg string) errors.Error {
  213. return errors.New(errors.CompositeErrorCode, UnusedParamWarning, arg)
  214. }
  215. func unusedDefinitionMsg(arg string) errors.Error {
  216. return errors.New(errors.CompositeErrorCode, UnusedDefinitionWarning, arg)
  217. }
  218. func unusedResponseMsg(arg string) errors.Error {
  219. return errors.New(errors.CompositeErrorCode, UnusedResponseWarning, arg)
  220. }
  221. func readOnlyAndRequiredMsg(path, param string) errors.Error {
  222. return errors.New(errors.CompositeErrorCode, ReadOnlyAndRequiredWarning, param, path)
  223. }
  224. func noParameterInPathMsg(param string) errors.Error {
  225. return errors.New(errors.CompositeErrorCode, NoParameterInPathError, param)
  226. }
  227. func requiredHasDefaultMsg(param, path string) errors.Error {
  228. return errors.New(errors.CompositeErrorCode, RequiredHasDefaultWarning, param, path)
  229. }
  230. func defaultValueDoesNotValidateMsg(param, path string) errors.Error {
  231. return errors.New(errors.CompositeErrorCode, DefaultValueDoesNotValidateError, param, path)
  232. }
  233. func defaultValueItemsDoesNotValidateMsg(param, path string) errors.Error {
  234. return errors.New(errors.CompositeErrorCode, DefaultValueItemsDoesNotValidateError, param, path)
  235. }
  236. func noValidResponseMsg(operation string) errors.Error {
  237. return errors.New(errors.CompositeErrorCode, NoValidResponseError, operation)
  238. }
  239. func defaultValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
  240. return errors.New(errors.CompositeErrorCode, DefaultValueHeaderDoesNotValidateError, operation, header, path)
  241. }
  242. func defaultValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
  243. return errors.New(errors.CompositeErrorCode, DefaultValueHeaderItemsDoesNotValidateError, operation, header, path)
  244. }
  245. func invalidPatternInHeaderMsg(operation, header, path, pattern string, args interface{}) errors.Error {
  246. return errors.New(errors.CompositeErrorCode, InvalidPatternInHeaderError, operation, header, path, pattern, args)
  247. }
  248. func invalidPatternInMsg(path, in, pattern string) errors.Error {
  249. return errors.New(errors.CompositeErrorCode, InvalidPatternInError, path, in, pattern)
  250. }
  251. func defaultValueInDoesNotValidateMsg(operation, path string) errors.Error {
  252. return errors.New(errors.CompositeErrorCode, DefaultValueInDoesNotValidateError, operation, path)
  253. }
  254. func exampleValueDoesNotValidateMsg(param, path string) errors.Error {
  255. return errors.New(errors.CompositeErrorCode, ExampleValueDoesNotValidateError, param, path)
  256. }
  257. func exampleValueItemsDoesNotValidateMsg(param, path string) errors.Error {
  258. return errors.New(errors.CompositeErrorCode, ExampleValueItemsDoesNotValidateError, param, path)
  259. }
  260. func exampleValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
  261. return errors.New(errors.CompositeErrorCode, ExampleValueHeaderDoesNotValidateError, operation, header, path)
  262. }
  263. func exampleValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
  264. return errors.New(errors.CompositeErrorCode, ExampleValueHeaderItemsDoesNotValidateError, operation, header, path)
  265. }
  266. func exampleValueInDoesNotValidateMsg(operation, path string) errors.Error {
  267. return errors.New(errors.CompositeErrorCode, ExampleValueInDoesNotValidateError, operation, path)
  268. }
  269. func examplesWithoutSchemaMsg(operation, response string) errors.Error {
  270. return errors.New(errors.CompositeErrorCode, ExamplesWithoutSchemaWarning, operation, response)
  271. }
  272. func examplesMimeNotSupportedMsg(operation, response string) errors.Error {
  273. return errors.New(errors.CompositeErrorCode, ExamplesMimeNotSupportedWarning, operation, response)
  274. }
  275. func refNotAllowedInHeaderMsg(path, header, ref string) errors.Error {
  276. return errors.New(errors.CompositeErrorCode, RefNotAllowedInHeaderError, path, header, ref)
  277. }
  278. func cannotResolveRefMsg(path, ref string, err error) errors.Error {
  279. return errors.New(errors.CompositeErrorCode, CannotResolveReferenceError, path, ref, err)
  280. }
  281. func invalidParameterDefinitionMsg(path, method, operationID string) errors.Error {
  282. return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionError, path, method, operationID)
  283. }
  284. func invalidParameterDefinitionAsSchemaMsg(path, method, operationID string) errors.Error {
  285. return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionAsSchemaError, path, method, operationID)
  286. }
  287. // disabled
  288. //func invalidResponseDefinitionAsSchemaMsg(path, method string) errors.Error {
  289. // return errors.New(errors.CompositeErrorCode, InvalidResponseDefinitionAsSchemaError, path, method)
  290. //}
  291. func someParametersBrokenMsg(path, method, operationID string) errors.Error {
  292. return errors.New(errors.CompositeErrorCode, SomeParametersBrokenError, path, method, operationID)
  293. }
  294. func refShouldNotHaveSiblingsMsg(path, operationID string) errors.Error {
  295. return errors.New(errors.CompositeErrorCode, RefShouldNotHaveSiblingsWarning, operationID, path)
  296. }
上海开阖软件有限公司 沪ICP备12045867号-1