Documentation
¶
Overview ¶
Package validate provides input validation helpers with safe defaults.
Index ¶
- Variables
- type DNSResolver
- type EmailOption
- func WithEmailAllowARecordFallback(allow bool) EmailOption
- func WithEmailAllowDisplayName(allow bool) EmailOption
- func WithEmailAllowIDN(allow bool) EmailOption
- func WithEmailAllowIPLiteral(allow bool) EmailOption
- func WithEmailAllowQuotedLocal(allow bool) EmailOption
- func WithEmailDNSResolver(resolver DNSResolver) EmailOption
- func WithEmailRequireMX(require bool) EmailOption
- func WithEmailRequireTLD(require bool) EmailOption
- func WithEmailVerifyDomain(verify bool) EmailOption
- type EmailResult
- type EmailValidator
- type ReputationResult
- type ReputationVerdict
- type StaticReputation
- type URLOption
- func WithURLAllowIDN(allow bool) URLOption
- func WithURLAllowIPLiteral(allow bool) URLOption
- func WithURLAllowLocalhost(allow bool) URLOption
- func WithURLAllowPrivateIP(allow bool) URLOption
- func WithURLAllowUserInfo(allow bool) URLOption
- func WithURLAllowedHosts(hosts ...string) URLOption
- func WithURLAllowedSchemes(schemes ...string) URLOption
- func WithURLBlockedHosts(hosts ...string) URLOption
- func WithURLCheckRedirects(maxRedirects int) URLOption
- func WithURLHTTPClient(client *http.Client) URLOption
- func WithURLMaxLength(maxLen int) URLOption
- func WithURLRedirectMethod(method string) URLOption
- func WithURLReputationChecker(checker URLReputationChecker) URLOption
- type URLRedirect
- type URLReputationChecker
- type URLReputationCheckerFunc
- type URLResult
- type URLValidator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidEmailConfig indicates that the email validation configuration is invalid. ErrInvalidEmailConfig = ewrap.New("invalid email validation config") // ErrInvalidURLConfig indicates that the URL validation configuration is invalid. ErrInvalidURLConfig = ewrap.New("invalid url validation config") // ErrEmailEmpty indicates that the email is empty. ErrEmailEmpty = ewrap.New("email is empty") // ErrEmailInvalid indicates that the email is invalid. ErrEmailInvalid = ewrap.New("email is invalid") // ErrEmailDisplayName indicates that the email display name is not allowed. ErrEmailDisplayName = ewrap.New("email display name is not allowed") // ErrEmailLocalPartInvalid indicates that the email local part is invalid. ErrEmailLocalPartInvalid = ewrap.New("email local part is invalid") // ErrEmailDomainInvalid indicates that the email domain is invalid. ErrEmailDomainInvalid = ewrap.New("email domain is invalid") // ErrEmailDomainTooLong indicates that the email domain is too long. ErrEmailDomainTooLong = ewrap.New("email domain is too long") // ErrEmailLocalPartTooLong indicates that the email local part is too long. ErrEmailLocalPartTooLong = ewrap.New("email local part is too long") // ErrEmailAddressTooLong indicates that the email address is too long. ErrEmailAddressTooLong = ewrap.New("email address is too long") // ErrEmailQuotedLocalPart indicates that the email quoted local part is not allowed. ErrEmailQuotedLocalPart = ewrap.New("email quoted local part is not allowed") // ErrEmailIPLiteralNotAllowed indicates that the email ip-literal domain is not allowed. ErrEmailIPLiteralNotAllowed = ewrap.New("email ip-literal domain is not allowed") // ErrEmailIDNNotAllowed indicates that the email idn domains are not allowed. ErrEmailIDNNotAllowed = ewrap.New("email idn domains are not allowed") // ErrEmailDomainLookupFailed indicates that the email domain lookup failed. ErrEmailDomainLookupFailed = ewrap.New("email domain lookup failed") // ErrEmailDomainUnverified indicates that the email domain is unverified. ErrEmailDomainUnverified = ewrap.New("email domain is unverified") // ErrURLInvalid indicates that the URL is invalid. ErrURLInvalid = ewrap.New("url is invalid") // ErrURLTooLong indicates that the URL is too long. ErrURLTooLong = ewrap.New("url is too long") // ErrURLSchemeNotAllowed indicates that the URL scheme is not allowed. ErrURLSchemeNotAllowed = ewrap.New("url scheme is not allowed") // ErrURLHostMissing indicates that the URL host is required. ErrURLHostMissing = ewrap.New("url host is required") // ErrURLUserInfoNotAllowed indicates that the URL userinfo is not allowed. ErrURLUserInfoNotAllowed = ewrap.New("url userinfo is not allowed") // ErrURLHostNotAllowed indicates that the URL host is not allowed. ErrURLHostNotAllowed = ewrap.New("url host is not allowed") // ErrURLPrivateIPNotAllowed indicates that the URL private IP is not allowed. ErrURLPrivateIPNotAllowed = ewrap.New("url private ip is not allowed") // ErrURLRedirectNotAllowed indicates that URL redirects are not allowed. ErrURLRedirectNotAllowed = ewrap.New("url redirect is not allowed") // ErrURLRedirectLoop indicates that a URL redirect loop was detected. ErrURLRedirectLoop = ewrap.New("url redirect loop detected") // ErrURLRedirectLimit indicates that the URL redirect limit was exceeded. ErrURLRedirectLimit = ewrap.New("url redirect limit exceeded") // ErrURLReputationFailed indicates that the URL reputation check failed. ErrURLReputationFailed = ewrap.New("url reputation check failed") // ErrURLReputationBlocked indicates that the URL reputation check blocked the URL. ErrURLReputationBlocked = ewrap.New("url reputation blocked") )
Functions ¶
This section is empty.
Types ¶
type DNSResolver ¶
type DNSResolver interface {
LookupMX(ctx context.Context, name string) ([]*net.MX, error)
LookupHost(ctx context.Context, name string) ([]string, error)
}
DNSResolver abstracts DNS lookups for email validation.
type EmailOption ¶
type EmailOption func(*emailOptions) error
EmailOption configures EmailValidator.
func WithEmailAllowARecordFallback ¶
func WithEmailAllowARecordFallback(allow bool) EmailOption
WithEmailAllowARecordFallback enables A/AAAA fallback when MX is missing.
func WithEmailAllowDisplayName ¶
func WithEmailAllowDisplayName(allow bool) EmailOption
WithEmailAllowDisplayName permits display names like "Name <[email protected]>".
func WithEmailAllowIDN ¶
func WithEmailAllowIDN(allow bool) EmailOption
WithEmailAllowIDN permits IDN domains and normalizes them to ASCII.
func WithEmailAllowIPLiteral ¶
func WithEmailAllowIPLiteral(allow bool) EmailOption
WithEmailAllowIPLiteral permits [ip] literal domains.
func WithEmailAllowQuotedLocal ¶
func WithEmailAllowQuotedLocal(allow bool) EmailOption
WithEmailAllowQuotedLocal permits quoted local parts.
func WithEmailDNSResolver ¶
func WithEmailDNSResolver(resolver DNSResolver) EmailOption
WithEmailDNSResolver sets a custom DNS resolver.
func WithEmailRequireMX ¶
func WithEmailRequireMX(require bool) EmailOption
WithEmailRequireMX enforces MX records when domain verification is enabled.
func WithEmailRequireTLD ¶
func WithEmailRequireTLD(require bool) EmailOption
WithEmailRequireTLD requires a dot in the domain part.
func WithEmailVerifyDomain ¶
func WithEmailVerifyDomain(verify bool) EmailOption
WithEmailVerifyDomain enables MX/A lookups for domain verification.
type EmailResult ¶
type EmailResult struct {
Address string
LocalPart string
Domain string
DomainASCII string
DomainVerified bool
VerifiedByMX bool
VerifiedByA bool
}
EmailResult contains normalized email details.
type EmailValidator ¶
type EmailValidator struct {
// contains filtered or unexported fields
}
EmailValidator validates email addresses with optional DNS checks.
func NewEmailValidator ¶
func NewEmailValidator(opts ...EmailOption) (*EmailValidator, error)
NewEmailValidator constructs a validator with optional configuration.
func (*EmailValidator) Validate ¶
func (v *EmailValidator) Validate(ctx context.Context, input string) (EmailResult, error)
Validate validates an email address and optionally verifies its domain.
type ReputationResult ¶
type ReputationResult struct {
Verdict ReputationVerdict
Reason string
}
ReputationResult describes a reputation check result.
type ReputationVerdict ¶
type ReputationVerdict int
ReputationVerdict indicates reputation outcome.
const ( // ReputationUnknown indicates an unknown reputation verdict. ReputationUnknown ReputationVerdict = iota // ReputationAllowed indicates an allowed reputation verdict. ReputationAllowed // ReputationBlocked indicates a blocked reputation verdict. ReputationBlocked )
type StaticReputation ¶
type StaticReputation struct {
// contains filtered or unexported fields
}
StaticReputation checks against allow/block lists.
func NewStaticReputation ¶
func NewStaticReputation(allowHosts, blockHosts []string) *StaticReputation
NewStaticReputation constructs a static checker.
func (*StaticReputation) Check ¶
func (s *StaticReputation) Check(_ context.Context, target *url.URL) (ReputationResult, error)
Check implements URLReputationChecker.
type URLOption ¶
type URLOption func(*urlOptions) error
URLOption configures URLValidator.
func WithURLAllowIDN ¶
WithURLAllowIDN allows IDN hostnames.
func WithURLAllowIPLiteral ¶
WithURLAllowIPLiteral allows IP literal hosts.
func WithURLAllowLocalhost ¶
WithURLAllowLocalhost allows localhost hostnames.
func WithURLAllowPrivateIP ¶
WithURLAllowPrivateIP allows private/loopback IPs.
func WithURLAllowUserInfo ¶
WithURLAllowUserInfo allows userinfo in URLs.
func WithURLAllowedHosts ¶
WithURLAllowedHosts restricts validation to specific hosts.
func WithURLAllowedSchemes ¶
WithURLAllowedSchemes sets allowed schemes.
func WithURLBlockedHosts ¶
WithURLBlockedHosts blocks specific hosts.
func WithURLCheckRedirects ¶
WithURLCheckRedirects enables redirect checks with a max hop count.
func WithURLHTTPClient ¶
WithURLHTTPClient sets a custom HTTP client for redirect checks.
func WithURLMaxLength ¶
WithURLMaxLength sets the max URL length.
func WithURLRedirectMethod ¶
WithURLRedirectMethod sets the HTTP method for redirect checks.
func WithURLReputationChecker ¶
func WithURLReputationChecker(checker URLReputationChecker) URLOption
WithURLReputationChecker sets a reputation checker.
type URLRedirect ¶
URLRedirect captures a single redirect hop.
type URLReputationChecker ¶
type URLReputationChecker interface {
Check(ctx context.Context, target *url.URL) (ReputationResult, error)
}
URLReputationChecker evaluates a URL's reputation.
type URLReputationCheckerFunc ¶
URLReputationCheckerFunc adapts a function to URLReputationChecker.
func (URLReputationCheckerFunc) Check ¶
func (f URLReputationCheckerFunc) Check(ctx context.Context, target *url.URL) (ReputationResult, error)
Check implements URLReputationChecker.
type URLResult ¶
type URLResult struct {
NormalizedURL string
FinalURL string
Redirects []URLRedirect
Reputation ReputationResult
}
URLResult describes URL validation output.
type URLValidator ¶
type URLValidator struct {
// contains filtered or unexported fields
}
URLValidator validates URLs with optional redirect and reputation checks.
func NewURLValidator ¶
func NewURLValidator(opts ...URLOption) (*URLValidator, error)
NewURLValidator constructs a validator with options.