Documentation
¶
Index ¶
- Variables
- func JSON[T any](s string) (T, error)
- func Load[T any](s Schema) (*T, error)
- func ParseJSON[T any](s string) (T, error)
- func Validate[T any](validator Validator[T], variable Variable[T]) (T, error)
- func WithDefault[T any](v T) *T
- type ErrorCode
- type PDomain
- type PHost
- type PURL
- type Parser
- type Schema
- type ValidationError
- type Validator
- type ValidatorSupplier
- type Variable
Constants ¶
This section is empty.
Variables ¶
var Bool = ParseBool
Bool see ParseBool.
var BoolMap = map[string]bool{ "1": true, "true": true, "t": true, "yes": true, "y": true, "on": true, "0": false, "false": false, "f": false, "no": false, "n": false, "off": false, }
BoolMap maps bool-like strings to bool values.
var Domain = ParseDomain
Domain see ParseDomain.
var Email = ParseEmail
Email see ParseEmail.
var Float32 = ParseFloat32
Float32 see ParseFloat32.
var Float64 = ParseFloat64
Float64 see ParseFloat64.
var Host = ParseHost
Host see ParseHost.
var IPv4 = ParseIPv4
IPv4 see ParseIPv4.
var IPv6 = ParseIPv6
IPv6 see ParseIPv6.
var Int32 = ParseInt32
Int32 see ParseInt32.
var Int64 = ParseInt64
Int64 see ParseInt64.
var ParseBool = TrimmedParser(func(s string) (bool, error) { s = strings.ToLower(s) if v, ok := BoolMap[s]; ok { return v, nil } return false, errors.New("value must be a bool-like") })
ParseBool parses the string s as a bool.
It returns an error if the string s is not a bool.
var ParseDomain = TrimmedParser(func(s string) (*PDomain, error) { if strings.HasSuffix(s, ".") { return nil, errors.New("value must not contain a trailing dot") } if strings.IndexByte(s, '.') == -1 { return nil, errors.New("value must contain a zone") } v, err := idna.Registration.ToASCII(s) if err != nil { return nil, errors.New("value must be a valid domain") } lastDot := strings.LastIndexByte(v, '.') nextDot := strings.LastIndexByte(v[:lastDot], '.') var subdomainPtr *string hostname := v if nextDot != -1 { hostname = v[nextDot+1:] sub := v[:nextDot] subdomainPtr = &sub } zone, _ := publicsuffix.PublicSuffix(s) return &PDomain{ String: v, Subdomain: subdomainPtr, Hostname: hostname, Zone: ptr(zone), }, nil })
ParseDomain parses the string s as a domain (including IDN).
It returns an error if the string s is not a valid domain.
var ParseEmail = TrimmedParser(func(s string) (string, error) { address, err := mail.ParseAddress(s) if err != nil { return "", errors.New("value must be an email") } hasDiff := address.Address != s hasBracket := strings.IndexByte(address.Address, '[') != -1 if hasDiff || hasBracket { return "", errors.New("value must be in format \"user@domain\"") } _, domain, _ := strings.Cut(address.Address, "@") if _, err := ParseDomain(domain); err != nil { return "", errors.New("email must contain a valid domain") } return s, nil })
ParseEmail parses the string s as an email address in the format "user@domain".
It returns an error if the string s is not a valid email address.
var ParseFloat32 = TrimmedParser(func(s string) (float32, error) { v, err := parseFloatGeneric[float32](s) return v, err })
ParseFloat32 parses the string s as a float.
It returns an error if the string s is not a number or out of range.
var ParseFloat64 = TrimmedParser(func(s string) (float64, error) { v, err := parseFloatGeneric[float64](s) return v, err })
ParseFloat64 parses the string s as a float.
It returns an error if the string s is not a number or out of range.
var ParseHost = TrimmedParser(func(s string) (*PHost, error) { if strings.EqualFold(s, "localhost") { d := &PDomain{String: "localhost", Hostname: "localhost"} return &PHost{String: "localhost", Domain: d}, nil } if v, err := ParseIPv4(s); err == nil { return &PHost{String: v.String(), IP: v}, nil } if v, err := ParseIPv6(s); err == nil { return &PHost{String: v.String(), IP: v}, nil } if v, err := ParseDomain(s); err == nil { return &PHost{String: v.String, Domain: v}, nil } return nil, errors.New("value must be a host (IPv4, IPv6, or domain, including localhost)") })
ParseHost parses a string as a host, which can be: - an IPv4 address; - an IPv6 address; - a domain (including localhost).
It returns an error if the string s is not a host.
var ParseIPv4 = TrimmedParser(func(s string) (net.IP, error) { v := net.ParseIP(s) if v == nil || v.To4() == nil { return nil, errors.New("value must be a valid IPv4 address") } return v, nil })
ParseIPv4 parses the string s as a IPv4 address.
It returns an error if the string s is not a valid IPv4 address.
var ParseIPv6 = TrimmedParser(func(s string) (net.IP, error) { v := net.ParseIP(s) if v == nil || v.To16() == nil || v.To4() != nil { return nil, errors.New("value must be a valid IPv6 address") } return v, nil })
ParseIPv6 parses the string s as a IPv6 address.
It returns an error if the string s is not a valid IPv6 address.
var ParseInt32 = TrimmedParser(func(s string) (int32, error) { v, err := parseIntGeneric[int32](s) return v, err })
ParseInt32 parses the string s as an integer.
It returns an error if the string s is not a number or out of range.
var ParseInt64 = TrimmedParser(func(s string) (int64, error) { v, err := parseIntGeneric[int64](s) return v, err })
ParseInt64 parses the string s as an integer.
It returns an error if the string s is not a number or out of range.
var ParsePort = TrimmedParser(func(s string) (uint16, error) { v, err := parseIntGeneric[int32](s) if err != nil || v < minPort || v > maxPort { return 0, fmt.Errorf("port must be a number between %d and %d", minPort, maxPort) } return uint16(v), nil })
ParsePort parses the string s as a TCP/UDP port (1–65535).
It returns an error if the string s is not a number or out of range.
var ParseStr = TrimmedParser(func(s string) (string, error) { return s, nil })
ParseStr parses string s as a trimmed string.
var ParseURL = TrimmedParser(func(s string) (*PURL, error) { uri, err := url.Parse(s) if err != nil { return nil, errors.New("value must be a URL") } if uri.Scheme == "" { return nil, errors.New("value must be in absolute format (protocol://...)") } if uri.Opaque != "" { return nil, errors.New("value must be in default url format") } if h := uri.Hostname(); h == "" { return nil, errors.New("value must contain a host") } var portPtr *uint16 if p := uri.Port(); p != "" { pp, err := ParsePort(p) if err != nil { return nil, err } portPtr = &pp } host, err := ParseHost(uri.Hostname()) if err != nil { return nil, errors.New("hostname must be a valid host (IPv4, IPv6, or domain, including localhost)") } var usernamePtr, passwordPtr *string if uri.User != nil { usernamePtr = ptr(uri.User.Username()) p, _ := uri.User.Password() passwordPtr = &p } var pathPtr *string if p := uri.Path; p != "" && p != "/" { pathPtr = ptr(strings.TrimPrefix(p, "/")) } var fragmentPtr *string if uri.Fragment != "" { fragmentPtr = ptr(uri.Fragment) } return &PURL{ String: s, Protocol: strings.ToLower(uri.Scheme), Username: usernamePtr, Password: passwordPtr, Host: *host, Port: portPtr, Path: pathPtr, Params: uri.Query(), Anchor: fragmentPtr, }, nil })
ParseURL parses the string s as a url in the maximum format "protocol://username:password@host:port/path?params#anchor".
It returns an error if the string s is not a valid url.
var Port = ParsePort
Port see ParsePort.
var Str = ParseStr
Str see ParseStr.
var URL = ParseURL
URL see ParseURL.
Functions ¶
func Load ¶
Load loads and validates environment variables, creating a config with type T using the provided Schema.
func ParseJSON ¶
ParseJSON parses the string s as an JSON with schema T.
It returns an error if the string s is not a valid JSON.
func Validate ¶
Validate loads and validates an environment variable using the provided Validator.
It returns the default value if the env var is not set and a default value is specified; the dev default value if the env var is not set, a dev default value is specified, GO_ENV var is specified and is not "production"; a ValidationError with ErrNoValue if the env var is not set and no default value is specified; a ValidationError with ErrInvalidValue if the env var is not found in the available variants or is invalid for the target type.
func WithDefault ¶
func WithDefault[T any](v T) *T
WithDefault provides a default value for a Variable.
Types ¶
type ErrorCode ¶
type ErrorCode int
const ( // ErrNoValue occurs when an env var is not set and no default value is specified. ErrNoValue ErrorCode // ErrInvalidValue occurs when an env var is invalid for the target type. ErrInvalidValue )
type PDomain ¶
type PDomain struct {
String string // e.g. docs.nyashmyash99.dev
Subdomain *string // docs (see String)
Hostname string // nyashmyash99.dev (see String)
Zone *string // dev (see String)
}
PDomain describes a parsed domain.
type PHost ¶
type PHost struct {
String string // e.g. docs.nyashmyash99.dev, 127.0.0.1 or ::1
Domain *PDomain // Optional domain, depending on the host type
IP net.IP // Optional ip, depending on the host type
}
PHost describes a parsed host.
type PURL ¶
type PURL struct {
String string // e.g. http://user:pass@docs.nyashmyash99.dev:443/envalid?tab=documentation#quick-start
Protocol string // http (see String)
Username *string // user (see String)
Password *string // pass (see String)
Host PHost // docs.nyashmyash99.dev (see String)
Port *uint16 // 443 (see String)
Path *string // envalid (see String)
Params url.Values // tab="documentation" (see String)
Anchor *string // quick-start
}
PURL describes a parsed url.
type Parser ¶
Parser describes a generic function that parses a string into a value of type T.
It returns an error if the input is invalid for the target type.
func TrimmedParser ¶
TrimmedParser wraps the Parser, preprocessing the input with the strings.TrimSpace function.
type Schema ¶
type Schema map[string]ValidatorSupplier
Schema maps config field names to their validators.
type ValidationError ¶
type ValidationError struct {
Code ErrorCode
// contains filtered or unexported fields
}
ValidationError occurs when validation fails.
It implements the error interface.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error
type ValidatorSupplier ¶
ValidatorSupplier describes a function that provides a validator for the Schema.
type Variable ¶
type Variable[T any] struct { Key string // Required env var key (e.g. GO_ENV) Description string // Optional description used in errors Variants []string // Optional array of available values for the env var. It is case-sensitive. Default *T // Optional default value returned if the env var is not set. See Variable for priority determination. DevDefault *T // Optional default value returned if the env var is not set, GO_ENV var is specified and is not "production". See Variable for priority determination. }
Variable describes the environment variable for validation.
Options priority: 1. DevDefault (if GO_ENV != production) 2. Default 3. Variants 4. Validator