Skip to content

Commit e3a5252

Browse files
committed
Add some rules and can add custom validation function
1 parent dc461b0 commit e3a5252

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/debug-ing/validation-go
22

33
go 1.22.3
44

5-
require github.com/google/uuid v1.6.0
5+
require (
6+
github.com/google/uuid v1.6.0
7+
github.com/oklog/ulid/v2 v2.1.0
8+
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
22
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3+
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
4+
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
5+
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/debug-ing/validation-go/pkg/validation"
@@ -12,6 +13,7 @@ type UpdateIAMUserRequest struct {
1213
LName string `validate:"required,minlength=10"`
1314
Age int `validate:"required,numeric,minmax=18-100"`
1415
Nike string `validate:"required,between=1-10"`
16+
KOS string `validate:"test"`
1517
}
1618

1719
func main() {
@@ -22,11 +24,20 @@ func main() {
2224
Age: 29,
2325
Nike: "sdfkjdf",
2426
}
25-
//
27+
validation.AddCustomValidator("test", "%s vard kon", validateRequired)
2628
err := validation.ValidateStruct(req)
2729
if err != nil {
2830
fmt.Println("Validation error:", err)
2931
} else {
3032
fmt.Println("Validation passed")
3133
}
34+
//
35+
}
36+
37+
func validateRequired(value string, errorMsg string) error {
38+
if value == "" {
39+
fmt.Println(errorMsg)
40+
return errors.New(errorMsg)
41+
}
42+
return nil
3243
}

pkg/validation/rules.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package validation
22

33
import (
44
"errors"
5+
"regexp"
56
"strconv"
67
"strings"
78

89
"github.com/google/uuid"
10+
"github.com/oklog/ulid/v2"
911
)
1012

1113
func validateRequired(value string, errorMsg string) error {
@@ -23,6 +25,38 @@ func validationIsUUID(value string, errorMsg string) error {
2325
return nil
2426
}
2527

28+
func validationIsULID(value string, errorMsg string) error {
29+
_, err := ulid.Parse(value)
30+
if err != nil {
31+
return errors.New(errorMsg)
32+
}
33+
return nil
34+
}
35+
36+
func validationIsBIC(value string, errorMsg string) error {
37+
bicRegex := `^[A-Za-z]{4}[A-Za-z]{2}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
38+
if matched, _ := regexp.MatchString(bicRegex, value); !matched {
39+
return errors.New(errorMsg)
40+
}
41+
return nil
42+
}
43+
44+
func validationIsEthereumAddress(value string, errorMsg string) error {
45+
ethRegex := `^0x[a-fA-F0-9]{40}$`
46+
if matched, _ := regexp.MatchString(ethRegex, value); !matched {
47+
return errors.New(errorMsg)
48+
}
49+
return nil
50+
}
51+
52+
func validationIsBtcAddress(value string, errorMsg string) error {
53+
btcRegex := `^(1|3|bc1)[a-zA-HJ-NP-Z0-9]{25,39}$`
54+
if matched, _ := regexp.MatchString(btcRegex, value); !matched {
55+
return errors.New(errorMsg)
56+
}
57+
return nil
58+
}
59+
2660
func validateMinLength(value string, min int, errorMsg string) error {
2761
if len(value) < min {
2862
return errors.New(errorMsg)

pkg/validation/validator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ func ValidateStruct(s interface{}) error {
101101
return nil
102102
}
103103

104+
func AddCustomValidator(tagName, message string, fn interface{}) {
105+
validators[tagName] = fn
106+
data[tagName] = map[string]string{
107+
"error_msg": message,
108+
}
109+
fmt.Println(data)
110+
}
111+
104112
func getMessage(tagName, fieldName string, args ...interface{}) string {
105113
errorMsgTemplate := data[tagName]["error_msg"]
106114
return fmt.Sprintf(errorMsgTemplate, append([]interface{}{fieldName}, args...)...)

0 commit comments

Comments
 (0)