Skip to content

Commit

Permalink
Add vstring.Base64
Browse files Browse the repository at this point in the history
  • Loading branch information
SladeThe committed Oct 10, 2023
1 parent f0eaa7e commit ff59840
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const (

CheckNameRegexp = "regexp"

CheckNameBase64 = "base64"
CheckNameBase64URL = "base64url"
CheckNameBase64RawURL = "base64rawurl"

CheckNameAlpha = "alpha"
CheckNameAlphanumeric = "alphanum"
CheckNameNumeric = "numeric"
Expand Down
49 changes: 49 additions & 0 deletions vstring/base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package vstring

import (
"regexp"

"github.com/SladeThe/yav"
)

var (
base64Regex = regexp.MustCompile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$")
base64URLRegex = regexp.MustCompile("^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$")
base64RawURLRegex = regexp.MustCompile("^(?:[A-Za-z0-9-_]{4})*[A-Za-z0-9-_]{2,4}$")
)

func Base64(name string, value string) (stop bool, err error) {
if !base64Regex.MatchString(value) {
return true, yav.Error{
CheckName: yav.CheckNameBase64,
ValueName: name,
Value: value,
}
}

return false, nil
}

func Base64URL(name string, value string) (stop bool, err error) {
if !base64URLRegex.MatchString(value) {
return true, yav.Error{
CheckName: yav.CheckNameBase64URL,
ValueName: name,
Value: value,
}
}

return false, nil
}

func Base64RawURL(name string, value string) (stop bool, err error) {
if !base64RawURLRegex.MatchString(value) {
return true, yav.Error{
CheckName: yav.CheckNameBase64RawURL,
ValueName: name,
Value: value,
}
}

return false, nil
}

0 comments on commit ff59840

Please sign in to comment.