Class: Explicit::Type::Literal

Inherits:
Explicit::Type show all
Defined in:
lib/explicit/type/literal.rb

Instance Attribute Summary collapse

Attributes inherited from Explicit::Type

#auth_type, #default, #description, #nilable, #param_location

Instance Method Summary collapse

Methods inherited from Explicit::Type

#auth_basic?, #auth_bearer?, build, #error_i18n, #mcp_schema, #merge_base_json_schema, #param_location_body?, #param_location_path?, #param_location_query?, #required?, #swagger_i18n, #swagger_schema

Constructor Details

#initialize(value:) ⇒ Literal

Returns a new instance of Literal.



6
7
8
9
10
11
12
# File 'lib/explicit/type/literal.rb', line 6

def initialize(value:)
  if !value.is_a?(::String) && !value.is_a?(::Integer)
    raise ArgumentError("literal must be a string or integer") 
  end

  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/explicit/type/literal.rb', line 4

def value
  @value
end

Instance Method Details

#json_schema(flavour) ⇒ Object



36
37
38
39
40
41
# File 'lib/explicit/type/literal.rb', line 36

def json_schema(flavour)
  {
    type: @value.is_a?(::String) ? "string" : "integer",
    enum: [@value]
  }
end

#validate(value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/explicit/type/literal.rb', line 14

def validate(value)
  if value == @value
    [:ok, value]
  else
    error_i18n("literal", value: @value.inspect)
  end
end