Skip to content

Commit

Permalink
Add some rules and can add custom validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
debug-ing committed Nov 16, 2024
1 parent dc461b0 commit e3a5252
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

"github.com/debug-ing/validation-go/pkg/validation"
Expand All @@ -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() {
Expand All @@ -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
}
34 changes: 34 additions & 0 deletions pkg/validation/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions pkg/validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)...)
Expand Down

0 comments on commit e3a5252

Please sign in to comment.