Repository: moov-io/ach
Pull request #1108 (2022-10-21 19:44:10)

feat: add ReadJSONFile and ReadJSONFileWith helpers


Patch:
diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock
index b51ccfbb..d089d50f 100644
--- a/docs/Gemfile.lock
+++ b/docs/Gemfile.lock
@@ -222,11 +222,11 @@ GEM
       jekyll-seo-tag (~> 2.1)
     minitest (5.14.4)
     multipart-post (2.1.1)
-    nokogiri (1.13.9-arm64-darwin)
+    nokogiri (1.13.6-arm64-darwin)
       racc (~> 1.4)
-    nokogiri (1.13.9-x86_64-darwin)
+    nokogiri (1.13.6-x86_64-darwin)
       racc (~> 1.4)
-    nokogiri (1.13.9-x86_64-linux)
+    nokogiri (1.13.6-x86_64-linux)
       racc (~> 1.4)
     octokit (4.21.0)
       faraday (>= 0.9)
diff --git a/file.go b/file.go
index a03a6f17..ac24e330 100644
--- a/file.go
+++ b/file.go
@@ -22,6 +22,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"os"
 	"strings"
 	"sync"
 	"time"
@@ -109,6 +110,25 @@ func FileFromJSON(bs []byte) (*File, error) {
 	return FileFromJSONWith(bs, nil)
 }
 
+// ReadJSONFile will consume the specified filepath and parse the contents as a JSON formatted ACH file.
+func ReadJSONFile(path string) (*File, error) {
+	bs, err := os.ReadFile(path)
+	if err != nil {
+		return nil, err
+	}
+	return FileFromJSON(bs)
+}
+
+// ReadJSONFileWith will consume the specified filepath and parse the contents
+// as a JSON formatted ACH file with custom ValidateOpts.
+func ReadJSONFileWith(path string, opts *ValidateOpts) (*File, error) {
+	bs, err := os.ReadFile(path)
+	if err != nil {
+		return nil, err
+	}
+	return FileFromJSONWith(bs, opts)
+}
+
 // FileFromJSONWith attempts to return a *File object assuming the input is valid JSON.
 //
 // It allows custom validation overrides, so the file may not be Nacha compliant
diff --git a/fileHeader.go b/fileHeader.go
index 12649398..5949007b 100644
--- a/fileHeader.go
+++ b/fileHeader.go
@@ -258,10 +258,6 @@ func (fh *FileHeader) ValidateWith(opts *ValidateOpts) error {
 // fieldInclusion validate mandatory fields are not default values. If fields are
 // invalid the ACH transfer will be returned.
 func (fh *FileHeader) fieldInclusion() error {
-	if fh.validateOpts != nil && fh.validateOpts.AllowMissingFileHeader {
-		return nil
-	}
-
 	if fh.ImmediateDestination == "" {
 		return fieldError("ImmediateDestination", ErrConstructor, fh.ImmediateDestinationField())
 	}
diff --git a/go.mod b/go.mod
index 2adf7cce..b42b1b92 100644
--- a/go.mod
+++ b/go.mod
@@ -13,7 +13,7 @@ require (
 	github.com/moov-io/base v0.36.0
 	github.com/prometheus/client_golang v1.13.0
 	github.com/stretchr/testify v1.8.0
-	golang.org/x/oauth2 v0.1.0
+	golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
 )
 
 require (
@@ -30,8 +30,8 @@ require (
 	github.com/prometheus/common v0.37.0 // indirect
 	github.com/prometheus/procfs v0.8.0 // indirect
 	github.com/rickar/cal/v2 v2.1.7 // indirect
-	golang.org/x/net v0.1.0 // indirect
-	golang.org/x/sys v0.1.0 // indirect
+	golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
+	golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
 	google.golang.org/appengine v1.6.7 // indirect
 	google.golang.org/protobuf v1.28.1 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
diff --git a/go.sum b/go.sum
index d33b17cd..42e5c52c 100644
--- a/go.sum
+++ b/go.sum
@@ -311,7 +311,6 @@ golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su
 golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
 golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
 golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
-golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
 golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -324,7 +323,6 @@ golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094/go.mod h1:h4gKUeWbJ4rQPri
 golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
 golang.org/x/oauth2 v0.0.0-20221006150949-b44042a4b9c1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
 golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
-golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -377,8 +375,6 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
 golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
-golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=


Test patch:
diff --git a/fileHeader_test.go b/fileHeader_test.go
index 8d4d24a6..31e61faf 100644
--- a/fileHeader_test.go
+++ b/fileHeader_test.go
@@ -18,7 +18,6 @@
 package ach
 
 import (
-	"github.com/stretchr/testify/require"
 	"strings"
 	"testing"
 	"time"
@@ -99,16 +98,6 @@ func TestFileHeader__ImmediateOrigin(t *testing.T) {
 	}
 }
 
-func TestFileHeader__ByPassingFileHeaderValidation(t *testing.T) {
-	header := NewFileHeader()
-	header.ImmediateDestination = ""
-	header.ImmediateOrigin = ""
-	header.SetValidation(&ValidateOpts{AllowMissingFileHeader: true})
-
-	err := header.fieldInclusion()
-	require.NoError(t, err)
-}
-
 func TestFileHeader__ImmediateDestination(t *testing.T) {
 	tests := []struct {
 		name                        string
diff --git a/file_test.go b/file_test.go
index 8b14a09c..ea2ede9b 100644
--- a/file_test.go
+++ b/file_test.go
@@ -474,17 +474,11 @@ func TestFile__ValidateOptsJSON(t *testing.T) {
 	require.False(t, opts.BypassDestinationValidation)
 }
 
-func TestFile__readFromJson(t *testing.T) {
+func TestFileReadJSONFile(t *testing.T) {
 	path := filepath.Join("test", "testdata", "ppd-valid.json")
-	bs, err := os.ReadFile(path)
-	if err != nil {
-		t.Fatal(err)
-	}
 
-	file, err := FileFromJSON(bs)
-	if err != nil {
-		t.Fatal(err)
-	}
+	file, err := ReadJSONFile(path)
+	require.NoError(t, err)
 	require.Nil(t, file.GetValidation())
 
 	// Ensure the file is valid
@@ -537,6 +531,8 @@ func TestFile__readFromJson(t *testing.T) {
 
 	// ensure we error on struct tag unmarshal
 	var f File
+	bs, err := os.ReadFile(path)
+	require.NoError(t, err)
 	if err := json.Unmarshal(bs, &f); err != nil {
 		t.Error(err)
 	}
@@ -647,11 +643,7 @@ func TestFile__iso8601JSON(t *testing.T) {
 }
 
 func TestFile__IATdatetimeParse(t *testing.T) {
-	bs, err := os.ReadFile(filepath.Join("test", "testdata", "iat-debit.json"))
-	if err != nil {
-		t.Fatal(err)
-	}
-	file, err := FileFromJSON(bs)
+	file, err := ReadJSONFile(filepath.Join("test", "testdata", "iat-debit.json"))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -668,12 +660,12 @@ func TestFile__IATdatetimeParse(t *testing.T) {
 }
 
 func TestFile__JsonBypassOrigin(t *testing.T) {
-	bs, err := os.ReadFile(filepath.Join("test", "testdata", "json-bypass-origin.json"))
-	require.NoError(t, err)
-
-	file, err := FileFromJSONWith(bs, &ValidateOpts{
+	path := filepath.Join("test", "testdata", "json-bypass-origin.json")
+	opts := &ValidateOpts{
 		BypassOriginValidation: true,
-	})
+	}
+
+	file, err := ReadJSONFileWith(path, opts)
 	require.NoError(t, err)
 	require.NoError(t, file.Validate())
 
@@ -684,13 +676,13 @@ func TestFile__JsonBypassOrigin(t *testing.T) {
 }
 
 func TestFile__JsonBypassDestinationAndOrigin(t *testing.T) {
-	bs, err := os.ReadFile(filepath.Join("test", "testdata", "json-bypass-origin-and-destination.json"))
-	require.NoError(t, err)
-
-	file, err := FileFromJSONWith(bs, &ValidateOpts{
+	path := filepath.Join("test", "testdata", "json-bypass-origin-and-destination.json")
+	opts := &ValidateOpts{
 		BypassOriginValidation:      true,
 		BypassDestinationValidation: true,
-	})
+	}
+
+	file, err := ReadJSONFileWith(path, opts)
 	require.NoError(t, err)
 	require.NoError(t, file.Validate())