Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fail validation of cash letter control for settlementDate field if it is not a return type #185

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cashLetterControl.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (clc *CashLetterControl) String() string {

// Validate performs imagecashletter format rule checks on the record and returns an error if not Validated
// The first error encountered is returned and stops the parsing.
func (clc *CashLetterControl) Validate() error {
if err := clc.fieldInclusion(); err != nil {
func (clc *CashLetterControl) Validate(collectionTypeIndicator string) error {
if err := clc.fieldInclusion(collectionTypeIndicator); err != nil {
return err
}
if clc.recordType != "90" {
Expand All @@ -149,7 +149,7 @@ func (clc *CashLetterControl) Validate() error {

// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the Electronic Exchange will be returned.
func (clc *CashLetterControl) fieldInclusion() error {
func (clc *CashLetterControl) fieldInclusion(collectionTypeIndicator string) error {
if clc.recordType == "" {
return &FieldError{FieldName: "recordType",
Value: clc.recordType,
Expand All @@ -165,7 +165,8 @@ func (clc *CashLetterControl) fieldInclusion() error {
Value: clc.CashLetterTotalAmountField(),
Msg: msgFieldInclusion + ", did you use CashLetterControl()?"}
}
if clc.SettlementDate.IsZero() {
// If the type of the cash letter control is `Return`, we do not require to have this field present.
if clc.SettlementDate.IsZero() && !isReturnCollectionType(collectionTypeIndicator) {
return &FieldError{FieldName: "SettlementDate",
Value: clc.SettlementDate.String(),
Msg: msgFieldInclusion + ", did you use CashLetterControl()?"}
Expand Down Expand Up @@ -212,3 +213,10 @@ func (clc *CashLetterControl) CreditTotalIndicatorField() string {
func (clc *CashLetterControl) reservedField() string {
return clc.alphaField(clc.reserved, 14)
}

func isReturnCollectionType(code string) bool {
if code == "03" || code == "04" || code == "05" || code == "06" {
return true
}
return false
}
20 changes: 10 additions & 10 deletions cashLetterControl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func mockCashLetterControl() *CashLetterControl {
// TestMockCashLetterControl creates a CashLetterControl
func TestMockCashLetterControl(t *testing.T) {
clc := mockCashLetterControl()
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
t.Error("mockCashLetterControl does not validate and will break other tests: ", err)
}
if clc.recordType != "90" {
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestParseCashLetterControl(t *testing.T) {
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
err := r.parseCashLetterControl()
err := r.parseCashLetterControl("03")
if err != nil {
t.Errorf("%T: %s", err, err)
log.Fatal(err)
Expand Down Expand Up @@ -103,7 +103,7 @@ func testCLCString(t testing.TB) {
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
err := r.parseCashLetterControl()
err := r.parseCashLetterControl("01")
if err != nil {
t.Errorf("%T: %s", err, err)
log.Fatal(err)
Expand Down Expand Up @@ -131,7 +131,7 @@ func BenchmarkCLCString(b *testing.B) {
func TestCLCRecordType(t *testing.T) {
clc := mockCashLetterControl()
clc.recordType = "00"
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "recordType" {
t.Errorf("%T: %s", err, err)
Expand All @@ -144,7 +144,7 @@ func TestCLCRecordType(t *testing.T) {
func TestECEInstitutionName(t *testing.T) {
clc := mockCashLetterControl()
clc.ECEInstitutionName = "®©"
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "ECEInstitutionName" {
t.Errorf("%T: %s", err, err)
Expand All @@ -157,7 +157,7 @@ func TestECEInstitutionName(t *testing.T) {
func TestCLCCreditTotalIndicator(t *testing.T) {
clc := mockCashLetterControl()
clc.CreditTotalIndicator = 9
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "CreditTotalIndicator" {
t.Errorf("%T: %s", err, err)
Expand All @@ -170,7 +170,7 @@ func TestCLCCreditTotalIndicator(t *testing.T) {
func TestCLCFieldInclusionRecordType(t *testing.T) {
clc := mockCashLetterControl()
clc.recordType = ""
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "recordType" {
t.Errorf("%T: %s", err, err)
Expand All @@ -183,7 +183,7 @@ func TestCLCFieldInclusionRecordType(t *testing.T) {
func TestFieldInclusionCashLetterItemsCount(t *testing.T) {
clc := mockCashLetterControl()
clc.CashLetterItemsCount = 0
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "CashLetterItemsCount" {
t.Errorf("%T: %s", err, err)
Expand All @@ -196,7 +196,7 @@ func TestFieldInclusionCashLetterItemsCount(t *testing.T) {
func TestFieldInclusionCashLetterTotalAmount(t *testing.T) {
clc := mockCashLetterControl()
clc.CashLetterTotalAmount = 0
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "CashLetterTotalAmount" {
t.Errorf("%T: %s", err, err)
Expand All @@ -209,7 +209,7 @@ func TestFieldInclusionCashLetterTotalAmount(t *testing.T) {
func TestFieldInclusionRecordTypeSettlementDate(t *testing.T) {
clc := mockCashLetterControl()
clc.SettlementDate = time.Time{}
if err := clc.Validate(); err != nil {
if err := clc.Validate("01"); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "SettlementDate" {
t.Errorf("%T: %s", err, err)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
Expand Down
9 changes: 6 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ func (r *Reader) parseLine() error {
r.currentCashLetter.AddRoutingNumberSummary(r.currentCashLetter.currentRoutingNumberSummary)
r.currentCashLetter.currentRoutingNumberSummary = new(RoutingNumberSummary)
case cashLetterControlPos, cashLetterControlEbcPos:
if err := r.parseCashLetterControl(); err != nil {
// This is needed for validation od CashLetterControl since SettlementDate
// is a conditional field and is only available for certain types of CashLetters.
collectionTypeIndicator := r.currentCashLetter.CashLetterHeader.CollectionTypeIndicator
if err := r.parseCashLetterControl(collectionTypeIndicator); err != nil {
return err
}
if err := r.currentCashLetter.Validate(); err != nil {
Expand Down Expand Up @@ -664,15 +667,15 @@ func (r *Reader) parseRoutingNumberSummary() error {
}

// parseCashLetterControl takes the input record string and parses the CashLetterControl values
func (r *Reader) parseCashLetterControl() error {
func (r *Reader) parseCashLetterControl(collectionTypeIndicator string) error {
r.recordName = "CashLetterControl"
if r.currentCashLetter.CashLetterHeader == nil {
// CashLetterControl without a current CashLetter
return r.error(&FileError{Msg: msgFileCashLetterControl})
}
r.currentCashLetter.GetControl().Parse(r.decodeLine(r.line))
// Ensure valid CashLetterControl
if err := r.currentCashLetter.GetControl().Validate(); err != nil {
if err := r.currentCashLetter.GetControl().Validate(collectionTypeIndicator); err != nil {
return r.error(err)
}
return nil
Expand Down