feat: expose owner_name in coder_workspace resource (#11639)

This commit is contained in:
Marcin Tojek
2024-01-17 13:20:45 +01:00
committed by GitHub
parent b173195e0d
commit 5eb3e1cdaa
40 changed files with 353 additions and 146 deletions

View File

@ -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.

View File

@ -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
}

View File

@ -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)
})
}
}