Skip to content

Commit

Permalink
Merge pull request #1481 from adamdecaf/file-from-json-read-validateopts
Browse files Browse the repository at this point in the history
fix: read validateOpts from JSON blobs
  • Loading branch information
adamdecaf authored Aug 28, 2024
2 parents 53b5fd1 + 24e9490 commit 6ef1db5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
6 changes: 6 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ func FileFromJSONWith(bs []byte, opts *ValidateOpts) (*File, error) {
if opts != nil {
file.SetValidation(opts)
}
validateOpts, err := readValidateOpts(bs)
if err != nil {
return nil, fmt.Errorf("reading validate opts: %w", err)
}
file.SetValidation(file.validateOpts.merge(validateOpts))

if err := file.Create(); err != nil {
return file, err
}
Expand Down
28 changes: 28 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,3 +2191,31 @@ func TestFile_ValidateOpts_Merge(t *testing.T) {
require.NotNil(t, full.merge(empty))
})
}

func TestFileJSON_ValidateOpts(t *testing.T) {
file := mockFilePPD(t)
file.SetValidation(&ValidateOpts{
PreserveSpaces: true,
})

bs, err := json.Marshal(file)
require.NoError(t, err)
require.Contains(t, string(bs), `"preserveSpaces":true`)

file, err = FileFromJSON(bs)
require.NoError(t, err)
require.True(t, file.GetValidation().PreserveSpaces)
}

func TestFileFromJSON_ValidateOpts(t *testing.T) {
bs, err := os.ReadFile(filepath.Join("test", "testdata", "ppd-valid-preserve-spaces.json"))
require.NoError(t, err)

file, err := FileFromJSON(bs)
require.NoError(t, err)
require.NotNil(t, file)

validateOpts := file.GetValidation()
require.NotNil(t, validateOpts)
require.True(t, validateOpts.PreserveSpaces)
}
2 changes: 2 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ components:
nullable: true
fileADVControl:
$ref: '#/components/schemas/ADVFileControl'
validateOpts:
$ref: '#/components/schemas/ValidateOpts'
required:
- ID
- fileHeader
Expand Down
2 changes: 1 addition & 1 deletion server/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestFiles__CustomJsonValidation(t *testing.T) {
var resp createFileResponse
err = json.NewDecoder(w.Body).Decode(&resp)

require.ErrorContains(t, err, "ImmediateOrigin 000000000 is a mandatory field")
require.NoError(t, err)
require.Equal(t, "adam-01", resp.ID)
require.NotNil(t, resp.File)
require.Equal(t, nil, resp.Err)
Expand Down
64 changes: 64 additions & 0 deletions test/testdata/ppd-valid-preserve-spaces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"id": "fileId",
"fileHeader": {
"id": "fileId",
"immediateDestination": "231380104",
"immediateOrigin": "121042882",
"fileCreationDate": "2018-10-08T00:00:00Z",
"fileCreationTime": "2018-10-08T01:01:02Z",
"fileIDModifier": "A",
"immediateDestinationName": "Citadel",
"immediateOriginName": "Wells Fargo"
},
"batches": [
{
"batchHeader": {
"id": "fileId",
"serviceClassCode": 200,
"companyName": "Wells Fargo",
"companyIdentification": "121042882",
"standardEntryClassCode": "PPD",
"companyEntryDescription": "Trans. Des",
"effectiveEntryDate": "2018-10-09T00:00:00Z",
"ODFIIdentification": "12104288",
"batchNumber": 1
},
"entryDetails": [
{
"id": "fileId",
"transactionCode": 22,
"RDFIIdentification": "23138010",
"checkDigit": "4",
"DFIAccountNumber": "81967038518 ",
"amount": 100000,
"identificationNumber": "#83738AB# ",
"individualName": "Steven Tander ",
"discretionaryData": " ",
"addendaRecordIndicator": 0,
"traceNumber": "121042880000001",
"category": "Forward"
},
{
"id": "fileId",
"transactionCode": 27,
"RDFIIdentification": "12104288",
"checkDigit": "2",
"DFIAccountNumber": "17124411",
"amount": 100000,
"identificationNumber": "#83738AB#",
"individualName": "My ODFI",
"discretionaryData": " ",
"addendaRecordIndicator": 0,
"traceNumber": "121042880000002",
"category": "Forward"
}
]
}
],
"IATBatches": null,
"NotificationOfChange": null,
"ReturnEntries": null,
"validateOpts": {
"preserveSpaces": true
}
}

0 comments on commit 6ef1db5

Please sign in to comment.