Skip to content

Commit

Permalink
separate validate name from validate description
Browse files Browse the repository at this point in the history
  • Loading branch information
irenarindos authored and kheina committed May 13, 2024
1 parent b4eb186 commit 52b4986
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions internal/daemon/controller/handlers/scopes/scope_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ func validateUpdateRequest(req *pbs.UpdateScopeRequest) error {
switch {
case trimmed == "":
badFields["name"] = "Cannot set empty string as name"
case !handlers.ValidNameDescription(trimmed):
case !handlers.ValidName(trimmed):
badFields["name"] = "Name contains unprintable characters"
default:
item.GetName().Value = trimmed
Expand All @@ -1017,7 +1017,7 @@ func validateUpdateRequest(req *pbs.UpdateScopeRequest) error {
switch {
case trimmed == "":
badFields["description"] = "Cannot set empty string as description"
case !handlers.ValidNameDescription(trimmed):
case !handlers.ValidDescription(trimmed):
badFields["description"] = "Description contains unprintable characters"
default:
item.GetDescription().Value = trimmed
Expand Down
18 changes: 13 additions & 5 deletions internal/daemon/controller/handlers/verifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ValidateCreateRequest(i ApiResource, fn CustomValidatorFunc) error {
switch {
case trimmed == "":
badFields["name"] = "Cannot set empty string as name"
case !ValidNameDescription(trimmed):
case !ValidName(trimmed):
badFields["name"] = "Name contains unprintable characters"
default:
i.GetName().Value = trimmed
Expand All @@ -52,7 +52,7 @@ func ValidateCreateRequest(i ApiResource, fn CustomValidatorFunc) error {
switch {
case trimmed == "":
badFields["description"] = "Cannot set empty string as description"
case !ValidNameDescription(trimmed):
case !ValidDescription(trimmed):
badFields["description"] = "Description contains unprintable characters"
default:
i.GetDescription().Value = trimmed
Expand Down Expand Up @@ -100,7 +100,7 @@ func ValidateUpdateRequest(r UpdateRequest, i ApiResource, fn CustomValidatorFun
switch {
case trimmed == "":
badFields["name"] = "Cannot set empty string as name"
case !ValidNameDescription(trimmed):
case !ValidName(trimmed):
badFields["name"] = "Name contains unprintable characters"
default:
i.GetName().Value = trimmed
Expand All @@ -111,7 +111,7 @@ func ValidateUpdateRequest(r UpdateRequest, i ApiResource, fn CustomValidatorFun
switch {
case trimmed == "":
badFields["description"] = "Cannot set empty string as description"
case !ValidNameDescription(trimmed):
case !ValidDescription(trimmed):
badFields["description"] = "Description contains unprintable characters"
default:
i.GetDescription().Value = trimmed
Expand Down Expand Up @@ -193,10 +193,18 @@ func ValidId(i Id, prefixes ...string) bool {
return false
}

func ValidNameDescription(in string) bool {
func ValidDescription(in string) bool {
idx := strings.IndexFunc(in, func(c rune) bool {
return !(unicode.IsPrint(c) || unicode.IsSpace(c))
})

return idx == -1
}

func ValidName(in string) bool {
idx := strings.IndexFunc(in, func(c rune) bool {
return !unicode.IsPrint(c)
})

return idx == -1
}
14 changes: 10 additions & 4 deletions internal/daemon/controller/handlers/verifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ func TestValidId(t *testing.T) {
assert.False(t, ValidId(Id("prefix_short"), "short"))
}

func TestValidNameDescription(t *testing.T) {
assert.True(t, ValidNameDescription("foobar"))
assert.True(t, ValidNameDescription("this is\n a long description"))
assert.False(t, ValidNameDescription("foo\u200Bbar"))
func TestValidDescription(t *testing.T) {
assert.True(t, ValidDescription("foobar"))
assert.True(t, ValidDescription("this is\n a long description"))
assert.False(t, ValidDescription("foo\u200Bbar"))
}

func TestValidName(t *testing.T) {
assert.True(t, ValidName("foobar"))
assert.False(t, ValidName("this is\n a long description"))
assert.False(t, ValidName("foo\u200Bbar"))
}

func TestValidateGetRequest(t *testing.T) {
Expand Down

0 comments on commit 52b4986

Please sign in to comment.