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

Constant Summary

Constants included from Model::Attributes

Model::Attributes::DEFAULT_ARRAY, Model::Attributes::DEFAULT_HASH

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

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Base

Creates a new schema.



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

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.



54
55
56
# File 'lib/jsapi/meta/schema/base.rb', line 54

def validations
  @validations
end

Instance Method Details

#defaultObject

:attr: default The default value.



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

attribute :default

#default_value(definitions = nil, context: nil) ⇒ Object

Returns the default value within context.



67
68
69
70
71
# File 'lib/jsapi/meta/schema/base.rb', line 67

def default_value(definitions = nil, context: nil)
  return default unless default.nil?

  definitions&.default_value(type, context: context)
end

#deprecatedObject

:attr: deprecated Specifies whether or not the schema is deprecated.



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

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The description of the schema.



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

attribute :description, ::String

#enumObject

:attr: enum The allowed values.



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

attribute :enum, accessors: %i[reader]

#enum=(value) ⇒ Object

:nodoc:



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

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

#examplesObject

:attr: examples The samples matching the schema.



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

attribute :examples, []

#existenceObject

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



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

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

#external_docsObject

:attr: external_docs The OpenAPI::ExternalDocumentation object.



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

attribute :external_docs, ExternalDocumentation

#nullable?Boolean

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

Returns:



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

def nullable?
  existence <= Existence::ALLOW_NIL
end

#omittable?Boolean

Returns true if and only if values can be omitted.

Returns:



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

def omittable?
  existence <= Existence::ALLOW_OMITTED
end

#titleObject

:attr: title The title of the schema.



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

attribute :title, String

#to_json_schemaObject

Returns a hash representing the JSON Schema object.



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

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.



105
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
# File 'lib/jsapi/meta/schema/base.rb', line 105

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
      {
        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:



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

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