spec.go 2.2 KB

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