Class: Datacaster::AndWithErrorAggregationNode

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/and_with_error_aggregation_node.rb

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #json_schema, #json_schema_attributes, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(left, right) ⇒ AndWithErrorAggregationNode

Returns a new instance of AndWithErrorAggregationNode.



3
4
5
6
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 3

def initialize(left, right)
  @left = left
  @right = right
end

Instance Method Details

#cast(object, runtime:) ⇒ Object

Works like AndNode, but doesn’t stop at first error — in order to aggregate all Failures Makes sense only for Hash Schemas



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 10

def cast(object, runtime:)
  left_result = @left.with_runtime(runtime).(object)

  if left_result.valid?
    @right.with_runtime(runtime).(left_result.value)
  else
    right_result = @right.with_runtime(runtime).(object)
    if right_result.valid?
      left_result
    else
      Datacaster.ErrorResult(Utils.merge_errors(left_result.raw_errors, right_result.raw_errors))
    end
  end
end

#inspectObject



39
40
41
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 39

def inspect
  "#<Datacaster::AndWithErrorAggregationNode L: #{@left.inspect} R: #{@right.inspect}>"
end

#to_json_schemaObject



26
27
28
29
30
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 26

def to_json_schema
  [@left, @right].reduce(JsonSchemaResult.new) do |result, caster|
    result.apply(caster.to_json_schema)
  end
end

#to_json_schema_attributesObject



32
33
34
35
36
37
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 32

def to_json_schema_attributes
  super.merge(
    required:
      [@left, @right].any? { |caster| caster.to_json_schema_attributes[:required] }
  )
end