Sha256: 964ac001f65f2bbc78a747c416c1452ce02511929e11ee106d67840188735651

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module NxtSchema
  module Node
    class Collection < Node::Base
      def call
        apply_on_evaluators
        child_applications # build applications here so we can access them even when invalid
        return self if maybe_evaluator_applies?

        coerce_input
        validate_filled
        return self unless valid?

        child_applications.each_with_index do |item, index|
          current_application = item.call

          if !current_application.valid?
            merge_errors(current_application)
          else
            output[index] = current_application.output
          end
        end

        register_as_coerced_when_no_errors
        run_validations

        self
      end

      delegate :[], to: :child_applications

      private

      def validate_filled
        add_schema_error('is not allowed to be empty') if input.blank? && !maybe_evaluator_applies?
      end

      def child_applications
        @child_applications ||= begin
          return [] unless input.respond_to?(:each_with_index)

          input.each_with_index.map do |item, index|
            build_child_application(item, index)
          end
        end

      end

      def build_child_application(item, error_key)
        sub_node.build_application(input: item, context: context, parent: self, error_key: error_key)
      end

      def sub_node
        @sub_node ||= node.sub_nodes.values.first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nxt_schema-1.0.1 lib/nxt_schema/node/collection.rb