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

feat: Add KeysInValidator for validating fields containing specific keys #208

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions lang/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ var enUS = &Language{
"unique.element": "The :field element value has already been taken.",
"exists": "The :field does not exist.",
"exists.element": "The :field element value does not exist.",
"keysin": "The :field must have the following keys: :values.",
"keysin.element": "The :field elements must have the following keys: :values.",
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
},
fields: map[string]string{
"": "body",
Expand Down
43 changes: 43 additions & 0 deletions validation/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,46 @@ func (v *DoesntStartWithValidator) MessagePlaceholders(_ *Context) []string {
func DoesntStartWith(prefix ...string) *DoesntStartWithValidator {
return &DoesntStartWithValidator{Prefix: prefix}
}

//------------------------------

// KeysInValidator validates the field under validation must only contain the given keys.
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
type KeysInValidator struct {
BaseValidator
Keys []string
}

// Validate checks the field under validation satisfies this validator's criteria.
func (v *KeysInValidator) Validate(ctx *Context) bool {
obj, ok := ctx.Value.(map[string]interface{})
if !ok {
return false
}

allowedKeys := make(map[string]struct{})
for _, key := range v.Keys {
allowedKeys[key] = struct{}{}
}

for key := range obj {
if _, ok := allowedKeys[key]; !ok {
return false
}
}
return true
}

// Name returns the string name of the validator.
func (v *KeysInValidator) Name() string { return "keys_in" }

// MessagePlaceholders returns the ":values" placeholder.
func (v *KeysInValidator) MessagePlaceholders(_ *Context) []string {
return []string{
":values", strings.Join(v.Keys, ", "),
}
}

// KeysIn creates a new KeysInValidator.
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
func KeysIn(keys ...string) *KeysInValidator {
return &KeysInValidator{Keys: keys}
}
31 changes: 31 additions & 0 deletions validation/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,34 @@
})
}
}

func TestKeysInValidator(t *testing.T) {
t.Run("Constructor", func(t *testing.T) {
v := KeysIn([]string{"a", "b", "c"}...)
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
assert.NotNil(t, v)
assert.Equal(t, "keys_in", v.Name())
assert.False(t, v.IsType())
assert.False(t, v.IsTypeDependent())
assert.Equal(t, []string{":values", "a, b, c"}, v.MessagePlaceholders(&Context{}))
})

cases := []struct {

Check failure on line 182 in validation/string_test.go

View workflow job for this annotation

GitHub Actions / Lint

fieldalignment: struct with 40 pointer bytes could be 24 (govet)
value []string
input any
want bool
}{
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
{value: []string{"a", "b", "c"}, input: map[string]any{"a": 1, "b": 2, "c": 3}, want: true},
{value: []string{"a", "b", "c"}, input: map[string]any{"a": 1, "b": 2, "c": 3, "d": 4}, want: false},
{value: []string{"a", "b", "c"}, input: map[string]any{"a": 1, "b": 2}, want: true},
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tc := range cases {
tc := tc
t.Run(fmt.Sprintf("Validate_%v_%t", tc.value, tc.want), func(t *testing.T) {
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
v := KeysIn(tc.value...)
assert.Equal(t, tc.want, v.Validate(&Context{
Value: tc.input,
}))
})
}
}
Loading