-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathvalidators.go
593 lines (547 loc) · 13.2 KB
/
validators.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package wire
import (
"regexp"
"strings"
"unicode/utf8"
"golang.org/x/text/currency"
)
var (
// upperAlphanumericRegex = regexp.MustCompile(`[^ A-Z0-9!"#$%&'()*+,-.\\/:;<>=?@\[\]^_{}|~]+`)
// Alpha-Numeric including spaces and special characters as defined by FAIM 3.0.6:
// . ? ! , ; : _ @ & / \ ' " ` ~ ( ) < > $ # % + - =
// NOTE: This applies to all Fedwire tags except {8200} Unstructured Addenda Info
alphanumericRegex = regexp.MustCompile(`[^ \w.?!,;:_@&/\\'"\x60~()<>$#%+-=]+`)
numericRegex = regexp.MustCompile(`[^0-9]`)
amountRegex = regexp.MustCompile("[^0-9,.]")
)
const (
// maxBufferGrowth is the high limit for growing string builders and byte buffers.
//
// 1e8/1024/1024 is ~95MB which should be enough for anybody
maxBufferGrowth = 1e8
)
func validSizeInt(n int) bool {
return n > 0 && n < maxBufferGrowth
}
func validSizeUint(n uint) bool {
return n < maxBufferGrowth
}
// validator is common validation and formatting of golang types to WIRE type strings
type validator struct{}
// isAlphanumeric checks if a string only contains ASCII alphanumeric characters
func (v *validator) isAlphanumeric(s string) error {
if alphanumericRegex.MatchString(s) {
return ErrNonAlphanumeric
}
return nil
}
// isNumeric checks if a string only contains ASCII numeric (0-9) characters
func (v *validator) isNumeric(s string) error {
if numericRegex.MatchString(s) {
// [^ 0-9]
return ErrNonNumeric
}
return nil
}
// ToDo: Amount Decimal and AmountComma (only 1 per each) ?
// isAmount checks if a string only contains one comma and ASCII numeric (0-9) characters
func (v *validator) isAmount(s string) error {
str := strings.Trim(s, ",")
if amountRegex.MatchString(str) {
// [^ [0-9],.]
return ErrNonAmount
}
return nil
}
// isAmountImplied checks if a string contains only ASCII numeric (0-9) characters, decimal precision is
// implied (2), and no commas
func (v *validator) isAmountImplied(s string) error {
if numericRegex.MatchString(s) {
// [^ 0-9]
return ErrNonAmount
}
return nil
}
// ToDo Add 5 decimal precision?
// isTypeCode ensures tag {1510} TypeCode is valid
func (v *validator) isTypeCode(code string) error {
switch code {
case
FundsTransfer,
ForeignTransfer,
SettlementTransfer:
return nil
}
return ErrTypeCode
}
// isSubTypeCode ensures tag {1510} SubTypeCode is valid
func (v *validator) isSubTypeCode(code string) error {
switch code {
case
BasicFundsTransfer,
RequestReversal,
ReversalTransfer,
RequestReversalPriorDayTransfer,
ReversalPriorDayTransfer,
RequestCredit,
FundsTransferRequestCredit,
RefusalRequestCredit,
SSIServiceMessage:
return nil
}
return ErrSubTypeCode
}
func (v *validator) isLocalInstrumentCode(code string) error {
switch code {
case
ANSIX12format,
SequenceBCoverPaymentStructured,
GeneralXMLformat,
ISO20022XMLformat,
NarrativeText,
ProprietaryLocalInstrumentCode,
RemittanceInformationStructured,
RelatedRemittanceInformation,
STP820format,
SWIFTfield70,
UNEDIFACTformat:
return nil
}
return ErrLocalInstrumentCode
}
func (v *validator) isTestProductionCode(code string) error {
switch code {
case
EnvironmentTest,
EnvironmentProduction:
return nil
}
return ErrTestProductionCode
}
func (v *validator) isMessageDuplicationCode(code string) error {
switch code {
case
MessageDuplicationOriginal,
MessageDuplicationResend:
return nil
}
return ErrMessageDuplicationCode
}
func (v *validator) isBusinessFunctionCode(code string) error {
switch code {
case
BankTransfer,
CheckSameDaySettlement,
CustomerTransferPlus,
CustomerTransfer,
DepositSendersAccount,
BankDrawDownRequest,
CustomerCorporateDrawdownRequest,
DrawdownResponse,
FEDFundsReturned,
FEDFundsSold,
BFCServiceMessage:
return nil
}
return ErrBusinessFunctionCode
}
func (v *validator) isChargeDetails(code string) error {
switch code {
case
CDBeneficiary,
CDShared:
return nil
}
return ErrChargeDetails
}
func (v *validator) isTransactionTypeCode(code string) error {
switch code {
case
" ", "COV", "":
return nil
}
return ErrTransactionTypeCode
}
func (v *validator) isIdentificationCode(code string) error {
switch code {
case
SWIFTBankIdentifierCode,
CHIPSParticipant,
DemandDepositAccountNumber,
FEDRoutingNumber,
SWIFTBICORBEIANDAccountNumber,
CHIPSIdentifier,
PassportNumber,
TaxIdentificationNumber,
DriversLicenseNumber,
AlienRegistrationNumber,
CorporateIdentification,
OtherIdentification:
return nil
}
return ErrIdentificationCode
}
func (v *validator) isAdviceCode(code string) error {
switch code {
case
AdviceCodeHold,
AdviceCodeLetter,
AdviceCodePhone,
AdviceCodeTelex,
AdviceCodeWire:
return nil
}
return ErrAdviceCode
}
func (v *validator) isAddressType(code string) error {
switch code {
case
CompletePostalAddress,
HomeAddress,
BusinessAddress,
MailAddress,
DeliveryAddress,
PostOfficeBox:
return nil
}
return ErrAddressType
}
func (v *validator) isRemittanceLocationMethod(code string) error {
switch code {
case
RLMElectronicDataExchange,
RLMEmail,
RLMFax,
RLMPostalService,
RLMSMSM,
RLMURI:
return nil
}
return ErrRemittanceLocationMethod
}
func (v *validator) isIdentificationType(code string) error {
switch code {
case
OrganizationID,
PrivateID:
return nil
}
return ErrIdentificationType
}
func (v *validator) isOrganizationIdentificationCode(code string) error {
switch code {
case
OICBankPartyIdentification,
OICCustomerNumber,
OICDataUniversalNumberSystem,
OICEmployerIdentificationNumber,
OICGlobalLocationNumber,
OICProprietaryIdentificationNumber,
OICSWIFTBICORBEI,
OICTaxIdentificationNumber:
return nil
}
return ErrOrganizationIdentificationCode
}
func (v *validator) isPrivateIdentificationCode(code string) error {
switch code {
case
PICAlienRegistrationNumber,
PICPassportNumber,
PICCustomerNumber,
PICDateBirthPlace,
PICEmployeeIdentificationNumber,
PICNationalIdentityNumber,
PICProprietaryIdentificationNumber,
PICSocialSecurityNumber,
PICTaxIdentificationNumber:
return nil
}
return ErrPrivateIdentificationCode
}
func (v *validator) isDocumentTypeCode(code string) error {
switch code {
case
AccountsReceivableOpenItem,
BillLadingShippingNotice,
CommercialInvoice,
CommercialContract,
CreditNoteRelatedFinancialAdjustment,
CreditNote,
DebitNote,
DispatchAdvice,
DebitNoteRelatedFinancialAdjustment,
HireInvoice,
MeteredServiceInvoice,
ProprietaryDocumentType,
PurchaseOrder,
SelfBilledInvoice,
StatementAccount,
TradeServicesUtilityTransaction,
Voucher:
return nil
}
return ErrDocumentTypeCode
}
func (v *validator) isCreditDebitIndicator(code string) error {
switch code {
case
CreditIndicator,
DebitIndicator:
return nil
}
return ErrCreditDebitIndicator
}
func (v *validator) isAdjustmentReasonCode(code string) error {
switch code {
case
PricingError,
ExtensionError,
ItemNotAcceptedDamaged,
ItemNotAcceptedQuality,
QuantityContested,
IncorrectProduct,
ReturnsDamaged,
ReturnsQuality,
ItemNotReceived,
TotalOrderNotReceived,
CreditAgreed,
CoveredCreditMemo:
return nil
}
return ErrAdjustmentReasonCode
}
func (v *validator) isCurrencyCode(code string) error {
_, err := currency.ParseISO(code)
if err != nil {
return ErrNonCurrencyCode
}
return nil
}
// isCentury validates a 2 digit century 20-29
func (v *validator) isCentury(s string) error {
if s < "20" || s > "29" {
return ErrValidCentury
}
return nil
}
// isYear validates a 2 digit year 00-99
func (v *validator) isYear(s string) error {
if s < "00" || s > "99" {
return ErrValidYear
}
return nil
}
// isMonth validates a 2 digit month 01-12
func (v *validator) isMonth(s string) error {
switch s {
case
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12":
return nil
}
return ErrValidMonth
}
// isDay validates a 2 digit day based on a 2 digit month
// months are 01-12, days are 01-29, 01-30, or 01-31
func (v *validator) isDay(m string, d string) error {
switch m {
// February
case "02":
switch d {
case
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29":
return nil
}
// April, June, September, November
case "04", "06", "09", "11":
switch d {
case
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30":
return nil
}
// January, March, May, July, August, October, December
case "01", "03", "05", "07", "08", "10", "12":
switch d {
case
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31":
return nil
}
}
return ErrValidDay
}
// validateDate will return the incoming string only if it matches a valid CCYYMMDD
// date format. (C=Century, Y=Year, M=Month, D=Day)
func (v *validator) validateDate(s string) error {
if length := utf8.RuneCountInString(s); length != 8 {
return NewTagWrongLengthErr(8, len(s))
}
cc, yy, mm, dd := s[:2], s[2:4], s[4:6], s[6:8]
if err := v.isCentury(cc); err != nil {
return ErrValidDate
}
if err := v.isYear(yy); err != nil {
return ErrValidDate
}
if err := v.isMonth(mm); err != nil {
return ErrValidDate
}
if err := v.isDay(mm, dd); err != nil {
return ErrValidDate
}
return nil
}
// validatePartyIdentifier validates that PartyIdentifier must be one of the following two formats:
// 1. /Account Number (slash followed by at least one valid non-space character: e.g., /123456)
func (v *validator) validatePartyIdentifier(s string) error {
if s == "" {
return ErrPartyIdentifier
}
if utf8.RuneCountInString(s) < 2 {
return ErrPartyIdentifier
}
if s[:1] == "/" {
if strings.TrimSpace(s[1:2]) == "" {
return ErrPartyIdentifier
}
an := s[2:]
if alphanumericRegex.MatchString(an) {
return ErrPartyIdentifier
}
} else {
if err := v.validateUIDPartyIdentifier(s); err != nil {
return err
}
}
return nil
}
// uidPartyIdentifier validates a unique identifier for PartyIdentifier which is not an account number
// 2. Unique Identifier/ (4 character code followed by a slash and at least one valid non-space character:
// e.g., SOSE/123-456-789)
//
// ARNU: Alien Registration Number
// CCPT: Passport Number
// CUST: Customer Identification Number
// DRLC: Driver’s License Number
// EMPL: Employer Number
// NIDN: National Identify Number
// SOSE: Social Security Number
// TXID: Tax Identification Number
func (v *validator) validateUIDPartyIdentifier(s string) error {
if utf8.RuneCountInString(s) < 7 {
return ErrPartyIdentifier
}
uid := s[:4]
switch uid {
case
PartyIdentifierAlienRegistrationNumber,
PartyIdentifierPassportNumber,
PartyIdentifierCustomerIdentificationNumber,
PartyIdentifierDriversLicenseNumber,
PartyIdentifierEmployerNumber,
PartyIdentifierNationalIdentifyNumber,
PartyIdentifierSocialSecurityNumber,
PartyIdentifierTaxIdentificationNumber:
default:
return ErrPartyIdentifier
}
if s[4:5] != "/" {
return ErrPartyIdentifier
}
if strings.TrimSpace(s[5:6]) == "" {
return ErrPartyIdentifier
}
an := s[5:]
if alphanumericRegex.MatchString(an) {
return ErrPartyIdentifier
}
return nil
}
// validateOptionFLine validates OriginatorOptionF LineOne, LineTwo, LineThree
// Format: Must begin with one of the following Line Codes followed by a slash and at least one
// valid non-space character.
// 1 Name
// 2 Address
// 3 Country and Town
// 4 Date of Birth
// 5 Place of Birth
// 6 Customer Identification Number
// 7 National Identity Number
// 8 Additional Information
// For example:
// 2/123 MAIN STREET
// 3/US/NEW YORK, NY 10000
// 7/111-22-3456
func (v *validator) validateOptionFLine(s string) error {
if s == "" {
return nil
}
// Can be "" without an error, but if not it has to be at least 3.
if utf8.RuneCountInString(s) < 3 {
return ErrOptionFLine
}
switch s[:1] {
case
OptionFName,
OptionFAddress,
OptionFCountryTown,
OptionFDOB,
OptionFBirthPlace,
OptionFCustomerIdentificationNumber,
OptionFNationalIdentityNumber,
OptionFAdditionalInformation:
default:
return ErrOptionFLine
}
if s[1:2] != "/" {
return ErrOptionFLine
}
if strings.TrimSpace(s[2:3]) == "" {
return ErrOptionFLine
}
an := strings.TrimSpace(s[2:])
if alphanumericRegex.MatchString(an) {
return ErrOptionFLine
}
return nil
}
// validateOptionFName validates OriginatorOptionF
// Name Format:
//
// Must begin with Line Code 1 followed by a slash and at least one valid non-space character:
//
// e.g., 1/SMITH JOHN.
func (v *validator) validateOptionFName(s string) error {
if utf8.RuneCountInString(s) < 3 {
return ErrOptionFName
}
if s[:1] != OptionFName {
return ErrOptionFName
}
if s[1:2] != "/" {
return ErrOptionFName
}
if strings.TrimSpace(s[2:3]) == "" {
return ErrOptionFName
}
an := strings.TrimSpace(s[2:])
if alphanumericRegex.MatchString(an) {
return ErrOptionFName
}
return nil
}