Files
Crussell/backend/internal/validators/validators.go
T

34 lines
770 B
Go

package validators
import (
"github.com/go-playground/validator/v10"
"reflect"
"regexp"
"strings"
)
var Validate *validator.Validate
func init() {
Validate = validator.New()
Validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
}
// ID format: 12-character hexadecimal string (from gen_random_bytes(6) encoded as hex)
var validIDRegex = regexp.MustCompile(`^[0-9a-f]{12}$`)
// IsValidID checks if an ID is valid based on the database constraint (CHAR(12) hex string)
// Valid IDs are exactly 12 hexadecimal characters (0-9, a-f)
func IsValidID(id string) bool {
if id == "" {
return false
}
return validIDRegex.MatchString(id)
}