Module: OpenApi::SchemaValidator

Defined in:
lib/open_api/schema_validator.rb,
lib/open_api/schema_validator/version.rb

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.draft_fourObject



33
34
35
# File 'lib/open_api/schema_validator.rb', line 33

def self.draft_four
  @draft_four ||= JSON.parse(File.read(draft_four_file))
end

.draft_four_fileObject



37
38
39
# File 'lib/open_api/schema_validator.rb', line 37

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

.draft_four_schemaObject



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

def self.draft_four_schema
  @draft_four_schema ||= JSON::Schema.new(
    draft_four,
    Addressable::URI.parse('http://json-schema.org/draft-04/schema#')
  )
end

.swaggerObject



18
19
20
# File 'lib/open_api/schema_validator.rb', line 18

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

.swagger_fileObject



22
23
24
# File 'lib/open_api/schema_validator.rb', line 22

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

.swagger_schemaObject



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

def self.swagger_schema
  @swagger_schema ||= JSON::Schema.new(
    swagger,
    Addressable::URI.parse('http://swagger.io/v2/schema#')
  )
end

.validate!(json) ⇒ Object



8
9
10
# File 'lib/open_api/schema_validator.rb', line 8

def self.validate!(json)
  validate_schema!(swagger, json)
end

.validate_schema!(schema, json, opts = {}) ⇒ Object



12
13
14
15
16
# File 'lib/open_api/schema_validator.rb', line 12

def self.validate_schema!(schema, json, opts = {})
  JSON::Validator.add_schema(draft_four_schema)
  JSON::Validator.add_schema(swagger_schema)
  JSON::Validator.validate!(schema, json, opts)
end