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

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/schema/base.rb

Direct Known Subclasses

Array, Boolean, Numeric, Object, String

Constant Summary collapse

TYPES =

:nodoc:

%w[array boolean integer number object string].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#inspect, #reference?, #resolve

Methods included from Attributes::ClassMethods

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Base

Creates a new schema.



62
63
64
65
66
67
68
69
# File 'lib/jsapi/meta/schema/base.rb', line 62

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.



59
60
61
# File 'lib/jsapi/meta/schema/base.rb', line 59

def validations
  @validations
end

Instance Method Details

#defaultObject

:attr: default The optional default value.



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

attribute :default

#deprecatedObject

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



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

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The optional description of the schema.



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

attribute :description, ::String

#enumObject

:attr: enum The allowed values.



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

attribute :enum, writer: false

#enum=(value) ⇒ Object

:nodoc:



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

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

#examplesObject

:attr: examples The optional examples.



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

attribute :examples, [::Object]

#existenceObject

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



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

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

#external_docsObject

:attr: external_docs The optional OpenAPI::ExternalDocumentation object.



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

attribute :external_docs, OpenAPI::ExternalDocumentation

#nullable?Boolean

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

Returns:



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

def nullable?
  existence <= Existence::ALLOW_NIL
end

#titleObject

:attr: title The optional title of the schema.



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

attribute :title, String

#to_json_schemaObject

Returns a hash representing the JSON Schema object.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsapi/meta/schema/base.rb', line 83

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.



99
100
101
102
103
104
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
# File 'lib/jsapi/meta/schema/base.rb', line 99

def to_openapi(version)
  version = OpenAPI::Version.from(version)
  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,
      deprecated: deprecated?.presence
    }
  else
    # OpenAPI 3.1
    {
      type: nullable? ? [type, 'null'] : type,
      examples: examples,
      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.compact
end

#typeObject

:nodoc:



134
135
136
# File 'lib/jsapi/meta/schema/base.rb', line 134

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