Skip to content

Commit

Permalink
financialInstitution: combine duplicate validation functions (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
atonks2 authored Feb 9, 2024
1 parent e86b8f9 commit 81329d3
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 188 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: '>= 1.20.4'
go-version: '>= 1.22.0'
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand Down Expand Up @@ -50,13 +50,13 @@ jobs:

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: '>= 1.20.4'
go-version: '>= 1.22.0'
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.14
go-version: ^1.22
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install make (Windows)
if: runner.os == 'Windows'
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.14
go-version: ^1.22
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Check
run: make check
Expand Down Expand Up @@ -57,9 +57,9 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.14
go-version: ^1.22
id: go

- name: Check out code into the Go module directory
Expand Down Expand Up @@ -122,9 +122,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.14
go-version: ^1.22
id: go

- name: Check out code into the Go module directory
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine as builder
FROM golang:1.22-alpine as builder
WORKDIR /go/src/github.com/moov-io/wire
RUN apk add -U --no-cache make bash gcc git
RUN adduser -D -g '' --shell /bin/false moov
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.webui
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder
WORKDIR /go/src/github.com/moov-io/wire
RUN apt-get update && apt-get upgrade -y && apt-get install make gcc g++
COPY . .
Expand Down
47 changes: 3 additions & 44 deletions beneficiaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type BeneficiaryFI struct {
// Financial Institution
FinancialInstitution FinancialInstitution `json:"financialInstitution,omitempty"`

// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
Expand Down Expand Up @@ -130,53 +128,14 @@ func (bfi *BeneficiaryFI) Format(options FormatOptions) string {
// Validate performs WIRE format rule checks on BeneficiaryFI and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (bfi *BeneficiaryFI) Validate() error {
if err := bfi.fieldInclusion(); err != nil {
return err
}
if bfi.tag != TagBeneficiaryFI {
return fieldError("tag", ErrValidTagForType, bfi.tag)
}
if err := bfi.isIdentificationCode(bfi.FinancialInstitution.IdentificationCode); err != nil {
return fieldError("IdentificationCode", err, bfi.FinancialInstitution.IdentificationCode)
}
// Can only be these Identification Codes
switch bfi.FinancialInstitution.IdentificationCode {
case
SWIFTBankIdentifierCode,
CHIPSParticipant,
DemandDepositAccountNumber,
FEDRoutingNumber,
CHIPSIdentifier:
default:
return fieldError("IdentificationCode", ErrIdentificationCode, bfi.FinancialInstitution.IdentificationCode)
}
if err := bfi.isAlphanumeric(bfi.FinancialInstitution.Identifier); err != nil {
return fieldError("Identifier", err, bfi.FinancialInstitution.Identifier)
}
if err := bfi.isAlphanumeric(bfi.FinancialInstitution.Name); err != nil {
return fieldError("Name", err, bfi.FinancialInstitution.Name)
}
if err := bfi.isAlphanumeric(bfi.FinancialInstitution.Address.AddressLineOne); err != nil {
return fieldError("AddressLineOne", err, bfi.FinancialInstitution.Address.AddressLineOne)
}
if err := bfi.isAlphanumeric(bfi.FinancialInstitution.Address.AddressLineTwo); err != nil {
return fieldError("AddressLineTwo", err, bfi.FinancialInstitution.Address.AddressLineTwo)
}
if err := bfi.isAlphanumeric(bfi.FinancialInstitution.Address.AddressLineThree); err != nil {
return fieldError("AddressLineThree", err, bfi.FinancialInstitution.Address.AddressLineThree)
}
return nil
}

// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (bfi *BeneficiaryFI) fieldInclusion() error {
if bfi.FinancialInstitution.IdentificationCode != "" && bfi.FinancialInstitution.Identifier == "" {
return fieldError("Identifier", ErrFieldRequired)
}
if bfi.FinancialInstitution.IdentificationCode == "" && bfi.FinancialInstitution.Identifier != "" {
return fieldError("IdentificationCode", ErrFieldRequired)
if err := bfi.FinancialInstitution.Validate(); err != nil {
return err
}

return nil
}

Expand Down
43 changes: 3 additions & 40 deletions beneficiaryIntermediaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type BeneficiaryIntermediaryFI struct {
// Financial Institution
FinancialInstitution FinancialInstitution `json:"financialInstitution,omitempty"`

// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
Expand Down Expand Up @@ -131,49 +129,14 @@ func (bifi *BeneficiaryIntermediaryFI) Format(options FormatOptions) string {
// The first error encountered is returned and stops that parsing.
// If ID Code is present, Identifier is mandatory and vice versa.
func (bifi *BeneficiaryIntermediaryFI) Validate() error {
if err := bifi.fieldInclusion(); err != nil {
return err
}
if bifi.tag != TagBeneficiaryIntermediaryFI {
return fieldError("tag", ErrValidTagForType, bifi.tag)
}
if err := bifi.isIdentificationCode(bifi.FinancialInstitution.IdentificationCode); err != nil {
return fieldError("IdentificationCode", err, bifi.FinancialInstitution.IdentificationCode)
}
// Can only be these Identification Codes
switch bifi.FinancialInstitution.IdentificationCode {
case
"B", "C", "D", "F", "U":
default:
return fieldError("IdentificationCode", ErrIdentificationCode, bifi.FinancialInstitution.IdentificationCode)
}
if err := bifi.isAlphanumeric(bifi.FinancialInstitution.Identifier); err != nil {
return fieldError("Identifier", err, bifi.FinancialInstitution.Identifier)
}
if err := bifi.isAlphanumeric(bifi.FinancialInstitution.Name); err != nil {
return fieldError("Name", err, bifi.FinancialInstitution.Name)
}
if err := bifi.isAlphanumeric(bifi.FinancialInstitution.Address.AddressLineOne); err != nil {
return fieldError("AddressLineOne", err, bifi.FinancialInstitution.Address.AddressLineOne)
}
if err := bifi.isAlphanumeric(bifi.FinancialInstitution.Address.AddressLineTwo); err != nil {
return fieldError("AddressLineTwo", err, bifi.FinancialInstitution.Address.AddressLineTwo)
}
if err := bifi.isAlphanumeric(bifi.FinancialInstitution.Address.AddressLineThree); err != nil {
return fieldError("AddressLineThree", err, bifi.FinancialInstitution.Address.AddressLineThree)
}
return nil
}

// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (bifi *BeneficiaryIntermediaryFI) fieldInclusion() error {
if bifi.FinancialInstitution.IdentificationCode != "" && bifi.FinancialInstitution.Identifier == "" {
return fieldError("BeneficiaryIntermediaryFI.FinancialInstitution.Identifier", ErrFieldRequired)
}
if bifi.FinancialInstitution.IdentificationCode == "" && bifi.FinancialInstitution.Identifier != "" {
return fieldError("BeneficiaryIntermediaryFI.FinancialInstitution.IdentificationCode", ErrFieldRequired)
if err := bifi.FinancialInstitution.Validate(); err != nil {
return err
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions beneficiaryIntermediaryFI_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestBeneficiaryIntermediaryFIIdentificationCodeRequired(t *testing.T) {

err := bifi.Validate()

require.EqualError(t, err, fieldError("BeneficiaryIntermediaryFI.FinancialInstitution.IdentificationCode", ErrFieldRequired).Error())
require.EqualError(t, err, fieldError("IdentificationCode", ErrFieldRequired).Error())
}

// TestBeneficiaryIntermediaryFIIdentifierRequired validates BeneficiaryIntermediaryFI Identifier is required
Expand All @@ -114,7 +114,7 @@ func TestBeneficiaryIntermediaryFIIdentifierRequired(t *testing.T) {

err := bifi.Validate()

require.EqualError(t, err, fieldError("BeneficiaryIntermediaryFI.FinancialInstitution.Identifier", ErrFieldRequired).Error())
require.EqualError(t, err, fieldError("Identifier", ErrFieldRequired).Error())
}

// TestParseBeneficiaryIntermediaryFIWrongLength parses a wrong BeneficiaryIntermediaryFI record length
Expand Down
59 changes: 59 additions & 0 deletions financiaInstitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

package wire

import (
"golang.org/x/exp/slices"
)

var (
financialInstitutionIDCodes = []string{
SWIFTBankIdentifierCode,
CHIPSParticipant,
DemandDepositAccountNumber,
FEDRoutingNumber,
CHIPSIdentifier,
}
)

// FinancialInstitution is demographic information for a financial institution
type FinancialInstitution struct {
// IdentificationCode: * `B` - SWIFT Bank Identifier Code (BIC) * `C` - CHIPS Participant * `D` - Demand Deposit Account (DDA) Number * `F` - Fed Routing Number * `T` - SWIFT BIC or Bank Entity Identifier (BEI) and Account Number * `U` - CHIPS Identifier
Expand All @@ -14,4 +28,49 @@ type FinancialInstitution struct {
Name string `json:"name"`
// Address
Address Address `json:"address"`

validator
}

func (fi FinancialInstitution) Validate() error {
if err := fi.fieldInclusion(); err != nil {
return err
}

// if ID Code is present, make sure it's a valid value
if fi.IdentificationCode != "" && !slices.Contains(financialInstitutionIDCodes, fi.IdentificationCode) {
return fieldError("IdentificationCode", ErrIdentificationCode, fi.IdentificationCode)
}

if err := fi.isAlphanumeric(fi.Identifier); err != nil {
return fieldError("Identifier", err, fi.Identifier)
}
if err := fi.isAlphanumeric(fi.Name); err != nil {
return fieldError("Name", err, fi.Name)
}
if err := fi.isAlphanumeric(fi.Address.AddressLineOne); err != nil {
return fieldError("AddressLineOne", err, fi.Address.AddressLineOne)
}
if err := fi.isAlphanumeric(fi.Address.AddressLineTwo); err != nil {
return fieldError("AddressLineTwo", err, fi.Address.AddressLineTwo)
}
if err := fi.isAlphanumeric(fi.Address.AddressLineThree); err != nil {
return fieldError("AddressLineThree", err, fi.Address.AddressLineThree)
}

return nil
}

func (fi FinancialInstitution) fieldInclusion() error {
// if Identifier is present, IdentificationCode must be provided.
if fi.Identifier != "" && fi.IdentificationCode == "" {
return fieldError("IdentificationCode", ErrFieldRequired)
}

// If IdentificationCode is present, Identifier must be present
if fi.IdentificationCode != "" && fi.Identifier == "" {
return fieldError("Identifier", ErrFieldRequired)
}

return nil
}
Loading

0 comments on commit 81329d3

Please sign in to comment.