File: openapi.rb

package info (click to toggle)
ruby-json-schemer 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,428; makefile: 4; sh: 4
file content (38 lines) | stat: -rw-r--r-- 948 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
module JSONSchemer
  class OpenAPI
    def initialize(document, **options)
      @document = document

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

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

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

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

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

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