Skip to content

Commit

Permalink
Merge pull request #149 from moov-io/patch-for-business-account
Browse files Browse the repository at this point in the history
patch for business account
  • Loading branch information
adamdecaf committed May 2, 2023
2 parents b57c1eb + 3777d21 commit 2c8d3ec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
71 changes: 42 additions & 29 deletions pkg/lib/base_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,10 @@ func (r *BaseSegment) Bytes() []byte {

// Validate performs some checks on the record and returns an error if not Validated
func (r *BaseSegment) Validate() error {
return r.validateRecord(r, baseSegmentCharacterFormat, "base segment")
if err := r.validateRecord(r, baseSegmentCharacterFormat, "base segment"); err != nil {
return err
}
return nil
}

// BlockSize returns size of block
Expand Down Expand Up @@ -741,8 +744,7 @@ func (r *BaseSegment) MarshalJSON() ([]byte, error) {
func (r *BaseSegment) UnmarshalJSON(data []byte) error {

dummy := make(map[string]interface{})
err := json.Unmarshal(data, &dummy)
if err != nil {
if err := json.Unmarshal(data, &dummy); err != nil {
return fmt.Errorf("invalid json format (%s)", err.Error())
}

Expand All @@ -758,9 +760,8 @@ func (r *BaseSegment) UnmarshalJSON(data []byte) error {
switch key {
case BaseSegmentName:
base := baseJson{}
err := json.Unmarshal(buf, &base)
if err != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, err.Error())
if parseErr := json.Unmarshal(buf, &base); parseErr != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, parseErr.Error())
}

fromFields := reflect.ValueOf(&base).Elem()
Expand All @@ -775,19 +776,18 @@ func (r *BaseSegment) UnmarshalJSON(data []byte) error {
}
case J1SegmentName, J2SegmentName:
var list []interface{}
err := json.Unmarshal(buf, &list)
if err != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, err.Error())
if parseErr := json.Unmarshal(buf, &list); parseErr != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, parseErr.Error())
}

for _, subSegment := range list {
subBuf, err := json.Marshal(subSegment)
if err != nil {
return err
subBuf, parseErr := json.Marshal(subSegment)
if parseErr != nil {
return parseErr
}
err = unmarshalApplicableSegments(key, subBuf, r)
if err != nil {
return err
parseErr = unmarshalApplicableSegments(key, subBuf, r)
if parseErr != nil {
return parseErr
}
}
default:
Expand Down Expand Up @@ -903,6 +903,10 @@ func (r *BaseSegment) ValidateTelephoneNumber() error {
// validation of social security number
func (r *BaseSegment) ValidateSocialSecurityNumber() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if r.ECOACode == "2" || r.ECOACode == "5" {
return nil
}
return utils.NewErrInvalidValueOfField("social security number", "base segment")
}
return nil
Expand All @@ -911,6 +915,10 @@ func (r *BaseSegment) ValidateSocialSecurityNumber() error {
// validation of date of birth
func (r *BaseSegment) ValidateDateBirth() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if r.ECOACode == "2" || r.ECOACode == "5" {
return nil
}
return utils.NewErrInvalidValueOfField("date birth", "base segment")
}
return nil
Expand Down Expand Up @@ -1192,8 +1200,7 @@ func (r *PackedBaseSegment) MarshalJSON() ([]byte, error) {
func (r *PackedBaseSegment) UnmarshalJSON(data []byte) error {

dummy := make(map[string]interface{})
err := json.Unmarshal(data, &dummy)
if err != nil {
if err := json.Unmarshal(data, &dummy); err != nil {
return fmt.Errorf("invalid json format (%s)", err.Error())
}

Expand All @@ -1209,9 +1216,8 @@ func (r *PackedBaseSegment) UnmarshalJSON(data []byte) error {
switch key {
case BaseSegmentName:
base := baseJson{}
err := json.Unmarshal(buf, &base)
if err != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, err.Error())
if parseErr := json.Unmarshal(buf, &base); parseErr != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, parseErr.Error())
}
fromFields := reflect.ValueOf(&base).Elem()
toFields := reflect.ValueOf(r).Elem()
Expand All @@ -1225,18 +1231,17 @@ func (r *PackedBaseSegment) UnmarshalJSON(data []byte) error {
}
case J1SegmentName, J2SegmentName:
var list []interface{}
err := json.Unmarshal(buf, &list)
if err != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, err.Error())
if parseErr := json.Unmarshal(buf, &list); parseErr != nil {
return fmt.Errorf("unabled to parse %s segment (%s)", key, parseErr.Error())
}
for _, subSegment := range list {
subBuf, err := json.Marshal(subSegment)
if err != nil {
return err
subBuf, parseErr := json.Marshal(subSegment)
if parseErr != nil {
return parseErr
}
err = unmarshalApplicableSegments(key, subBuf, r)
if err != nil {
return err
parseErr = unmarshalApplicableSegments(key, subBuf, r)
if parseErr != nil {
return parseErr
}
}
default:
Expand Down Expand Up @@ -1352,6 +1357,10 @@ func (r *PackedBaseSegment) ValidateTelephoneNumber() error {
// validation of social security number
func (r *PackedBaseSegment) ValidateSocialSecurityNumber() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if r.ECOACode == "2" || r.ECOACode == "5" {
return nil
}
return utils.NewErrInvalidValueOfField("social security number", "base segment")
}
return nil
Expand All @@ -1360,6 +1369,10 @@ func (r *PackedBaseSegment) ValidateSocialSecurityNumber() error {
// validation of date of birth
func (r *PackedBaseSegment) ValidateDateBirth() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if r.ECOACode == "2" || r.ECOACode == "5" {
return nil
}
return utils.NewErrInvalidValueOfField("date birth", "base segment")
}
return nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/lib/j2_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (s *J2Segment) ValidateResidenceCode() error {
// validation of social security number
func (r *J2Segment) ValidateSocialSecurityNumber() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if strings.ToUpper(r.ECOACode) == "W" {
return nil
}
return utils.NewErrInvalidValueOfField("social security number", "base segment")
}
return nil
Expand All @@ -260,6 +264,10 @@ func (r *J2Segment) ValidateSocialSecurityNumber() error {
// validation of date of birth
func (r *J2Segment) ValidateDateBirth() error {
if r.SocialSecurityNumber == 0 && r.DateBirth.IsZero() {
// social security number and date birth may omit for business account
if strings.ToUpper(r.ECOACode) == "W" {
return nil
}
return utils.NewErrInvalidValueOfField("date birth", "base segment")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (v *validator) validateRecord(r interface{}, spec map[string]field, recordN
fields := reflect.ValueOf(r).Elem()
for i := 0; i < fields.NumField(); i++ {
fieldName := fields.Type().Field(i).Name
if spec, ok := spec[fieldName]; ok {
if spec.Required == required {
if elmSpec, ok := spec[fieldName]; ok {
if elmSpec.Required == required {
fieldValue := fields.FieldByName(fieldName)
if fieldValue.IsZero() {
return utils.NewErrFieldRequired(fieldName, recordName)
Expand Down

0 comments on commit 2c8d3ec

Please sign in to comment.