Sha256: 5ddd3e88c98ce2bc301ba442b88af7311a152207d9e040340ec307bef9224d28

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module Dry
  module Validation
    class Result
      include Dry::Equalizer(:output, :messages)
      include Enumerable

      attr_reader :output
      attr_reader :errors
      attr_reader :error_compiler
      attr_reader :hint_compiler

      alias_method :to_hash, :output

      EMPTY_MESSAGES = {}.freeze

      def initialize(output, errors, error_compiler, hint_compiler)
        @output = output
        @errors = errors
        @error_compiler = error_compiler
        @hint_compiler = hint_compiler
      end

      def each(&block)
        output.each(&block)
      end

      def [](name)
        output.fetch(name)
      end

      def success?
        errors.empty?
      end

      def failure?
        !success?
      end

      def messages(options = {})
        @messages ||=
          begin
            return EMPTY_MESSAGES if success?

            hints = hint_compiler.with(options).call
            comp = error_compiler.with(options.merge(hints: hints))

            comp.(error_ast)
          end
      end

      def to_ast
        [:set, error_ast]
      end

      private

      def error_ast
        errors.map { |error| error.to_ast }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-validation-0.7.2 lib/dry/validation/result.rb
dry-validation-0.7.1 lib/dry/validation/result.rb
dry-validation-0.7.0 lib/dry/validation/result.rb