-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.go
195 lines (148 loc) · 3.83 KB
/
validator.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
package vat
import (
"errors"
"regexp"
"strings"
"github.com/ltns35/go-vat/countries"
)
var countriesVATDoesNotStartWithCountryCode = []string{
countries.Brazil.Name,
}
var allCountries = []countries.Calculer{
countries.Albania,
countries.Andorra,
countries.Argentina,
countries.Austria,
countries.Belarus,
countries.Belgium,
countries.Bolivia,
countries.Brazil,
countries.Bulgaria,
countries.Canada,
countries.Croatia,
countries.Cyprus,
countries.CzechRepublic,
countries.Denmark,
countries.Estonia,
countries.Finland,
countries.France,
countries.Germany,
countries.Greece,
countries.HongKong,
countries.Hungary,
countries.Ireland,
countries.Italy,
countries.Kazakhstan,
countries.Latvia,
countries.Liechtenstein,
countries.Lithuania,
countries.Luxembourg,
countries.Malta,
countries.Netherlands,
countries.NorthMacedonia,
countries.Norway,
countries.Peru,
countries.Poland,
countries.Portugal,
countries.Romania,
countries.Russia,
countries.SanMarino,
countries.Serbia,
countries.Slovakia,
countries.Slovenia,
countries.Spain,
countries.Sweden,
countries.Switzerland,
countries.Turkey,
countries.Ukraine,
countries.UnitedKingdom,
}
func makeResult(vat string, isValid bool, isSupported bool, country countries.Country) *CheckResult {
var checkResult CheckResult
checkResult.Value = vat
checkResult.IsValid = isValid
checkResult.IsSupportedCountry = isSupported
if isSupported {
checkResult.Country = country
}
return &checkResult
}
func removeExtraChars(vat string) string {
regex := regexp.MustCompile(`(\s|-|\.|\/)+`)
vat = strings.ToUpper(vat)
vat = regex.ReplaceAllString(vat, "")
return vat
}
func getCountryCodes(country countries.Country) []string {
codes := country.Codes
if country.Name == countries.Greece.Name {
codes = append(codes, "EL")
} else if country.Name == countries.Liechtenstein.Name {
codes = append(codes, "FL")
}
return codes
}
func getCountry(vat string, countriesList []countries.Calculer) (countries.Calculer, error) {
for _, c := range countriesList {
country := c.GetCountry()
if startsWithCode(vat, country) || (!isVATStartWithCountryCode(country.Name) && isVATStartWithNumber(vat)) {
return c, nil
}
}
return nil, errors.New("cannot retrieve a supported country")
}
func startsWithCode(vat string, country countries.Country) bool {
countryCodes := getCountryCodes(country)
for _, code := range countryCodes {
if strings.HasPrefix(vat, code) {
return true
}
}
return false
}
func isVATStartWithCountryCode(countryName string) bool {
for _, country := range countriesVATDoesNotStartWithCountryCode {
if country == countryName {
return false
}
}
return true
}
func isVATStartWithNumber(vat string) bool {
regex := regexp.MustCompile("^\\d{2}")
return regex.MatchString(vat)
}
func isVatValidToRegexp(vat string, regexArr []string) (bool, *regexp.Regexp) {
for _, regexStr := range regexArr {
regex := regexp.MustCompile(regexStr)
isValid := regex.MatchString(vat)
if isValid {
return true, regex
}
}
return false, nil
}
func isVatValid(vat string, country countries.Calculer) bool {
regexpValidRes, regex := isVatValidToRegexp(vat, country.GetCountry().Rules.Regex)
if !regexpValidRes {
return false
}
foundResults := regex.FindAllStringSubmatch(vat, -1)
cleanVAT := foundResults[0][2]
return country.Calc(cleanVAT)
}
func Validate(vat string, countriesList ...countries.Calculer) (*CheckResult, error) {
if strings.Trim(vat, " ") == "" {
return makeResult(vat, false, false, countries.Country{}), nil
}
cleanVAT := removeExtraChars(vat)
if countriesList[0] == nil {
countriesList = allCountries
}
country, err := getCountry(cleanVAT, countriesList)
if err != nil {
return nil, err
}
isValid := isVatValid(cleanVAT, country)
return makeResult(cleanVAT, isValid, true, country.GetCountry()), nil
}