-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
48 lines (38 loc) · 1.06 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package env
import (
"fmt"
"strings"
)
// Validator defines the interface for validating environment variable values.
type Validator interface {
Validate(envName string, value string) error
}
// ValidatorFactory represents a function to build validator
type ValidatorFactory func(args string) Validator
// requiredValidator checks if a value is present.
type requiredValidator struct{}
func newRequiredValidator(_ string) Validator {
return &requiredValidator{}
}
func (v requiredValidator) Validate(envName string, value string) error {
if value == "" {
return fmt.Errorf("%s is required", envName)
}
return nil
}
type expectedValueValidator struct {
expectedValues []string
}
func newExpectedValueValidator(args string) Validator {
return &expectedValueValidator{
expectedValues: strings.Split(args, " "),
}
}
func (v expectedValueValidator) Validate(envName string, value string) error {
for _, expectedValue := range v.expectedValues {
if value == expectedValue {
return nil
}
}
return fmt.Errorf("%s is unexpected value: %s", envName, value)
}