mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: expose owner_name
in coder_workspace
resource (#11639)
This commit is contained in:
@ -80,6 +80,20 @@ func init() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userRealNameValidator := func(fl validator.FieldLevel) bool {
|
||||
f := fl.Field().Interface()
|
||||
str, ok := f.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
valid := UserRealNameValid(str)
|
||||
return valid == nil
|
||||
}
|
||||
err = Validate.RegisterValidation("user_real_name", userRealNameValidator)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Is404Error returns true if the given error should return a 404 status code.
|
||||
|
@ -79,3 +79,15 @@ func TemplateDisplayNameValid(str string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UserRealNameValid returns whether the input string is a valid real user name.
|
||||
func UserRealNameValid(str string) error {
|
||||
if len(str) > 128 {
|
||||
return xerrors.New("must be <= 128 characters")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(str) != str {
|
||||
return xerrors.New("must not have leading or trailing white spaces")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -209,3 +209,37 @@ func TestFrom(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserRealNameValid(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Valid bool
|
||||
}{
|
||||
{"1", true},
|
||||
{"A", true},
|
||||
{"A1", true},
|
||||
{".", true},
|
||||
{"Mr Bean", true},
|
||||
{"Severus Snape", true},
|
||||
{"Prof. Albus Percival Wulfric Brian Dumbledore", true},
|
||||
{"Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso", true},
|
||||
{"Hector Ó hEochagáin", true},
|
||||
{"Małgorzata Kalinowska-Iszkowska", true},
|
||||
{"成龍", true},
|
||||
{". .", true},
|
||||
|
||||
{"Lord Voldemort ", false},
|
||||
{" Bellatrix Lestrange", false},
|
||||
{" ", false},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(testCase.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
valid := httpapi.UserRealNameValid(testCase.Name)
|
||||
require.Equal(t, testCase.Valid, valid == nil)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user