Skip to content

Commit

Permalink
Allow uppercase letters(A-Z) for document keys (#483)
Browse files Browse the repository at this point in the history
To support capital letters, an additional rule, case_sensitive_slug is added.
  • Loading branch information
shiningsoo authored Mar 6, 2023
1 parent 5e363d0 commit 79f170b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
// (https://datatracker.ietf.org/doc/html/rfc3986#section-2.3)
// and copied from https://gist.github.com/dpk/4757681
slugRegexString = `^[a-z0-9\-._~]+$`
caseSensitiveSlugRegexString = `^[a-zA-Z0-9\-._~]+$`
containAlphaRegexString = `[a-zA-Z]`
containNumberRegexString = `[0-9]`
containSpecialCharRegexString = `[\{\}\[\]\/?.,;:|\)*~!^\-_+<>@\#$%&\\\=\(\'\"\x60]`
Expand All @@ -42,6 +43,7 @@ const (

var (
slugRegex = regexp.MustCompile(slugRegexString)
caseSensitiveSlugRegex = regexp.MustCompile(caseSensitiveSlugRegexString)
containAlphaRegex = regexp.MustCompile(containAlphaRegexString)
containNumberRegex = regexp.MustCompile(containNumberRegexString)
containSpecialCharRegex = regexp.MustCompile(containSpecialCharRegexString)
Expand Down Expand Up @@ -234,6 +236,14 @@ func init() {
})
RegisterTranslation("slug", "{0} must only contain letters, numbers, hyphen, period, underscore, and tilde")

RegisterValidation("case_sensitive_slug", func(level validator.FieldLevel) bool {
val := level.Field().String()
return caseSensitiveSlugRegex.MatchString(val)
})
RegisterTranslation(
"case_sensitive_slug",
"{0} must only contain case-sensitive letters, numbers, hyphen, period, underscore, and tilde")

RegisterValidation("alpha_num_special", func(level validator.FieldLevel) bool {
val := level.Field().String()
return hasAlphaNumSpecial(val)
Expand Down
9 changes: 9 additions & 0 deletions internal/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func TestValidation(t *testing.T) {
err = ValidateValue("invalid-key-$-wrong-string-value", "required,slug,min=4,max=30")
assert.Equal(t, err.(Violation).Tag, "slug")

err = ValidateValue("Invalid-Key", "required,slug,min=4,max=30")
assert.Equal(t, err.(Violation).Tag, "slug")

err = ValidateValue("Valid-Doc-Key", "required,case_sensitive_slug,min=4,max=30")
assert.Nil(t, err, "Valid document Key with case-sensitive letters")

err = ValidateValue("Invalid Doc Key", "required,case_sensitive_slug,min=4,max=30")
assert.Equal(t, err.(Violation).Tag, "case_sensitive_slug")

err = ValidateValue("1h30m20s", "duration,min=2")
assert.Nil(t, err, "valid time duration string format")

Expand Down
2 changes: 1 addition & 1 deletion pkg/document/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Key) String() string {
func (k Key) Validate() error {
if err := validation.Validate(k.String(), []any{
"required",
"slug",
"case_sensitive_slug",
"min=4",
"max=120",
}); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/document/key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func TestKey(t *testing.T) {

err = Key("-----_________________-a").Validate()
assert.Nil(t, err, "key should be valid")

err = Key("Capital-Character-Key").Validate()
assert.Nil(t, err, "key should be valid")
})

t.Run("invalid key test", func(t *testing.T) {
Expand Down

0 comments on commit 79f170b

Please sign in to comment.