forked from obalunenko/csvvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_example_test.go
71 lines (63 loc) · 1.96 KB
/
validation_example_test.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
package csvvalidator_test
import (
"fmt"
"github.com/obalunenko/csvvalidator"
)
func ExampleValidationRules_ValidateRow() {
// initialise columns
var (
clientNameCol = csvvalidator.NewColumn(0, "Client Name")
transactionDateCol = csvvalidator.NewColumn(1, "Transaction Date")
transactionAmountCol = csvvalidator.NewColumn(2, "Transaction Amount")
debitCreditCodeCol = csvvalidator.NewColumn(3, "Debit Credit Code")
customerIDCol = csvvalidator.NewColumn(4, "Customer ID")
// set total columns number
totalColNum = uint(5)
)
// set up validation rules
var rowRules = csvvalidator.ValidationRules{
clientNameCol: csvvalidator.Rule{
MaxLength: 4,
MinLength: 4,
RestrictedChars: nil,
},
transactionDateCol: csvvalidator.Rule{
MaxLength: 10,
MinLength: 10,
RestrictedChars: nil,
},
transactionAmountCol: csvvalidator.Rule{
MaxLength: 14,
MinLength: 4,
RestrictedChars: nil,
},
debitCreditCodeCol: csvvalidator.Rule{
MaxLength: 1,
MinLength: 1,
RestrictedChars: nil,
},
customerIDCol: csvvalidator.Rule{
MaxLength: 10,
MinLength: 1,
RestrictedChars: nil,
},
}
// create csv row ([]string)
var testDataRow = make([]string, totalColNum)
testDataRow[clientNameCol.Number] = "Test Client Name too long" // exceed maxlength
testDataRow[transactionDateCol.Number] = "2019/05/30"
testDataRow[transactionAmountCol.Number] = "100.45"
testDataRow[debitCreditCodeCol.Number] = "C"
testDataRow[customerIDCol.Number] = "ID"
// validate row
err := rowRules.ValidateRow(testDataRow)
fmt.Print(err)
// Output:
// invalid column [row:[Test Client Name too long 2019/05/30 100.45 C ID]; column:0:Client Name]: invalid length [column:[Test Client Name too long]; has len:[25]; should be:[4]]
}
func ExampleNewColumn() {
clientNameCol := csvvalidator.NewColumn(0, "Client Name")
fmt.Print(clientNameCol.String())
// Output:
// 0:Client Name
}