add & moved to validations folder; del validator/v10
This commit is contained in:
@@ -1,157 +0,0 @@
|
||||
package utils
|
||||
|
||||
// import "regexp"
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/pkg/logger"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/jbub/banking/iban"
|
||||
"github.com/jbub/banking/swift"
|
||||
)
|
||||
|
||||
var xssPatterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(`(?i)<script`),
|
||||
regexp.MustCompile(`(?i)javascript:`),
|
||||
regexp.MustCompile(`(?i)on\w+\s*=`),
|
||||
regexp.MustCompile(`(?i)(vbscript|data):`),
|
||||
regexp.MustCompile(`(?i)<(iframe|object|embed|applet)`),
|
||||
regexp.MustCompile(`(?i)expression\s*\(`),
|
||||
regexp.MustCompile(`(?i)url\s*\(`),
|
||||
regexp.MustCompile(`(?i)<\?`),
|
||||
regexp.MustCompile(`(?i)<%`),
|
||||
regexp.MustCompile(`(?i)<!\[CDATA\[`),
|
||||
regexp.MustCompile(`(?i)<(svg|animate)`),
|
||||
regexp.MustCompile(`(?i)<(audio|video|source)`),
|
||||
regexp.MustCompile(`(?i)base64`),
|
||||
}
|
||||
|
||||
func ValidateToTrue(fl validator.FieldLevel) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func AgeValidator(fl validator.FieldLevel) bool {
|
||||
fieldValue := fl.Field()
|
||||
dateOfBirth := fieldValue.Interface().(time.Time)
|
||||
now := time.Now()
|
||||
age := now.Year() - dateOfBirth.Year()
|
||||
|
||||
if now.YearDay() < dateOfBirth.YearDay() {
|
||||
age-- // if birthday is in the future..
|
||||
}
|
||||
|
||||
return age >= 18
|
||||
}
|
||||
|
||||
func SubscriptionModelValidator(fl validator.FieldLevel) bool {
|
||||
fieldValue := fl.Field().String()
|
||||
var names []string
|
||||
if err := database.DB.Model(&models.SubscriptionModel{}).Pluck("name", &names).Error; err != nil {
|
||||
logger.Error.Fatalf("Couldn't get SubscriptionModel names: %#v", err)
|
||||
return false
|
||||
}
|
||||
return slices.Contains(names, fieldValue)
|
||||
}
|
||||
|
||||
func IBANValidator(fl validator.FieldLevel) bool {
|
||||
fieldValue := fl.Field().String()
|
||||
|
||||
return iban.Validate(fieldValue) == nil
|
||||
}
|
||||
|
||||
func ValidateRequiredMembershipField(fl validator.FieldLevel) bool {
|
||||
user := fl.Top().Interface().(*models.User)
|
||||
membership := user.Membership
|
||||
subModel := membership.SubscriptionModel
|
||||
|
||||
// Get the field name specified in RequiredMembershipField
|
||||
fieldName := subModel.RequiredMembershipField
|
||||
if fieldName == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
// Get the value of the field specified by RequiredMembershipField
|
||||
fieldValue := reflect.ValueOf(membership).FieldByName(fieldName)
|
||||
|
||||
// Check if the fieldValue is valid
|
||||
if !fieldValue.IsValid() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the fieldValue is a nil pointer
|
||||
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Ensure that the fieldValue is an uint
|
||||
var fieldUint uint
|
||||
if fieldValue.Kind() == reflect.Uint {
|
||||
fieldUint = uint(fieldValue.Uint())
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
var membershipIDs []uint
|
||||
if err := database.DB.Model(&models.Membership{}).Pluck("id", &membershipIDs).Error; err != nil {
|
||||
logger.Error.Fatalf("Couldn't get SubscriptionModel names: %#v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the field value is zero (empty)
|
||||
return slices.Contains(membershipIDs, fieldUint)
|
||||
}
|
||||
|
||||
func BICValidator(fl validator.FieldLevel) bool {
|
||||
fieldValue := fl.Field().String()
|
||||
|
||||
return swift.Validate(fieldValue) == nil
|
||||
}
|
||||
|
||||
func ValidateSafeContent(fl validator.FieldLevel) bool {
|
||||
input := strings.ToLower(fl.Field().String())
|
||||
for _, pattern := range xssPatterns {
|
||||
if pattern.MatchString(input) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
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