Skip to content

Commit

Permalink
Merge pull request #118 from rezakhademix/feat-a-much-faster-email-regex
Browse files Browse the repository at this point in the history
feat: a much faster regex check for email and mac address
  • Loading branch information
rezakhademix authored Nov 21, 2024
2 parents 539b401 + f30c1f4 commit 258c8a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
20 changes: 17 additions & 3 deletions email.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package govalidator

import (
"regexp"
"strings"
)

const (
// Email represents rule name which will be used to find the default error message.
Email = "email"
// EmailMsg is the default error message format for fields with Email validation rule.
EmailMsg = "%s is not valid"
// EmailRegex is the default pattern to validate email field by RFC 5322 rule.
EmailRegex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])"

minEmailValidLen = 3
maxEmailValidLen = 320
)

// EmailRegex is the default pattern to validate email field by RFC 5322 rule.
var EmailRegex = regexp.MustCompile(`^([a-z0-9!#$%&'*+/=?^_` + "`" + `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" + `{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])$`)

// Email checks the value under validation must match the EmailRegex regular expression.
//
// Example:
Expand All @@ -19,7 +28,12 @@ const (
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) Email(s, field, msg string) Validator {
v.RegexMatches(s, EmailRegex, field, v.msg(Email, msg, field))
if len(s) < minEmailValidLen || len(s) > maxEmailValidLen || !strings.Contains(s, "@") {
v.check(false, field, v.msg(Email, msg, field))
return v
}

v.check(EmailRegex.MatchString(s), field, v.msg(Email, msg, field))

return v
}
9 changes: 6 additions & 3 deletions mac.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package govalidator

import "regexp"

const (
// MAC represents rule name which will be used to find the default error message.
MAC = "MAC"
// MACMsg is the default error message format for fields with MAC validation rule.
MACMsg = "%s is not valid"
// MACRegex is the default pattern to validate MAC address field.
MACRegex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
)

// MACRegex is the default pattern to validate MAC address field.
var MACRegex = regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})`)

// MAC checks the value under validation must match the MACRegex regular expression.
//
// Example:
Expand All @@ -19,7 +22,7 @@ const (
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) MAC(s, field, msg string) Validator {
v.RegexMatches(s, MACRegex, field, v.msg(MAC, msg, field))
v.check(MACRegex.MatchString(s), field, v.msg(Email, msg, field))

return v
}
4 changes: 1 addition & 3 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const (
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) RegexMatches(s string, pattern string, field, msg string) Validator {
r := regexp.MustCompile(pattern)

v.check(r.Match([]byte(s)), field, v.msg(Regex, msg))
v.check(regexp.MustCompile(pattern).MatchString(s), field, v.msg(Regex, msg))

return v
}

0 comments on commit 258c8a5

Please sign in to comment.