Class: Jsapi::Meta::Schema::Base
- Inherits:
-
Model::Base
- Object
- Model::Base
- Jsapi::Meta::Schema::Base
- Includes:
- OpenAPI::Extensions
- Defined in:
- lib/jsapi/meta/schema/base.rb
Constant Summary
Constants included from Model::Attributes
Model::Attributes::DEFAULT_ARRAY, Model::Attributes::DEFAULT_HASH
Instance Attribute Summary collapse
-
#validations ⇒ Object
readonly
The validations.
Instance Method Summary collapse
-
#default ⇒ Object
:attr: default The default value.
-
#default_value(definitions = nil, context: nil) ⇒ Object
Returns the default value within
context
. -
#deprecated ⇒ Object
:attr: deprecated Specifies whether or not the schema is deprecated.
-
#description ⇒ Object
:attr: description The description of the schema.
-
#enum ⇒ Object
:attr: enum The allowed values.
-
#enum=(value) ⇒ Object
:nodoc:.
-
#examples ⇒ Object
:attr: examples The samples matching the schema.
-
#existence ⇒ Object
:attr: existence The level of Existence.
-
#external_docs ⇒ Object
:attr: external_docs The OpenAPI::ExternalDocumentation object.
-
#initialize(keywords = {}) ⇒ Base
constructor
Creates a new schema.
-
#nullable? ⇒ Boolean
Returns true if and only if values can be
null
as specified by JSON Schema. -
#omittable? ⇒ Boolean
Returns true if and only if values can be omitted.
-
#title ⇒ Object
:attr: title The title of the schema.
-
#to_json_schema ⇒ Object
Returns a hash representing the JSON Schema object.
-
#to_openapi(version) ⇒ Object
Returns a hash representing the OpenAPI schema object.
-
#type ⇒ Object
:nodoc:.
Methods included from OpenAPI::Extensions
Methods inherited from Model::Base
#inspect, #merge!, #reference?, #resolve
Methods included from Model::Attributes
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
#validations ⇒ Object (readonly)
The validations.
54 55 56 |
# File 'lib/jsapi/meta/schema/base.rb', line 54 def validations @validations end |
Instance Method Details
#default ⇒ Object
: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 |
#deprecated ⇒ Object
: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] |
#description ⇒ Object
:attr: description The description of the schema.
22 |
# File 'lib/jsapi/meta/schema/base.rb', line 22 attribute :description, ::String |
#enum ⇒ Object
: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 |
#examples ⇒ Object
:attr: examples The samples matching the schema.
32 |
# File 'lib/jsapi/meta/schema/base.rb', line 32 attribute :examples, [] |
#existence ⇒ Object
: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_docs ⇒ Object
: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.
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.
84 85 86 |
# File 'lib/jsapi/meta/schema/base.rb', line 84 def omittable? existence <= Existence::ALLOW_OMITTED end |
#title ⇒ Object
:attr: title The title of the schema.
47 |
# File 'lib/jsapi/meta/schema/base.rb', line 47 attribute :title, String |
#to_json_schema ⇒ Object
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 |
#type ⇒ Object
:nodoc:
143 144 145 |
# File 'lib/jsapi/meta/schema/base.rb', line 143 def type # :nodoc: @type ||= self.class.name.demodulize.downcase end |