Skip to content

Commit

Permalink
Merge pull request #1451 from hshih-lead/amount-validate-opt
Browse files Browse the repository at this point in the history
feat: Added validation option to allow $0 entry amount
  • Loading branch information
adamdecaf authored Jul 29, 2024
2 parents e69ab46 + 25fec51 commit 79da155
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,9 @@ func (batch *Batch) ValidAmountForCodes(entry *EntryDetail) error {
return fieldError("Amount", ErrBatchAmountNonZero, entry.Amount)
} else {
if entry.Amount == 0 {
if batch.validateOpts != nil && batch.validateOpts.AllowZeroEntryAmount {
return nil
}
return fieldError("Amount", ErrBatchAmountZero, entry.Amount)
}
}
Expand Down
17 changes: 17 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,23 @@ func TestBatch__ValidAmountForCodes(t *testing.T) {
})
}

func TestBatch_ValidAmountForCodes_AllowZeroEntryAmount(t *testing.T) {
b1 := mockBatchWEB(t)

// Standard EntryDetails should pass
require.NoError(t, b1.Create())

// Verify a zero amount fails
b1.Entries[0].Amount = 0
require.ErrorContains(t, b1.Create(), ErrBatchAmountZero.Error())

// Bypass zero amount check
b1.SetValidation(&ValidateOpts{
AllowZeroEntryAmount: true,
})
require.NoError(t, b1.Create())
}

func TestBatch_AllowInvalidAmounts(t *testing.T) {
bh := &BatchHeader{
OriginatorStatusCode: 1,
Expand Down
3 changes: 3 additions & 0 deletions docs/custom-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ UnequalAddendaCounts bool `json:"unequalAddendaCounts"`
```
// AllowInvalidAmounts will skip verifying the Amount is valid for the TransactionCode and entry type.
AllowInvalidAmounts bool `json:"allowInvalidAmounts"`
// AllowZeroEntryAmount will skip enforcing the entry Amount to be non-zero.
AllowZeroEntryAmount bool `json:"allowZeroEntryAmount"`
```

### File Header
Expand Down
3 changes: 3 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ type ValidateOpts struct {

// AllowInvalidAmounts will skip verifying the Amount is valid for the TransactionCode and entry type.
AllowInvalidAmounts bool `json:"allowInvalidAmounts"`

// AllowZeroEntryAmount will skip enforcing the entry Amount to be non-zero
AllowZeroEntryAmount bool `json:"allowZeroEntryAmount"`
}

// merge will combine two ValidateOpts structs and keep any non-zero field values.
Expand Down
4 changes: 4 additions & 0 deletions server/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
unequalAddendaCounts = "unequalAddendaCounts"
preserveSpaces = "preserveSpaces"
allowInvalidAmounts = "allowInvalidAmounts"
allowZeroEntryAmount = "allowZeroEntryAmount"
)

// readValidateOpts parses ValidateOpts from the URL query parameters and from the request body.
Expand Down Expand Up @@ -75,6 +76,7 @@ func readValidateOpts(request *http.Request) (io.Reader, *ach.ValidateOpts, erro
unequalAddendaCounts,
preserveSpaces,
allowInvalidAmounts,
allowZeroEntryAmount,
}

var buf bytes.Buffer
Expand Down Expand Up @@ -130,6 +132,8 @@ func readValidateOpts(request *http.Request) (io.Reader, *ach.ValidateOpts, erro
opts.PreserveSpaces = yes
case allowInvalidAmounts:
opts.AllowInvalidAmounts = yes
case allowZeroEntryAmount:
opts.AllowZeroEntryAmount = yes
}
}

Expand Down

0 comments on commit 79da155

Please sign in to comment.