Class: JSONSchemer::OpenAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schemer/openapi.rb

Instance Method Summary collapse

Constructor Details

#initialize(document, **options) ⇒ OpenAPI

Returns a new instance of OpenAPI.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json_schemer/openapi.rb', line 4

def initialize(document, **options)
  @document = document

  version = document['openapi']
  case version
  when /\A3\.1\.\d+\z/
    @document_schema = JSONSchemer.openapi31_document
    json_schema_dialect = document.fetch('jsonSchemaDialect') { OpenAPI31::BASE_URI.to_s }
  when /\A3\.0\.\d+\z/
    @document_schema = JSONSchemer.openapi30_document
    json_schema_dialect = OpenAPI30::BASE_URI.to_s
  else
    raise UnsupportedOpenAPIVersion, version
  end

  meta_schema = META_SCHEMAS_BY_BASE_URI_STR[json_schema_dialect] || raise(UnsupportedMetaSchema, json_schema_dialect)

  @schema = JSONSchemer.schema(@document, :meta_schema => meta_schema, **options)
end

Instance Method Details

#ref(value) ⇒ Object



32
33
34
# File 'lib/json_schemer/openapi.rb', line 32

def ref(value)
  @schema.ref(value)
end

#schema(name) ⇒ Object



36
37
38
# File 'lib/json_schemer/openapi.rb', line 36

def schema(name)
  ref("#/components/schemas/#{name}")
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/json_schemer/openapi.rb', line 24

def valid?
  @document_schema.valid?(@document)
end

#validate(**options) ⇒ Object



28
29
30
# File 'lib/json_schemer/openapi.rb', line 28

def validate(**options)
  @document_schema.validate(@document, **options)
end