Module: OpenApi::SchemaValidator
- Defined in:
- lib/open_api/schema_validator.rb,
lib/open_api/schema_validator/version.rb
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
- .draft_four ⇒ Object
- .draft_four_file ⇒ Object
- .draft_four_schema ⇒ Object
- .oas3 ⇒ Object
- .oas3_file ⇒ Object
- .oas3_schema ⇒ Object
- .swagger ⇒ Object
- .swagger_file ⇒ Object
- .swagger_schema ⇒ Object
- .validate!(json, version = 2) ⇒ Object
- .validate_schema!(schema, json, opts = {}) ⇒ Object
Class Method Details
.draft_four ⇒ Object
54 55 56 |
# File 'lib/open_api/schema_validator.rb', line 54 def self.draft_four @draft_four ||= JSON.parse(File.read(draft_four_file)) end |
.draft_four_file ⇒ Object
58 59 60 |
# File 'lib/open_api/schema_validator.rb', line 58 def self.draft_four_file File.('json_schemas/json_schema_v4.json', __dir__) end |
.draft_four_schema ⇒ Object
62 63 64 65 66 67 |
# File 'lib/open_api/schema_validator.rb', line 62 def self.draft_four_schema @draft_four_schema ||= JSON::Schema.new( draft_four, Addressable::URI.parse('http://json-schema.org/draft-04/schema#') ) end |
.oas3 ⇒ Object
24 25 26 |
# File 'lib/open_api/schema_validator.rb', line 24 def self.oas3 @oas3 ||= JSON.parse(File.read(oas3_file)) end |
.oas3_file ⇒ Object
28 29 30 |
# File 'lib/open_api/schema_validator.rb', line 28 def self.oas3_file File.('json_schemas/oas_v3_schema.json', __dir__) end |
.oas3_schema ⇒ Object
32 33 34 35 36 37 |
# File 'lib/open_api/schema_validator.rb', line 32 def self.oas3_schema @oas3_schema ||= JSON::Schema.new( oas3, Addressable::URI.parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v3.0/schema.json') ) end |
.swagger ⇒ Object
39 40 41 |
# File 'lib/open_api/schema_validator.rb', line 39 def self.swagger @swagger ||= JSON.parse(File.read(swagger_file)) end |
.swagger_file ⇒ Object
43 44 45 |
# File 'lib/open_api/schema_validator.rb', line 43 def self.swagger_file File.('json_schemas/swagger_schema.json', __dir__) end |
.swagger_schema ⇒ Object
47 48 49 50 51 52 |
# File 'lib/open_api/schema_validator.rb', line 47 def self.swagger_schema @swagger_schema ||= JSON::Schema.new( swagger, Addressable::URI.parse('http://swagger.io/v2/schema#') ) end |
.validate!(json, version = 2) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/open_api/schema_validator.rb', line 8 def self.validate!(json, version = 2) case version when 2 validate_schema!(swagger, json) when 3 validate_schema!(oas3, json) end end |
.validate_schema!(schema, json, opts = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/open_api/schema_validator.rb', line 17 def self.validate_schema!(schema, json, opts = {}) JSON::Validator.add_schema(draft_four_schema) JSON::Validator.add_schema(swagger_schema) JSON::Validator.add_schema(oas3_schema) JSON::Validator.validate!(schema, json, opts) end |