Skip to content

Commit 5c9a940

Browse files
committed
fix: ensure CompanyName, CompanyIdentification, and CompanyEntryDescription are non-zero
Issue: #1600
1 parent 2fac0a4 commit 5c9a940

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

batchHeader.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,27 @@ func (bh *BatchHeader) Validate() error {
316316
if err := bh.isAlphanumeric(bh.CompanyName); err != nil {
317317
return fieldError("CompanyName", err, bh.CompanyName)
318318
}
319+
if err := bh.isNonZero(bh.CompanyName); err != nil {
320+
return fieldError("CompanyName", err, bh.CompanyName)
321+
}
322+
319323
if err := bh.isAlphanumeric(bh.CompanyDiscretionaryData); err != nil {
320324
return fieldError("CompanyDiscretionaryData", err, bh.CompanyDiscretionaryData)
321325
}
326+
322327
if err := bh.isAlphanumeric(bh.CompanyIdentification); err != nil {
323328
return fieldError("CompanyIdentification", err, bh.CompanyIdentification)
324329
}
330+
if err := bh.isNonZero(bh.CompanyIdentification); err != nil {
331+
return fieldError("CompanyIdentification", err, bh.CompanyIdentification)
332+
}
333+
325334
if err := bh.isAlphanumeric(bh.CompanyEntryDescription); err != nil {
326335
return fieldError("CompanyEntryDescription", err, bh.CompanyEntryDescription)
327336
}
337+
if err := bh.isNonZero(bh.CompanyEntryDescription); err != nil {
338+
return fieldError("CompanyEntryDescription", err, bh.CompanyEntryDescription)
339+
}
328340
}
329341
return nil
330342
}

batchHeader_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ func BenchmarkBatchHeaderFieldInclusion(b *testing.B) {
269269
}
270270
}
271271

272+
func TestBatchHeader_CompanyIdentificationZeros(t *testing.T) {
273+
bh := mockBatchHeader()
274+
bh.CompanyIdentification = strings.Repeat("0", 10)
275+
err := bh.Validate()
276+
require.ErrorContains(t, err, "CompanyIdentification 0000000000 contains only spaces and zeros")
277+
}
278+
279+
func TestBatchHeader_CompanyNameZeros(t *testing.T) {
280+
bh := mockBatchHeader()
281+
bh.CompanyName = "000 000"
282+
err := bh.Validate()
283+
require.ErrorContains(t, err, "CompanyName 000 000 contains only spaces and zeros")
284+
}
285+
286+
func TestBatchHeader_CompanyEntryDescriptionZeros(t *testing.T) {
287+
bh := mockBatchHeader()
288+
bh.CompanyEntryDescription = " 000 "
289+
err := bh.Validate()
290+
require.ErrorContains(t, err, "CompanyEntryDescription 000 contains only spaces and zeros")
291+
}
292+
272293
// testBatchHeaderCompanyNameAlphaNumeric validates batch header company name is alphanumeric
273294
func testBatchHeaderCompanyNameAlphaNumeric(t testing.TB) {
274295
bh := mockBatchHeader()

validators.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strconv"
2626
"strings"
2727
"time"
28+
"unicode"
2829
"unicode/utf8"
2930
)
3031

@@ -435,6 +436,20 @@ func (v *validator) isAlphanumeric(s string) error {
435436
return nil
436437
}
437438

439+
var (
440+
ErrOnlyZeros = errors.New("contains only spaces and zeros")
441+
)
442+
443+
// isNonZero checks if a string is not blank and non-zero
444+
func (v *validator) isNonZero(s string) error {
445+
for _, r := range s {
446+
if !unicode.IsSpace(r) && r != '0' {
447+
return nil
448+
}
449+
}
450+
return ErrOnlyZeros
451+
}
452+
438453
// CalculateCheckDigit returns a check digit for a routing number
439454
// Multiply each digit in the Routing number by a weighting factor. The weighting factors for each digit are:
440455
// Position: 1 2 3 4 5 6 7 8

validators_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,49 @@ func TestValidators__isAlphanumericExamples(t *testing.T) {
227227
})
228228
}
229229

230+
func TestValidators__isNonZero(t *testing.T) {
231+
v := &validator{}
232+
233+
cases := []struct {
234+
input string
235+
err error
236+
}{
237+
{
238+
input: "",
239+
err: ErrOnlyZeros,
240+
},
241+
{
242+
input: " ",
243+
err: ErrOnlyZeros,
244+
},
245+
{
246+
input: "0",
247+
err: ErrOnlyZeros,
248+
},
249+
{
250+
input: "0 0",
251+
err: ErrOnlyZeros,
252+
},
253+
{
254+
input: " 000 ",
255+
err: ErrOnlyZeros,
256+
},
257+
// Valid cases
258+
{input: "abc"},
259+
{input: "000 123 a "},
260+
}
261+
for _, tc := range cases {
262+
t.Run(tc.input, func(t *testing.T) {
263+
err := v.isNonZero(tc.input)
264+
if tc.err == nil {
265+
require.NoError(t, err)
266+
} else {
267+
require.ErrorIs(t, tc.err, ErrOnlyZeros)
268+
}
269+
})
270+
}
271+
}
272+
230273
func TestValidators__validateJulianDay(t *testing.T) {
231274
empty := " "
232275
cases := map[string]string{

0 commit comments

Comments
 (0)