mirror of
https://github.com/Fishwaldo/validator.git
synced 2025-03-15 11:41:32 +00:00
Add support for jwt validation (#783)
This commit is contained in:
parent
8cfa1e93b6
commit
e40bece342
7 changed files with 61 additions and 0 deletions
|
@ -159,6 +159,7 @@ Baked-in Validations
|
|||
| isbn10 | International Standard Book Number 10 |
|
||||
| isbn13 | International Standard Book Number 13 |
|
||||
| json | JSON |
|
||||
| jwt | JSON Web Token (JWT) |
|
||||
| latitude | Latitude |
|
||||
| longitude | Longitude |
|
||||
| rgb | RGB String |
|
||||
|
|
|
@ -181,6 +181,7 @@ var (
|
|||
"url_encoded": isURLEncoded,
|
||||
"dir": isDir,
|
||||
"json": isJSON,
|
||||
"jwt": isJWT,
|
||||
"hostname_port": isHostnamePort,
|
||||
"lowercase": isLowercase,
|
||||
"uppercase": isUppercase,
|
||||
|
@ -2235,6 +2236,11 @@ func isJSON(fl FieldLevel) bool {
|
|||
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
|
||||
}
|
||||
|
||||
// isJWT is the validation function for validating if the current field's value is a valid JWT string.
|
||||
func isJWT(fl FieldLevel) bool {
|
||||
return jWTRegex.MatchString(fl.Field().String())
|
||||
}
|
||||
|
||||
// isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
|
||||
func isHostnamePort(fl FieldLevel) bool {
|
||||
val := fl.Field().String()
|
||||
|
|
6
doc.go
6
doc.go
|
@ -811,6 +811,12 @@ This validates that a string value is valid JSON
|
|||
|
||||
Usage: json
|
||||
|
||||
JWT String
|
||||
|
||||
This validates that a string value is a valid JWT
|
||||
|
||||
Usage: jwt
|
||||
|
||||
File path
|
||||
|
||||
This validates that a string value contains a valid file path and that
|
||||
|
|
|
@ -48,6 +48,7 @@ const (
|
|||
uRLEncodedRegexString = `^(?:[^%]|%[0-9A-Fa-f]{2})*$`
|
||||
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(>)|(<)|(")|(&)+[;]?`
|
||||
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
|
||||
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
|
||||
splitParamsRegexString = `'[^']*'|\S+`
|
||||
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
|
||||
)
|
||||
|
@ -98,6 +99,7 @@ var (
|
|||
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
|
||||
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
|
||||
hTMLRegex = regexp.MustCompile(hTMLRegexString)
|
||||
jWTRegex = regexp.MustCompile(jWTRegexString)
|
||||
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
|
||||
bicRegex = regexp.MustCompile(bicRegexString)
|
||||
)
|
||||
|
|
|
@ -1284,6 +1284,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
|
|||
translation: "{0} must be a valid json string",
|
||||
override: false,
|
||||
},
|
||||
{
|
||||
tag: "jwt",
|
||||
translation: "{0} must be a valid jwt string",
|
||||
override: false,
|
||||
},
|
||||
{
|
||||
tag: "lowercase",
|
||||
translation: "{0} must be a lowercase string",
|
||||
|
|
|
@ -141,6 +141,7 @@ func TestTranslations(t *testing.T) {
|
|||
UniqueArray [3]string `validate:"unique"`
|
||||
UniqueMap map[string]string `validate:"unique"`
|
||||
JSONString string `validate:"json"`
|
||||
JWTString string `validate:"jwt"`
|
||||
LowercaseString string `validate:"lowercase"`
|
||||
UppercaseString string `validate:"uppercase"`
|
||||
Datetime string `validate:"datetime=2006-01-02"`
|
||||
|
@ -646,6 +647,10 @@ func TestTranslations(t *testing.T) {
|
|||
ns: "Test.JSONString",
|
||||
expected: "JSONString must be a valid json string",
|
||||
},
|
||||
{
|
||||
ns: "Test.JWTString",
|
||||
expected: "JWTString must be a valid jwt string",
|
||||
},
|
||||
{
|
||||
ns: "Test.LowercaseString",
|
||||
expected: "LowercaseString must be a lowercase string",
|
||||
|
|
|
@ -10787,6 +10787,42 @@ func TestJSONValidation(t *testing.T) {
|
|||
}, "Bad field type int")
|
||||
}
|
||||
|
||||
func TestJWTValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
param string
|
||||
expected bool
|
||||
}{
|
||||
{"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiZ29waGVyIn0.O_bROM_szPq9qBql-XDHMranHwP48ODdoLICWzqBr_U", true},
|
||||
{"acb123-_.def456-_.ghi789-_", true},
|
||||
{"eyJhbGciOiJOT05FIn0.e30.", true},
|
||||
{"eyJhbGciOiJOT05FIn0.e30.\n", false},
|
||||
{"\x00.\x00.\x00", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
validate := New()
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
errs := validate.Var(test.param, "jwt")
|
||||
|
||||
if test.expected {
|
||||
if !IsEqual(errs, nil) {
|
||||
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
|
||||
}
|
||||
} else {
|
||||
if IsEqual(errs, nil) {
|
||||
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
|
||||
} else {
|
||||
val := getError(errs, "", "")
|
||||
if val.Tag() != "jwt" {
|
||||
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_hostnameport_validator(t *testing.T) {
|
||||
type Host struct {
|
||||
Addr string `validate:"hostname_port"`
|
||||
|
|
Loading…
Add table
Reference in a new issue