spec.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 spec
  15. import "encoding/json"
  16. //go:generate curl -L --progress -o ./schemas/v2/schema.json http://swagger.io/v2/schema.json
  17. //go:generate curl -L --progress -o ./schemas/jsonschema-draft-04.json http://json-schema.org/draft-04/schema
  18. //go:generate go-bindata -pkg=spec -prefix=./schemas -ignore=.*\.md ./schemas/...
  19. //go:generate perl -pi -e s,Json,JSON,g bindata.go
  20. const (
  21. // SwaggerSchemaURL the url for the swagger 2.0 schema to validate specs
  22. SwaggerSchemaURL = "http://swagger.io/v2/schema.json#"
  23. // JSONSchemaURL the url for the json schema schema
  24. JSONSchemaURL = "http://json-schema.org/draft-04/schema#"
  25. )
  26. var (
  27. jsonSchema *Schema
  28. swaggerSchema *Schema
  29. )
  30. func init() {
  31. jsonSchema = MustLoadJSONSchemaDraft04()
  32. swaggerSchema = MustLoadSwagger20Schema()
  33. }
  34. // MustLoadJSONSchemaDraft04 panics when Swagger20Schema returns an error
  35. func MustLoadJSONSchemaDraft04() *Schema {
  36. d, e := JSONSchemaDraft04()
  37. if e != nil {
  38. panic(e)
  39. }
  40. return d
  41. }
  42. // JSONSchemaDraft04 loads the json schema document for json shema draft04
  43. func JSONSchemaDraft04() (*Schema, error) {
  44. b, err := Asset("jsonschema-draft-04.json")
  45. if err != nil {
  46. return nil, err
  47. }
  48. schema := new(Schema)
  49. if err := json.Unmarshal(b, schema); err != nil {
  50. return nil, err
  51. }
  52. return schema, nil
  53. }
  54. // MustLoadSwagger20Schema panics when Swagger20Schema returns an error
  55. func MustLoadSwagger20Schema() *Schema {
  56. d, e := Swagger20Schema()
  57. if e != nil {
  58. panic(e)
  59. }
  60. return d
  61. }
  62. // Swagger20Schema loads the swagger 2.0 schema from the embedded assets
  63. func Swagger20Schema() (*Schema, error) {
  64. b, err := Asset("v2/schema.json")
  65. if err != nil {
  66. return nil, err
  67. }
  68. schema := new(Schema)
  69. if err := json.Unmarshal(b, schema); err != nil {
  70. return nil, err
  71. }
  72. return schema, nil
  73. }