Skip to content

Commit 6239d2e

Browse files
committed
Add other rules
1 parent 297ccf7 commit 6239d2e

File tree

7 files changed

+105
-33
lines changed

7 files changed

+105
-33
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/debug-ing/validation-go
33
go 1.22.3
44

55
require (
6+
github.com/gofrs/uuid v4.4.0+incompatible
67
github.com/google/uuid v1.6.0
78
github.com/oklog/ulid/v2 v2.1.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
2+
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
13
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
24
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
35
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=

pkg/validation/rules.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"regexp"
66
"strconv"
77
"strings"
8-
9-
"github.com/google/uuid"
10-
"github.com/oklog/ulid/v2"
118
)
129

1310
func validateRequired(value string, errorMsg string) error {
@@ -17,19 +14,7 @@ func validateRequired(value string, errorMsg string) error {
1714
return nil
1815
}
1916

20-
func validationIsUUID(value string, errorMsg string) error {
21-
_, err := uuid.Parse(value)
22-
if err != nil {
23-
return errors.New(errorMsg)
24-
}
25-
return nil
26-
}
27-
28-
func validationIsULID(value string, errorMsg string) error {
29-
_, err := ulid.Parse(value)
30-
if err != nil {
31-
return errors.New(errorMsg)
32-
}
17+
func validateOptional(value string, errorMsg string) error {
3318
return nil
3419
}
3520

@@ -114,11 +99,3 @@ func validationIsNotContains(value string, contains string, errorMsg string) err
11499
}
115100
return nil
116101
}
117-
118-
func validationIsMongoID(value string, errorMsg string) error {
119-
mongoIDRegex := `^[0-9a-fA-F]{24}$`
120-
if matched, _ := regexp.MatchString(mongoIDRegex, value); !matched {
121-
return errors.New(errorMsg)
122-
}
123-
return nil
124-
}

pkg/validation/rules_hash.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package validation
2+
3+
import "errors"
4+
5+
func validationMd5(value string, errorMsg string) error {
6+
if len(value) != 32 {
7+
return errors.New(errorMsg)
8+
}
9+
return nil
10+
}

pkg/validation/rules_id.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package validation
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
7+
"github.com/google/uuid"
8+
"github.com/oklog/ulid/v2"
9+
)
10+
11+
func validationIsMongoID(value string, errorMsg string) error {
12+
mongoIDRegex := `^[0-9a-fA-F]{24}$`
13+
if matched, _ := regexp.MatchString(mongoIDRegex, value); !matched {
14+
return errors.New(errorMsg)
15+
}
16+
return nil
17+
}
18+
19+
func validationIsUUID(value string, errorMsg string) error {
20+
_, err := uuid.Parse(value)
21+
if err != nil {
22+
return errors.New(errorMsg)
23+
}
24+
return nil
25+
}
26+
27+
func validationIsULID(value string, errorMsg string) error {
28+
_, err := ulid.Parse(value)
29+
if err != nil {
30+
return errors.New(errorMsg)
31+
}
32+
return nil
33+
}

pkg/validation/rules_network.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package validation
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
)
7+
8+
func validationIsURL(value string, errorMsg string) error {
9+
urlRegex := `^(http|https)://[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,}(/\S*)?$`
10+
if matched, _ := regexp.MatchString(urlRegex, value); !matched {
11+
return errors.New(errorMsg)
12+
}
13+
return nil
14+
}
15+
16+
func validationIsIPv4(value string, errorMsg string) error {
17+
ipRegex := `^(\d{1,3}\.){3}\d{1,3}$`
18+
if matched, _ := regexp.MatchString(ipRegex, value); !matched {
19+
return errors.New(errorMsg)
20+
}
21+
return nil
22+
}
23+
24+
func validationIsIPv6(value string, errorMsg string) error {
25+
ipRegex := `^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$`
26+
if matched, _ := regexp.MatchString(ipRegex, value); !matched {
27+
return errors.New(errorMsg)
28+
}
29+
return nil
30+
}
31+
32+
func validationIsIP(value string, errorMsg string) error {
33+
if err := validationIsIPv4(value, errorMsg); err != nil {
34+
if err := validationIsIPv6(value, errorMsg); err != nil {
35+
return errors.New(errorMsg)
36+
}
37+
}
38+
return nil
39+
}

pkg/validation/validator.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,31 @@ import (
88
)
99

1010
var validators = map[string]interface{}{
11+
//base
1112
"required": validateRequired,
12-
"minlength": validateMinLength,
13-
"uuid": validationIsUUID,
14-
"ulid": validationIsULID,
15-
"bic": validationIsBIC,
16-
"ethaddress": validationIsEthereumAddress,
17-
"btcaddress": validationIsBtcAddress,
18-
"mongoID": validationIsMongoID,
19-
"email": validateIsEmail,
13+
"optional": validateOptional,
2014
"numeric": validateIsNumber,
2115
"boolean": validateIsBoolean,
2216
"contains": validationIsContains,
2317
"notcontains": validationIsNotContains,
24-
"between": validationBetween,
2518
"minmax": validationMinMaxNumber,
19+
"minlength": validateMinLength,
20+
"between": validationBetween,
21+
//id
22+
"uuid": validationIsUUID,
23+
"ulid": validationIsULID,
24+
"mongoID": validationIsMongoID,
25+
//wallet address
26+
"ethaddress": validationIsEthereumAddress,
27+
"btcaddress": validationIsBtcAddress,
28+
//bank
29+
"bic": validationIsBIC,
30+
//ip
31+
"ipv4": validationIsIPv4,
32+
"ipv6": validationIsIPv6,
33+
//url
34+
"url": validationIsURL,
35+
"email": validateIsEmail,
2636
}
2737

2838
var data = map[string]map[string]string{

0 commit comments

Comments
 (0)