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