add & moved to validations folder; del validator/v10
This commit is contained in:
38
internal/validation/DriversLicence_validation.go
Normal file
38
internal/validation/DriversLicence_validation.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func ValidateDriversLicence(fl validator.FieldLevel) bool {
|
||||
fieldValue := fl.Field().String()
|
||||
if len(fieldValue) != 11 {
|
||||
return false
|
||||
}
|
||||
|
||||
id, tenthChar := string(fieldValue[:9]), string(fieldValue[9])
|
||||
|
||||
if tenthChar == "X" {
|
||||
tenthChar = "10"
|
||||
}
|
||||
tenthValue, _ := strconv.ParseInt(tenthChar, 10, 8)
|
||||
|
||||
// for readability
|
||||
weights := []int{9, 8, 7, 6, 5, 4, 3, 2, 1}
|
||||
sum := 0
|
||||
|
||||
for i := 0; i < 9; i++ {
|
||||
char := string(id[i])
|
||||
value, _ := strconv.ParseInt(char, 36, 64)
|
||||
sum += int(value) * weights[i]
|
||||
}
|
||||
|
||||
calcCheckDigit := sum % 11
|
||||
if calcCheckDigit != int(tenthValue) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user