diff --git a/viper.go b/viper.go index c106c53..b77e682 100644 --- a/viper.go +++ b/viper.go @@ -1265,6 +1265,14 @@ func (v *Viper) GetKnownKeys() map[string]interface{} { return ret } +// IsKnown returns whether the given key has been set as a known key +func IsKnown(key string) bool { return v.IsKnown(key) } +func (v *Viper) IsKnown(key string) bool { + key = strings.ToLower(key) + _, exists := v.knownKeys[key] + return exists +} + // Set sets the value for the key in the override register. // Set is case-insensitive for a key. // Will be used instead of values obtained via diff --git a/viper_test.go b/viper_test.go index 2016561..5507a6d 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1879,3 +1879,18 @@ func TestKnownKeys(t *testing.T) { t.Error("SetKnown didn't mark key as known") } } + +func TestIsKnown(t *testing.T) { + v := New() + + v.SetDefault("default", 45) + assert.True(t, v.IsKnown("default")) + + v.SetKnown("known") + assert.True(t, v.IsKnown("known")) + + v.SetKnown("UpperKnown") + assert.True(t, v.IsKnown("UpperKnown")) + + assert.False(t, v.IsKnown("unknown")) +}