Class: ActionSpec::Schema::ArrayOf

Inherits:
Base
  • Object
show all
Defined in:
lib/action_spec/schema/array_of.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#blank, #default, #description, #enum, #example, #examples, #length, #pattern, #range

Instance Method Summary collapse

Methods inherited from Base

#blank_allowed?, #blank_value, #materialize_missing, #validate_constraints

Constructor Details

#initialize(item, options = {}) ⇒ ArrayOf

Returns a new instance of ArrayOf.



8
9
10
11
# File 'lib/action_spec/schema/array_of.rb', line 8

def initialize(item, options = {})
  super(options)
  @item = item
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



6
7
8
# File 'lib/action_spec/schema/array_of.rb', line 6

def item
  @item
end

Instance Method Details

#cast(value, context:, coerce:, result:, path:, field: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/action_spec/schema/array_of.rb', line 13

def cast(value, context:, coerce:, result:, path:, field: nil)
  unless value.is_a?(Array)
    if field
      field.add_error(result, path:, type: :invalid, value:, context:)
    else
      result.add_error(path.join("."), :invalid)
    end
    return []
  end

  output = value.each_with_index.map do |entry, index|
    item.cast(entry, context:, coerce:, result:, path: [*path, index], field: nil)
  end
  validate_constraints(output, result:, path:, field:, context:)
  output
end

#copyObject



30
31
32
# File 'lib/action_spec/schema/array_of.rb', line 30

def copy
  self.class.new(item.copy, default:, enum:, range:, pattern:, length:, blank:, desc: description, example:, examples:)
end

#custom_validation?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/action_spec/schema/array_of.rb', line 34

def custom_validation?
  @custom_validation ||= item.custom_validation?
end