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

Validation for python variable names #1195

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
54 changes: 54 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -231,6 +232,7 @@ var (
"mongodb": isMongoDB,
"cron": isCron,
"spicedb": isSpiceDB,
"python_var": isPythonVar,
}
)

Expand Down Expand Up @@ -2878,3 +2880,55 @@ func isCron(fl FieldLevel) bool {
cronString := fl.Field().String()
return cronRegex.MatchString(cronString)
}

// check if string is a valid python variable name
func isPythonVar(fl FieldLevel) bool {
// Python keywords can not be used as variable names
// in future this list may be subject to ammendement, pruning, or other change
switch fl.Field().String() {
case
"False",
"await",
"else",
"import",
"pass",
"None",
"break",
"except",
"in",
"raise",
"True",
"class",
"finally",
"is",
"return",
"and",
"continue",
"for",
"lambda",
"try",
"as",
"def",
"from",
"nonlocal",
"while",
"assert",
"del",
"global",
"not",
"with",
"async",
"elif",
"if",
"or",
"yield":
return false
}

// all variables must start with a letter or underscore
// further chars can be alphanumeric, or underscore
// dashes and special characters are not allowed
pattern := regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")

return pattern.Match([]byte(fl.Field().String()))
}
35 changes: 35 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13311,6 +13311,41 @@ func TestCronExpressionValidation(t *testing.T) {
}
}

func TestPythonVariableValidation(t *testing.T) {
tests := []struct {
value string `validate:"python_var"`
tag string
expected bool
}{
{"x", "python_var", true},
{"x0", "python_var", true},
{"x_0", "python_var", true},
{"__init__", "python_var", true},
{"x-x", "python_var", false},
{"0x", "python_var", false},
{"abcdefghijklmnopqrstuvwxyz", "python_var", true},
{"abcde_0_0466___", "python_var", true},
{"while", "python_var", false},
{"False", "python_var", false},
{"(x)", "python_var", false},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.value, test.tag)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf(`Index: %d python_var "%s" failed Error: %s`, i, test.value, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf(`Index: %d python_var "%s" should have errs`, i, test.value)
}
}
}
}

func TestNestedStructValidation(t *testing.T) {
validator := New()

Expand Down