Class: Explicit::Type::Array

Inherits:
Explicit::Type show all
Defined in:
lib/explicit/type/array.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(item_type:, empty: true) ⇒ Array

Returns a new instance of Array.



6
7
8
9
# File 'lib/explicit/type/array.rb', line 6

def initialize(item_type:, empty: true)
  @item_type = Explicit::Type.build(item_type)
  @empty = empty
end

Instance Attribute Details

#emptyObject (readonly)

Returns the value of attribute empty.



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

def empty
  @empty
end

#item_typeObject (readonly)

Returns the value of attribute item_type.



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

def item_type
  @item_type
end

Instance Method Details

#json_schema(flavour) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/explicit/type/array.rb', line 46

def json_schema(flavour)
  {
    type: "array",
    items: item_type.mcp_schema,
    minItems: empty ? 0 : 1
  }
end

#validate(values) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/explicit/type/array.rb', line 11

def validate(values)
  return error_i18n("array") if !values.is_a?(::Array)

  if values.empty? && !empty
    return error_i18n("empty")
  end

  validated = []

  values.each.with_index do |value, index|
    case item_type.validate(value)
    in [:ok, value]
      validated << value
    in [:error, error]
      return error_i18n("array_item", index:, error:)
    end
  end

  [:ok, validated]
end