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

Inherits:
Base::Model 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 Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

#add_openapi_extension, #openapi_extensions, #openapi_extensions=

Methods inherited from Base::Model

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

Methods included from Base::Attributes

#attribute, #attribute_names

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.



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.



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

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

#enum=(value) ⇒ Object

:nodoc:



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

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:



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

def nullable?
  existence <= Existence::ALLOW_NIL
end

#omittable?Boolean

Returns true if and only if values can be omitted.

Returns:



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

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.



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

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

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



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

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 |hash|
      hash[:title] = title
      hash[:description] = description
      hash[:default] = default
      hash[:externalDocs] = external_docs&.to_openapi

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

#typeObject

:nodoc:



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

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