This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ssn.go
144 lines (120 loc) · 3.41 KB
/
ssn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package api
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// SSN is a basic input.
type SSN struct {
ID int `json:"-"`
First string `json:"first"`
Middle string `json:"middle"`
Last string `json:"last"`
NotApplicable bool `json:"notApplicable"`
}
// Unmarshal bytes in to the entity properties.
func (entity *SSN) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, entity)
}
// Marshal to payload structure
func (entity *SSN) Marshal() Payload {
return MarshalPayloadEntity("ssn", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *SSN) Valid() (bool, error) {
if entity.NotApplicable {
return true, nil
}
var stack ErrorStack
first := strings.TrimSpace(entity.First)
if first == "" {
stack.Append("SSN", ErrFieldRequired{"The first part is required"})
} else if len(first) != 3 {
stack.Append("SSN", ErrFieldInvalid{"The first part should be 3 characters long"})
}
middle := strings.TrimSpace(entity.Middle)
if middle == "" {
stack.Append("SSN", ErrFieldRequired{"The middle part is required"})
} else if len(middle) != 2 {
stack.Append("SSN", ErrFieldInvalid{"The middle part should be 2 characters long"})
}
last := strings.TrimSpace(entity.Last)
if last == "" {
stack.Append("SSN", ErrFieldRequired{"The last part is required"})
} else if len(last) != 4 {
stack.Append("SSN", ErrFieldInvalid{"The last part should be 4 characters long"})
}
// Legacy system checks
fullSSN := fmt.Sprintf("%s-%s-%s", entity.First, entity.Middle, entity.Last)
legacyInvalid := []string{"999-99-9999", "123-45-6789"}
for _, bad := range legacyInvalid {
if bad == fullSSN {
stack.Append("SSN", ErrFieldInvalid{"Invalid social security number"})
}
}
// Checks using randomization
if ifirst, err := strconv.Atoi(first); err == nil {
boundaries := []struct {
lower int
upper int
}{
{lower: 0, upper: 0},
{lower: 666, upper: 666},
{lower: 900, upper: 999},
}
for _, boundary := range boundaries {
if boundary.lower <= ifirst && boundary.upper >= ifirst {
stack.Append("SSN", ErrFieldInvalid{"Invalid social security number"})
break
}
}
}
return !stack.HasErrors(), stack
}
// Save the SSN to data storage.
func (entity *SSN) Save(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the SSN from data storage.
func (entity *SSN) Delete(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Delete(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// Get the SSN from data storage.
func (entity *SSN) Get(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Select(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// GetID returns the entity identifier.
func (entity *SSN) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *SSN) SetID(id int) {
entity.ID = id
}
// Find is not used for SSNs. Please use the `Get` method.
func (entity *SSN) Find(context DatabaseService) error {
return nil
}