Sha256: 142a379bfb847a2f8837e2136f34b2a8a3d716e8f1af08e15a2713226fc1d705

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module FlexValidations
  class Or
    include Validation

    def initialize(*validations)
      @validations = validations
    end

    # @param value [Object] Value to be validated
    #
    # @return [FlexValidations::Result]
    def validate(value)
      fails = []

      @validations.each do |validation|
        res = validation.validate(value)

        return Result::Success::Composite.new(self, validation, res.message, value, res.raw) if res.success?

        fails.push(res)
      end

      Result::Fail::Simple.new(self, FailedMessage.new(fails), value, value)
    end

    # @return [String]
    def to_s
      "any of validation should succeed:\n#{IndentedString.new(List.new(@validations))}"
    end

    class FailedMessage
      include ResultMessage

      def initialize(fails)
        @fails = fails
      end

      def to_s
        "all validations failed:\n#{IndentedString.new(List.new(@fails))}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flex_validations-0.1.0 lib/flex_validations/or.rb