Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code optimization #490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const (

var (
userRegexp = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$")
hostRegexp = regexp.MustCompile("^[^\\s]+\\.[^\\s]+$")
hostRegexp = regexp.MustCompile(`^[^\s]+\.[^\s]+$`)
userDotRegexp = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})")
rxEmail = regexp.MustCompile(Email)
rxCreditCard = regexp.MustCompile(CreditCard)
Expand Down
18 changes: 9 additions & 9 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ var ParamTagMap = map[string]ParamValidator{

// ParamTagRegexMap maps param tags to their respective regexes.
var ParamTagRegexMap = map[string]*regexp.Regexp{
"range": regexp.MustCompile("^range\\((\\d+)\\|(\\d+)\\)$"),
"length": regexp.MustCompile("^length\\((\\d+)\\|(\\d+)\\)$"),
"runelength": regexp.MustCompile("^runelength\\((\\d+)\\|(\\d+)\\)$"),
"stringlength": regexp.MustCompile("^stringlength\\((\\d+)\\|(\\d+)\\)$"),
"range": regexp.MustCompile(`^range\((\d+)\|(\d+)\)$`),
"length": regexp.MustCompile(`^length\((\d+)\|(\d+)\)$`),
"runelength": regexp.MustCompile(`^runelength\((\d+)\|(\d+)\)$`),
"stringlength": regexp.MustCompile(`^stringlength\((\d+)\|(\d+)\)$`),
"in": regexp.MustCompile(`^in\((.*)\)`),
"matches": regexp.MustCompile(`^matches\((.+)\)$`),
"rsapub": regexp.MustCompile("^rsapub\\((\\d+)\\)$"),
"minstringlength": regexp.MustCompile("^minstringlength\\((\\d+)\\)$"),
"maxstringlength": regexp.MustCompile("^maxstringlength\\((\\d+)\\)$"),
"rsapub": regexp.MustCompile(`rsapub\((\d+)\)$`),
"minstringlength": regexp.MustCompile(`^minstringlength\((\d+)\)$`),
"maxstringlength": regexp.MustCompile(`^maxstringlength\((\d+)\)$`),
}

type customTypeTagMap struct {
Expand Down Expand Up @@ -177,7 +177,7 @@ type ISO3166Entry struct {
Numeric string
}

//ISO3166List based on https://www.iso.org/obp/ui/#search/code/ Code Type "Officially Assigned Codes"
// ISO3166List based on https://www.iso.org/obp/ui/#search/code/ Code Type "Officially Assigned Codes"
var ISO3166List = []ISO3166Entry{
{"Afghanistan", "Afghanistan (l')", "AF", "AFG", "004"},
{"Albania", "Albanie (l')", "AL", "ALB", "008"},
Expand Down Expand Up @@ -467,7 +467,7 @@ type ISO693Entry struct {
English string
}

//ISO693List based on http://data.okfn.org/data/core/language-codes/r/language-codes-3b2.json
// ISO693List based on http://data.okfn.org/data/core/language-codes/r/language-codes-3b2.json
var ISO693List = []ISO693Entry{
{Alpha3bCode: "aar", Alpha2Code: "aa", English: "Afar"},
{Alpha3bCode: "abk", Alpha2Code: "ab", English: "Abkhazian"},
Expand Down
68 changes: 35 additions & 33 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,32 @@ const rfc3339WithoutZone = "2006-01-02T15:04:05"
// SetFieldsRequiredByDefault causes validation to fail when struct fields
// do not include validations or are not explicitly marked as exempt (using `valid:"-"` or `valid:"email,optional"`).
// This struct definition will fail govalidator.ValidateStruct() (and the field values do not matter):
// type exampleStruct struct {
// Name string ``
// Email string `valid:"email"`
//
// type exampleStruct struct {
// Name string ``
// Email string `valid:"email"`
//
// This, however, will only fail when Email is empty or an invalid email address:
// type exampleStruct2 struct {
// Name string `valid:"-"`
// Email string `valid:"email"`
//
// type exampleStruct2 struct {
// Name string `valid:"-"`
// Email string `valid:"email"`
//
// Lastly, this will only fail when Email is an invalid email address but not when it's empty:
// type exampleStruct2 struct {
// Name string `valid:"-"`
// Email string `valid:"email,optional"`
//
// type exampleStruct2 struct {
// Name string `valid:"-"`
// Email string `valid:"email,optional"`
func SetFieldsRequiredByDefault(value bool) {
fieldsRequiredByDefault = value
}

// SetNilPtrAllowedByRequired causes validation to pass for nil ptrs when a field is set to required.
// The validation will still reject ptr fields in their zero value state. Example with this enabled:
// type exampleStruct struct {
// Name *string `valid:"required"`
//
// type exampleStruct struct {
// Name *string `valid:"required"`
//
// With `Name` set to "", this will be considered invalid input and will cause a validation error.
// With `Name` set to nil, this will be considered valid by validation.
// By default this is disabled.
Expand Down Expand Up @@ -154,8 +161,8 @@ func IsAlpha(str string) bool {
return rxAlpha.MatchString(str)
}

//IsUTFLetter checks if the string contains only unicode letter characters.
//Similar to IsAlpha but for all languages. Empty string is valid.
// IsUTFLetter checks if the string contains only unicode letter characters.
// Similar to IsAlpha but for all languages. Empty string is valid.
func IsUTFLetter(str string) bool {
if IsNull(str) {
return true
Expand Down Expand Up @@ -398,8 +405,8 @@ const ulidEncodedSize = 26
// IsULID checks if the string is a ULID.
//
// Implementation got from:
// https://github.com/oklog/ulid (Apache-2.0 License)
//
// https://github.com/oklog/ulid (Apache-2.0 License)
func IsULID(str string) bool {
// Check if a base32 encoded ULID is the right length.
if len(str) != ulidEncodedSize {
Expand Down Expand Up @@ -454,26 +461,26 @@ func IsCreditCard(str string) bool {
if !rxCreditCard.MatchString(sanitized) {
return false
}

number, _ := ToInt(sanitized)
number, lastDigit := number / 10, number % 10
number, lastDigit := number/10, number%10

var sum int64
for i:=0; number > 0; i++ {
for i := 0; number > 0; i++ {
digit := number % 10
if i % 2 == 0 {

if i%2 == 0 {
digit *= 2
if digit > 9 {
digit -= 9
}
}

sum += digit
number = number / 10
}
return (sum + lastDigit) % 10 == 0

return (sum+lastDigit)%10 == 0
}

// IsISBN10 checks if the string is an ISBN version 10.
Expand Down Expand Up @@ -595,25 +602,19 @@ func IsFilePath(str string) (bool, int) {
return false, Unknown
}

//IsWinFilePath checks both relative & absolute paths in Windows
// IsWinFilePath checks both relative & absolute paths in Windows
func IsWinFilePath(str string) bool {
if rxARWinPath.MatchString(str) {
//check windows path limit see:
// http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
if len(str[3:]) > 32767 {
return false
}
return true
return len(str[3:]) <= 32767
}
return false
}

//IsUnixFilePath checks both relative & absolute paths in Unix
// IsUnixFilePath checks both relative & absolute paths in Unix
func IsUnixFilePath(str string) bool {
if rxARUnixPath.MatchString(str) {
return true
}
return false
return rxARUnixPath.MatchString(str)
}

// IsDataURI checks if a string is base64 encoded data URI such as an image
Expand Down Expand Up @@ -1000,7 +1001,8 @@ func ValidateArray(array []interface{}, iterator ConditionIterator) bool {
// result will be equal to `false` if there are any errors.
// s is the map containing the data to be validated.
// m is the validation map in the form:
// map[string]interface{}{"name":"required,alpha","address":map[string]interface{}{"line1":"required,alphanum"}}
//
// map[string]interface{}{"name":"required,alpha","address":map[string]interface{}{"line1":"required,alphanum"}}
func ValidateMap(s map[string]interface{}, m map[string]interface{}) (bool, error) {
if s == nil {
return true, nil
Expand Down