53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package validation
|
|
|
|
import (
|
|
"GoMembership/internal/models"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
func validateDriverslicence(sl validator.StructLevel) {
|
|
dl := sl.Current().Interface().(models.User).Licence
|
|
if !validateLicence(dl.Number) {
|
|
sl.ReportError(dl.Number, "licence_number", "", "invalid", "")
|
|
}
|
|
if dl.IssuedDate.After(time.Now()) {
|
|
sl.ReportError(dl.IssuedDate, "issued_date", "", "invalid", "")
|
|
}
|
|
if dl.ExpirationDate.Before(time.Now().AddDate(0, 0, 3)) {
|
|
sl.ReportError(dl.ExpirationDate, "expiration_date", "", "too_soon", "")
|
|
}
|
|
}
|
|
|
|
func validateLicence(fieldValue string) bool {
|
|
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
|
|
}
|