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
|