Skip to content

Commit a30fd5b

Browse files
committed
Added new IsValidEnum method
Method, tests, examples and benchmark
1 parent fd14901 commit a30fd5b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

extra_validations.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,27 @@ func IsValidSocial(social string) (success bool, err error) {
181181
success = true
182182
return
183183
}
184+
185+
//IsValidEnum validates an enum given the required parameters and tests if the supplied value is valid from accepted values.
186+
func IsValidEnum(fieldValue string, allowedValues *[]string, canBeEmpty bool) (success bool, err error) {
187+
188+
//Empty is true and no value given?
189+
if canBeEmpty == true && len(fieldValue) == 0 {
190+
success = true
191+
return
192+
}
193+
194+
//Check that the value is an allowed value (case insensitive)
195+
for _, value := range *allowedValues {
196+
197+
//Compare both in lowercase
198+
if strings.ToLower(fieldValue) == strings.ToLower(value) {
199+
success = true
200+
return
201+
}
202+
}
203+
204+
//We must have an error
205+
err = fmt.Errorf("value %s is not allowed", fieldValue)
206+
return
207+
}

extra_validations_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,63 @@ func BenchmarkIsValidEmail(b *testing.B) {
375375
_, _ = IsValidEmail(testEmail, false)
376376
}
377377
}
378+
379+
//TestIsValidEnum testing the enum value in an accepted list of values
380+
func TestIsValidEnum(t *testing.T) {
381+
382+
var ok bool
383+
var err error
384+
385+
//Invalid value
386+
testEnumValue := "1"
387+
testAcceptedValues := []string{"123"}
388+
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); ok {
389+
t.Fatal("This should have failed - value is not found", testEnumValue, testAcceptedValues, err)
390+
} else if err.Error() != "value "+testEnumValue+" is not allowed" {
391+
t.Fatal("error message was not as expected", err.Error())
392+
}
393+
394+
//Valid value
395+
testEnumValue = "123"
396+
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); !ok {
397+
t.Fatal("This should have passed - value is valid", testEnumValue, testAcceptedValues, err)
398+
}
399+
400+
//Empty valid not allowed
401+
testEnumValue = ""
402+
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); ok {
403+
t.Fatal("This should have failed - can be empty flag", testEnumValue, testAcceptedValues, err)
404+
}
405+
406+
//Empty value allowed
407+
testEnumValue = ""
408+
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, true); !ok {
409+
t.Fatal("This should have passed - can be empty flag", testEnumValue, testAcceptedValues, err)
410+
}
411+
412+
}
413+
414+
//ExampleIsValidEnum_invalid example of an invalid enum
415+
func ExampleIsValidEnum_invalid() {
416+
testAcceptedValues := []string{"123"}
417+
ok, err := IsValidEnum("1", &testAcceptedValues, false) //Invalid
418+
fmt.Println(ok, err)
419+
// Output: false value 1 is not allowed
420+
}
421+
422+
//ExampleIsValidEnum_valid example of an valid enum
423+
func ExampleIsValidEnum_valid() {
424+
testAcceptedValues := []string{"123"}
425+
ok, err := IsValidEnum("123", &testAcceptedValues, false) //Valid
426+
fmt.Println(ok, err)
427+
// Output: true <nil>
428+
}
429+
430+
//BenchmarkIsValidEnum benchmarks the IsValidEnum (valid value)
431+
func BenchmarkIsValidEnum(b *testing.B) {
432+
testValue := "1"
433+
testAcceptedValues := []string{"123"}
434+
for i := 0; i < b.N; i++ {
435+
_, _ = IsValidEnum(testValue, &testAcceptedValues, false)
436+
}
437+
}

0 commit comments

Comments
 (0)