Sha256: 141c09a81134d9cb9aa5e9af96cb6d42990a3383bed70d953f17dd2512568464

Contents?: true

Size: 1.7 KB

Versions: 13

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require "dry/schema/path"
require "dry/validation/constants"

module Dry
  module Validation
    # Failure accumulator object
    #
    # @api public
    class Failures
      # The path for messages accumulated by failures object
      #
      # @return [Dry::Schema::Path]
      #
      # @api private
      attr_reader :path

      # Options for messages
      #
      # These options are used by MessageResolver
      #
      # @return [Hash]
      #
      # @api private
      attr_reader :opts

      # @api private
      def initialize(path = ROOT_PATH)
        @path = Dry::Schema::Path[path]
        @opts = EMPTY_ARRAY.dup
      end

      # Set failure
      #
      # @overload failure(message)
      #   Set message text explicitly
      #   @param message [String] The message text
      #   @example
      #     failure('this failed')
      #
      # @overload failure(id)
      #   Use message identifier (needs localized messages setup)
      #   @param id [Symbol] The message id
      #   @example
      #     failure(:taken)
      #
      # @overload failure(meta_hash)
      #   Use meta_hash[:text] as a message (either explicitely or as an identifier),
      #   setting the rest of the hash as error meta attribute
      #   @param meta [Hash] The hash containing the message as value for the :text key
      #   @example
      #     failure({text: :invalid, key: value})
      #
      # @see Evaluator#key
      # @see Evaluator#base
      #
      # @api public
      def failure(message, tokens = EMPTY_HASH)
        opts << {message: message, tokens: tokens, path: path}
        self
      end

      # @api private
      def empty?
        opts.empty?
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-validation-1.10.0 lib/dry/validation/failures.rb
dry-validation-1.9.0 lib/dry/validation/failures.rb
dry-validation-1.8.1 lib/dry/validation/failures.rb
dry-validation-1.8.0 lib/dry/validation/failures.rb
dry-validation-1.7.0 lib/dry/validation/failures.rb
dry-validation-1.6.0 lib/dry/validation/failures.rb
dry-validation-1.5.6 lib/dry/validation/failures.rb
dry-validation-1.5.5 lib/dry/validation/failures.rb
dry-validation-1.5.4 lib/dry/validation/failures.rb
dry-validation-1.5.3 lib/dry/validation/failures.rb
dry-validation-1.5.2 lib/dry/validation/failures.rb
dry-validation-1.5.1 lib/dry/validation/failures.rb
dry-validation-1.5.0 lib/dry/validation/failures.rb