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

86 line
3.8KB

  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. /*
  15. Package validate provides methods to validate a swagger specification,
  16. as well as tools to validate data against their schema.
  17. This package follows Swagger 2.0. specification (aka OpenAPI 2.0). Reference
  18. can be found here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
  19. Validating a specification
  20. Validates a spec document (from JSON or YAML) against the JSON schema for swagger,
  21. then checks a number of extra rules that can't be expressed in JSON schema.
  22. Entry points:
  23. - Spec()
  24. - NewSpecValidator()
  25. - SpecValidator.Validate()
  26. Reported as errors:
  27. [x] definition can't declare a property that's already defined by one of its ancestors
  28. [x] definition's ancestor can't be a descendant of the same model
  29. [x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
  30. [x] each security reference should contain only unique scopes
  31. [x] each security scope in a security definition should be unique
  32. [x] parameters in path must be unique
  33. [x] each path parameter must correspond to a parameter placeholder and vice versa
  34. [x] each referenceable definition must have references
  35. [x] each definition property listed in the required array must be defined in the properties of the model
  36. [x] each parameter should have a unique `name` and `type` combination
  37. [x] each operation should have only 1 parameter of type body
  38. [x] each reference must point to a valid object
  39. [x] every default value that is specified must validate against the schema for that property
  40. [x] items property is required for all schemas/definitions of type `array`
  41. [x] path parameters must be declared a required
  42. [x] headers must not contain $ref
  43. [x] schema and property examples provided must validate against their respective object's schema
  44. [x] examples provided must validate their schema
  45. Reported as warnings:
  46. [x] path parameters should not contain any of [{,},\w]
  47. [x] empty path
  48. [x] unused definitions
  49. [x] unsupported validation of examples on non-JSON media types
  50. [x] examples in response without schema
  51. [x] readOnly properties should not be required
  52. Validating a schema
  53. The schema validation toolkit validates data against JSON-schema-draft 04 schema.
  54. It is tested against the full json-schema-testing-suite (https://github.com/json-schema-org/JSON-Schema-Test-Suite),
  55. except for the optional part (bignum, ECMA regexp, ...).
  56. It supports the complete JSON-schema vocabulary, including keywords not supported by Swagger (e.g. additionalItems, ...)
  57. Entry points:
  58. - AgainstSchema()
  59. - ...
  60. Known limitations
  61. With the current version of this package, the following aspects of swagger are not yet supported:
  62. [ ] errors and warnings are not reported with key/line number in spec
  63. [ ] default values and examples on responses only support application/json producer type
  64. [ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
  65. [ ] rules for collectionFormat are not implemented
  66. [ ] no validation rule for polymorphism support (discriminator) [not done here]
  67. [ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
  68. [ ] arbitrary large numbers are not supported: max is math.MaxFloat64
  69. */
  70. package validate
上海开阖软件有限公司 沪ICP备12045867号-1