-
Notifications
You must be signed in to change notification settings - Fork 40
/
financiaInstitution.go
76 lines (64 loc) · 2.35 KB
/
financiaInstitution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
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
IdentificationCode string `json:"identificationCode"`
// Identifier
Identifier string `json:"identifier"`
// Name
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
}