Sha256: 5110bbde9a673e4e21928688dadc643d5d96da72d8159b4e073b492d44bcb6a6

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require "dry/initializer"
require "dry/schema/constants"

module Dry
  module Schema
    # Applies rules defined within the DSL
    #
    # @api private
    class RuleApplier
      extend ::Dry::Initializer

      # @api private
      param :rules

      # @api private
      option :config, default: -> { Schema.config.dup }

      # @api private
      option :message_compiler, default: -> { MessageCompiler.new(Messages.setup(config.messages)) }

      # @api private
      def call(input)
        results = EMPTY_ARRAY.dup

        rules.each do |name, rule|
          next if input.error?(name)

          result = rule.(input.to_h)
          results << result if result.failure?
        end

        input.concat(results)
      end

      # @api private
      def to_ast
        if config.messages.namespace
          [:namespace, [config.messages.namespace, [:set, rules.values.map(&:to_ast)]]]
        else
          [:set, rules.values.map(&:to_ast)]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-1.14.0 lib/dry/schema/rule_applier.rb