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

Class Method Details

.draft_fourObject



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_fileObject



58
59
60
# File 'lib/open_api/schema_validator.rb', line 58

def self.draft_four_file
  File.expand_path('json_schemas/json_schema_v4.json', __dir__)
end

.draft_four_schemaObject



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

.oas3Object



24
25
26
# File 'lib/open_api/schema_validator.rb', line 24

def self.oas3
  @oas3 ||= JSON.parse(File.read(oas3_file))
end

.oas3_fileObject



28
29
30
# File 'lib/open_api/schema_validator.rb', line 28

def self.oas3_file
  File.expand_path('json_schemas/oas_v3_schema.json', __dir__)
end

.oas3_schemaObject



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

.swaggerObject



39
40
41
# File 'lib/open_api/schema_validator.rb', line 39

def self.swagger
  @swagger ||= JSON.parse(File.read(swagger_file))
end

.swagger_fileObject



43
44
45
# File 'lib/open_api/schema_validator.rb', line 43

def self.swagger_file
  File.expand_path('json_schemas/swagger_schema.json', __dir__)
end

.swagger_schemaObject



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