Skip to content

Commit

Permalink
Validate keys of custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Feb 16, 2025
1 parent bbe4250 commit e0e726d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/config/user_config_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (config *UserConfig) Validate() error {
if err := validateKeybindings(config.Keybinding); err != nil {
return err
}
if err := validateCustomCommands(config.CustomCommands); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -78,3 +81,20 @@ func validateKeybindings(keybindingConfig KeybindingConfig) error {

return nil
}

func validateCustomCommandKey(key string) error {
if !isValidKeybindingKey(key) {
return fmt.Errorf("Unrecognized key '%s' for custom command. For permitted values see %s",
strings.ToLower(key), constants.Links.Docs.CustomKeybindings)
}
return nil
}

func validateCustomCommands(customCommands []CustomCommand) error {
for _, customCommand := range customCommands {
if err := validateCustomCommandKey(customCommand.Key); err != nil {
return err
}
}
return nil
}
18 changes: 18 additions & 0 deletions pkg/config/user_config_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ func TestUserConfigValidate_enums(t *testing.T) {
{value: "1,2,3,4,5,6", valid: false},
},
},
{
name: "Custom command keybinding",
setup: func(config *UserConfig, value string) {
config.CustomCommands = []CustomCommand{
{
Key: value,
Command: "echo 'hello'",
},
}
},
testCases: []testCase{
{value: "", valid: true},
{value: "<disabled>", valid: true},
{value: "q", valid: true},
{value: "<c-c>", valid: true},
{value: "invalid_value", valid: false},
},
},
}

for _, s := range scenarios {
Expand Down

0 comments on commit e0e726d

Please sign in to comment.