diff --git a/go.mod b/go.mod index 5196420..209ffed 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/debug-ing/validation-go go 1.22.3 -require github.com/google/uuid v1.6.0 +require ( + github.com/google/uuid v1.6.0 + github.com/oklog/ulid/v2 v2.1.0 +) diff --git a/go.sum b/go.sum index 7790d7c..3f7a479 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,5 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= +github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= +github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= diff --git a/main.go b/main.go index cd73931..da5c4c0 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/debug-ing/validation-go/pkg/validation" @@ -12,6 +13,7 @@ type UpdateIAMUserRequest struct { LName string `validate:"required,minlength=10"` Age int `validate:"required,numeric,minmax=18-100"` Nike string `validate:"required,between=1-10"` + KOS string `validate:"test"` } func main() { @@ -22,11 +24,20 @@ func main() { Age: 29, Nike: "sdfkjdf", } - // + validation.AddCustomValidator("test", "%s vard kon", validateRequired) err := validation.ValidateStruct(req) if err != nil { fmt.Println("Validation error:", err) } else { fmt.Println("Validation passed") } + // +} + +func validateRequired(value string, errorMsg string) error { + if value == "" { + fmt.Println(errorMsg) + return errors.New(errorMsg) + } + return nil } diff --git a/pkg/validation/rules.go b/pkg/validation/rules.go index fb1c565..23e9ca4 100644 --- a/pkg/validation/rules.go +++ b/pkg/validation/rules.go @@ -2,10 +2,12 @@ package validation import ( "errors" + "regexp" "strconv" "strings" "github.com/google/uuid" + "github.com/oklog/ulid/v2" ) func validateRequired(value string, errorMsg string) error { @@ -23,6 +25,38 @@ func validationIsUUID(value string, errorMsg string) error { return nil } +func validationIsULID(value string, errorMsg string) error { + _, err := ulid.Parse(value) + if err != nil { + return errors.New(errorMsg) + } + return nil +} + +func validationIsBIC(value string, errorMsg string) error { + bicRegex := `^[A-Za-z]{4}[A-Za-z]{2}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$` + if matched, _ := regexp.MatchString(bicRegex, value); !matched { + return errors.New(errorMsg) + } + return nil +} + +func validationIsEthereumAddress(value string, errorMsg string) error { + ethRegex := `^0x[a-fA-F0-9]{40}$` + if matched, _ := regexp.MatchString(ethRegex, value); !matched { + return errors.New(errorMsg) + } + return nil +} + +func validationIsBtcAddress(value string, errorMsg string) error { + btcRegex := `^(1|3|bc1)[a-zA-HJ-NP-Z0-9]{25,39}$` + if matched, _ := regexp.MatchString(btcRegex, value); !matched { + return errors.New(errorMsg) + } + return nil +} + func validateMinLength(value string, min int, errorMsg string) error { if len(value) < min { return errors.New(errorMsg) diff --git a/pkg/validation/validator.go b/pkg/validation/validator.go index b3b3d4d..c8bae78 100644 --- a/pkg/validation/validator.go +++ b/pkg/validation/validator.go @@ -101,6 +101,14 @@ func ValidateStruct(s interface{}) error { return nil } +func AddCustomValidator(tagName, message string, fn interface{}) { + validators[tagName] = fn + data[tagName] = map[string]string{ + "error_msg": message, + } + fmt.Println(data) +} + func getMessage(tagName, fieldName string, args ...interface{}) string { errorMsgTemplate := data[tagName]["error_msg"] return fmt.Sprintf(errorMsgTemplate, append([]interface{}{fieldName}, args...)...)