validate

package
v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package validate provides input validation helpers with safe defaults.

Index

Constants

This section is empty.

Variables

View Source
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

func WithURLAllowIDN(allow bool) URLOption

WithURLAllowIDN allows IDN hostnames.

func WithURLAllowIPLiteral

func WithURLAllowIPLiteral(allow bool) URLOption

WithURLAllowIPLiteral allows IP literal hosts.

func WithURLAllowLocalhost

func WithURLAllowLocalhost(allow bool) URLOption

WithURLAllowLocalhost allows localhost hostnames.

func WithURLAllowPrivateIP

func WithURLAllowPrivateIP(allow bool) URLOption

WithURLAllowPrivateIP allows private/loopback IPs.

func WithURLAllowUserInfo

func WithURLAllowUserInfo(allow bool) URLOption

WithURLAllowUserInfo allows userinfo in URLs.

func WithURLAllowedHosts

func WithURLAllowedHosts(hosts ...string) URLOption

WithURLAllowedHosts restricts validation to specific hosts.

func WithURLAllowedSchemes

func WithURLAllowedSchemes(schemes ...string) URLOption

WithURLAllowedSchemes sets allowed schemes.

func WithURLBlockedHosts

func WithURLBlockedHosts(hosts ...string) URLOption

WithURLBlockedHosts blocks specific hosts.

func WithURLCheckRedirects

func WithURLCheckRedirects(maxRedirects int) URLOption

WithURLCheckRedirects enables redirect checks with a max hop count.

func WithURLHTTPClient

func WithURLHTTPClient(client *http.Client) URLOption

WithURLHTTPClient sets a custom HTTP client for redirect checks.

func WithURLMaxLength

func WithURLMaxLength(maxLen int) URLOption

WithURLMaxLength sets the max URL length.

func WithURLRedirectMethod

func WithURLRedirectMethod(method string) URLOption

WithURLRedirectMethod sets the HTTP method for redirect checks.

func WithURLReputationChecker

func WithURLReputationChecker(checker URLReputationChecker) URLOption

WithURLReputationChecker sets a reputation checker.

type URLRedirect

type URLRedirect struct {
	From       string
	To         string
	StatusCode int
}

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

type URLReputationCheckerFunc func(ctx context.Context, target *url.URL) (ReputationResult, error)

URLReputationCheckerFunc adapts a function to URLReputationChecker.

func (URLReputationCheckerFunc) Check

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.

func (*URLValidator) Validate

func (v *URLValidator) Validate(ctx context.Context, raw string) (URLResult, error)

Validate validates the URL, optionally checking redirects and reputation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL