Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 7fd5cd1

Browse files
committed
Use macro for account number
1 parent d983493 commit 7fd5cd1

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

lib/mt9.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
require "dry-validation"
44
require "fixy"
55

6-
require_relative "mt9/base_record"
7-
require_relative "mt9/header_record"
8-
require_relative "mt9/version"
6+
require "mt9/values"
7+
require "mt9/validators/base_contract"
8+
require "mt9/validators/header_record_contract"
9+
require "mt9/base_record"
10+
require "mt9/header_record"
11+
require "mt9/version"
912

1013
module MT9
1114
class ValidationError < StandardError

lib/mt9/header_record.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require_relative "validators/header_record_contract"
4-
53
module MT9
64
class HeaderRecord < BaseRecord
75
set_line_ending Fixy::Record::LINE_ENDING_CRLF
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
module Validators
5+
class BaseContract < Dry::Validation::Contract
6+
register_macro(:is_account_number?) do
7+
key.failure("must be 15 or 16 numeric characters") unless /^(\d{15}|\d{16})$/.match(value)
8+
key.failure("must not start with a reserved bank code") if value.start_with?(*MT9::Values::RESERVED_BANK_CODES)
9+
end
10+
end
11+
end
12+
end

lib/mt9/validators/header_record_contract.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22

33
module MT9
44
module Validators
5-
class HeaderRecordContract < Dry::Validation::Contract
6-
RESERVED_BANK_CODES = %w[99].freeze
7-
8-
DIRECT_CREDIT = "12"
9-
DIRECT_DEBIT = "20"
10-
FILE_TYPES = [DIRECT_CREDIT, DIRECT_DEBIT].freeze
11-
5+
class HeaderRecordContract < BaseContract
126
schema do
13-
required(:file_type).filled(:string, included_in?: FILE_TYPES)
7+
required(:file_type).filled(:string, included_in?: MT9::Values::FILE_TYPES)
148
required(:account_number).filled(:string)
159
required(:due_date).filled(:string)
1610
optional(:client_short_name).value(:string, size?: 0..20)
1711
end
1812

19-
rule(:account_number) do
20-
key.failure("must be 15 or 16 numeric characters") unless /^(\d{15}|\d{16})$/.match(value)
21-
key.failure("must not start with a reserved bank code") if value.start_with?(*RESERVED_BANK_CODES)
22-
end
13+
rule(:account_number).validate(:is_account_number?)
2314

2415
rule(:due_date) do
2516
key.failure("must be 6 or 8 numeric characters") unless /^(\d{6}|\d{8})$/.match(value)

lib/mt9/values.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
module Values
5+
RESERVED_BANK_CODES = %w[99].freeze
6+
7+
DIRECT_CREDIT = "12"
8+
DIRECT_DEBIT = "20"
9+
FILE_TYPES = [DIRECT_CREDIT, DIRECT_DEBIT].freeze
10+
end
11+
end

0 commit comments

Comments
 (0)