Class: Jsapi::Meta::Schema::Base

Inherits:
Model::Base show all
Includes:
OpenAPI::Extensions
Defined in:
lib/jsapi/meta/schema/base.rb

Direct Known Subclasses

Array, Boolean, Numeric, Object, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

included

Methods inherited from Model::Base

#inspect, #merge!, #reference?, #resolve

Methods included from Model::Attributes

#attributes_frozen?, #freeze_attributes, included

Constructor Details

#initialize(keywords = {}) ⇒ Base

Creates a new schema.



59
60
61
62
63
64
65
66
# File 'lib/jsapi/meta/schema/base.rb', line 59

def initialize(keywords = {})
  keywords = keywords.dup
  add_example(keywords.delete(:example)) if keywords.key?(:example)

  @type = keywords.delete(:type)
  @validations = {}
  super(keywords)
end

Instance Attribute Details

#validationsObject (readonly)

The validations.



56
57
58
# File 'lib/jsapi/meta/schema/base.rb', line 56

def validations
  @validations
end

Instance Method Details

#defaultObject

:attr: default The default value.



14
# File 'lib/jsapi/meta/schema/base.rb', line 14

attribute :default

#deprecatedObject

:attr: deprecated Specifies whether the schema is marked as deprecated.



19
# File 'lib/jsapi/meta/schema/base.rb', line 19

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The description of the schema.



24
# File 'lib/jsapi/meta/schema/base.rb', line 24

attribute :description, ::String

#enumObject

:attr: enum The allowed values.



29
# File 'lib/jsapi/meta/schema/base.rb', line 29

attribute :enum, accessors: %i[reader]

#enum=(value) ⇒ Object

:nodoc:



72
73
74
75
76
77
# File 'lib/jsapi/meta/schema/base.rb', line 72

def enum=(value) # :nodoc:
  try_modify_attribute!(:enum) do
    add_validation('enum', Validation::Enum.new(value))
    @enum = value
  end
end

#examplesObject

:attr: examples The samples matching the schema.



34
# File 'lib/jsapi/meta/schema/base.rb', line 34

attribute :examples, []

#existenceObject

:attr: existence The level of Existence. The default level of existence is ALLOW_OMITTED.



44
# File 'lib/jsapi/meta/schema/base.rb', line 44

attribute :existence, Existence, default: Existence::ALLOW_OMITTED

#external_docsObject

:attr: external_docs The ExternalDocumentation object.



39
# File 'lib/jsapi/meta/schema/base.rb', line 39

attribute :external_docs, ExternalDocumentation

#nullable?Boolean

Returns true if and only if values can be null as specified by JSON Schema.

Returns:



80
81
82
# File 'lib/jsapi/meta/schema/base.rb', line 80

def nullable?
  existence <= Existence::ALLOW_NIL
end

#omittable?Boolean

Returns true if and only if values can be omitted.

Returns:



85
86
87
# File 'lib/jsapi/meta/schema/base.rb', line 85

def omittable?
  existence <= Existence::ALLOW_OMITTED
end

#titleObject

:attr: title The title of the schema.



49
# File 'lib/jsapi/meta/schema/base.rb', line 49

attribute :title, String

#to_json_schemaObject

Returns a hash representing the JSON Schema object.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jsapi/meta/schema/base.rb', line 90

def to_json_schema
  {
    type: nullable? ? [type, 'null'] : type,
    title: title,
    description: description,
    default: default,
    examples: examples.presence,
    deprecated: deprecated?.presence
  }.tap do |result|
    validations.each_value do |validation|
      result.merge!(validation.to_json_schema_validation)
    end
  end.compact
end

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jsapi/meta/schema/base.rb', line 106

def to_openapi(version, *)
  version = OpenAPI::Version.from(version)

  with_openapi_extensions(
    if version.major == 2
      # OpenAPI 2.0
      {
        type: type,
        example: examples.first
      }
    elsif version.minor.zero?
      # OpenAPI 3.0
      {
        type: type,
        nullable: nullable?.presence,
        examples: examples.presence,
        deprecated: deprecated?.presence
      }
    else
      # OpenAPI 3.1 and higher
      {
        type: nullable? ? [type, 'null'] : type,
        examples: examples.presence,
        deprecated: deprecated?.presence
      }
    end.tap do |result|
      result[:title] = title
      result[:description] = description
      result[:default] = default
      result[:externalDocs] = external_docs&.to_openapi

      validations.each_value do |validation|
        result.merge!(validation.to_openapi_validation(version))
      end
    end
  )
end

#typeObject

:nodoc:



144
145
146
# File 'lib/jsapi/meta/schema/base.rb', line 144

def type # :nodoc:
  @type ||= self.class.name.demodulize.downcase
end