From 78279e7196b466d86e946287fd8a7803aa72ce54 Mon Sep 17 00:00:00 2001 From: debug-ing Date: Sat, 16 Nov 2024 07:37:34 +0330 Subject: [PATCH] Add other rules network and edit document --- README.md | 12 ++++++++++++ pkg/validation/rules_network.go | 16 ++++++++++++++++ pkg/validation/validator.go | 7 +++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e56bac7..bf0a4d5 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,18 @@ This project is currently in development, and additional features will be added Stay tuned for upcoming enhancements! + +### Network: + +| Tag | Description | +| - | - | +| ip | Internet Protocol Address IP | +| ipv4 | Internet Protocol Address IPv4 | +| ipv6 | Internet Protocol Address IPv6 | +| mac | Media Access Control Address MAC | +| uri | URI String | +| url | URL String | + ## more I developed this project to deepen my understanding of Go and its capabilities. It's an opportunity for me to explore Go's features and enhance my skills in building more efficient and flexible systems. diff --git a/pkg/validation/rules_network.go b/pkg/validation/rules_network.go index 54f8526..448c6c5 100644 --- a/pkg/validation/rules_network.go +++ b/pkg/validation/rules_network.go @@ -13,6 +13,14 @@ func validationIsURL(value string, errorMsg string) error { return nil } +func validationIsURI(value string, errorMsg string) error { + uriRegex := `^([a-zA-Z0-9\-.]+)$` + if matched, _ := regexp.MatchString(uriRegex, value); !matched { + return errors.New(errorMsg) + } + return nil +} + func validationIsIPv4(value string, errorMsg string) error { ipRegex := `^(\d{1,3}\.){3}\d{1,3}$` if matched, _ := regexp.MatchString(ipRegex, value); !matched { @@ -37,3 +45,11 @@ func validationIsIP(value string, errorMsg string) error { } return nil } + +func validationIsMacAddress(value string, errorMsg string) error { + macRegex := `^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$` + if matched, _ := regexp.MatchString(macRegex, value); !matched { + return errors.New(errorMsg) + } + return nil +} diff --git a/pkg/validation/validator.go b/pkg/validation/validator.go index 6bbc85f..55529bc 100644 --- a/pkg/validation/validator.go +++ b/pkg/validation/validator.go @@ -27,11 +27,14 @@ var validators = map[string]interface{}{ "btcaddress": validationIsBtcAddress, //bank "bic": validationIsBIC, - //ip + //network "ipv4": validationIsIPv4, "ipv6": validationIsIPv6, + "ip": validationIsIP, + "mac": validationIsMacAddress, + "uri": validationIsURI, + "url": validationIsURL, //url - "url": validationIsURL, "email": validateIsEmail, }