Sha256: 51702ef0959aecd9cae5c09bc3cbd2fb1f5ef4fcd6f758a4670660b0355e0beb

Contents?: true

Size: 925 Bytes

Versions: 15

Compression:

Stored size: 925 Bytes

Contents

# frozen_string_literal: true

module LedgerSync
  module Util
    class Validator
      attr_reader :data,
                  :contract

      def initialize(contract:, data:)
        @data = data
        @contract = contract
      end

      def errors
        @errors ||= call_contract.errors
      end

      def full_messages
        @full_messages ||= errors.messages.map { |e| "#{e.path.map(&:to_s).join(' ')} #{e.text}" }
      end

      def message
        @message ||= full_messages.first
      end

      def valid?
        call_contract.success?
      end

      def validate
        return success if valid?

        failure
      end

      private

      def call_contract
        @call_contract ||= contract.new.call(data)
      end

      def failure
        ValidationResult.Failure(validator: self)
      end

      def success
        ValidationResult.Success(validator: self)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ledger_sync-2.0.2 lib/ledger_sync/util/validator.rb
ledger_sync-2.0.1 lib/ledger_sync/util/validator.rb
ledger_sync-2.0.0 lib/ledger_sync/util/validator.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/util/validator.rb
ledger_sync-1.8.1 lib/ledger_sync/util/validator.rb
ledger_sync-1.8.0 lib/ledger_sync/util/validator.rb
ledger_sync-1.7.0 lib/ledger_sync/util/validator.rb
ledger_sync-1.6.0 lib/ledger_sync/util/validator.rb
ledger_sync-1.5.2 lib/ledger_sync/util/validator.rb
ledger_sync-1.5.1 lib/ledger_sync/util/validator.rb
ledger_sync-1.5.0 lib/ledger_sync/util/validator.rb
ledger_sync-1.4.4 lib/ledger_sync/util/validator.rb
ledger_sync-1.4.2 lib/ledger_sync/util/validator.rb
ledger_sync-1.4.1 lib/ledger_sync/util/validator.rb
ledger_sync-1.4.0 lib/ledger_sync/util/validator.rb