validation

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package validation provides the input checks that CLI commands share:

  • Email address format
  • UUID IDs and path arguments
  • Domain name format
  • Page limits
  • API key scopes and IP allow lists
  • API URLs and loopback hosts

A check of a flag or an argument returns a validation error from the errors package, which exits with 4; CheckAPIURL returns only the reason, which its callers put in their own error.

Index

Constants

View Source
const APIKeyNotPrintable = "must be printable ASCII, without control or non-ASCII characters"

APIKeyNotPrintable is the reason an API key with a character that is not printable ASCII is refused: no AhaSend API key holds one, and an HTTP header cannot carry it, so a request with it could only fail.

Variables

View Source
var ScopeGroups = []ScopeGroup{
	{"Messages", []string{
		"messages:send:all", "messages:send:{domain}",
		"messages:cancel:all", "messages:cancel:{domain}",
		"messages:read:all", "messages:read:{domain}",
	}},
	{"Domains", []string{"domains:read", "domains:write", "domains:delete:all", "domains:delete:{domain}"}},
	{"Accounts", []string{
		"accounts:read", "accounts:write", "accounts:billing",
		"accounts:members:read", "accounts:members:add", "accounts:members:update", "accounts:members:remove",
	}},
	{"Webhooks", []string{
		"webhooks:read:all", "webhooks:read:{domain}",
		"webhooks:write:all", "webhooks:write:{domain}",
		"webhooks:delete:all", "webhooks:delete:{domain}",
	}},
	{"Routes", []string{
		"routes:read:all", "routes:read:{domain}",
		"routes:write:all", "routes:write:{domain}",
		"routes:delete:all", "routes:delete:{domain}",
	}},
	{"Suppressions", []string{"suppressions:read", "suppressions:write", "suppressions:delete", "suppressions:wipe"}},
	{"Templates", []string{"templates:read", "templates:write", "templates:delete"}},
	{"Campaigns", []string{"campaigns:read", "campaigns:write", "campaigns:delete", "campaigns:send"}},
	{"Contacts", []string{"contacts:read", "contacts:write", "contacts:delete"}},
	{"Lists", []string{"lists:read", "lists:write", "lists:delete"}},
	{"Segments", []string{"segments:read", "segments:write", "segments:delete"}},
	{"SMTP Credentials", []string{
		"smtp-credentials:read:all", "smtp-credentials:read:{domain}",
		"smtp-credentials:write:all", "smtp-credentials:write:{domain}",
		"smtp-credentials:delete:all", "smtp-credentials:delete:{domain}",
	}},
	{"Statistics", []string{"statistics:read:all", "statistics:read:{domain}"}},
	{"API Keys", []string{"api-keys:read", "api-keys:write", "api-keys:delete"}},
	{"Sub-Accounts", []string{
		"sub-accounts:read", "sub-accounts:write", "sub-accounts:delete", "sub-accounts:suspend", "sub-accounts:usage",
	}},
	{"Sub-Account API Keys", []string{
		"sub-account-api-keys:read", "sub-account-api-keys:write", "sub-account-api-keys:delete",
	}},
}

ScopeGroups are the API key scopes the API accepts, grouped and ordered as the API reference lists them. The API key commands and the sub-account API key commands check and list scopes from this one list, and accept the other names of scopeAliases too; a sub-account's key cannot have the parent-only scopes (see IsParentOnlyScope). A scope ending in {domain} takes one of the key's account's domains in its place, such as messages:send:{example.com}.

Functions

func ASCIIDomain added in v0.3.0

func ASCIIDomain(domain string) string

ASCIIDomain returns domain with each label that is not ASCII written as "xn--" and its Punycode, as DNS resolves an international domain name, such as "xn--bcher-kva.de" for "bücher.de". ASCII labels are kept as they are. It does no other mapping or checking: a caller lowercases the domain first.

func CanonicalScope added in v0.3.0

func CanonicalScope(scope string) string

CanonicalScope returns scope as the API compares scopes: under the name ScopeGroups lists, and the domain of a scope that takes one in lower case and as scopeDomain reads it. It serves only to compare scopes; the CLI sends each scope as it was given.

func CheckAPIURL added in v0.3.0

func CheckAPIURL(rawURL string) error

CheckAPIURL returns why rawURL cannot be the AhaSend API URL, or nil. The API URL passes CheckHTTPURL and has nothing after its host: the API's paths start at the root of the host, so a path would be lost, and the API key is the only credential the CLI sends.

func CheckHTTPURL added in v0.3.0

func CheckHTTPURL(rawURL, example string) error

CheckHTTPURL returns why rawURL is not an HTTP or HTTPS URL with a host and no user name or password, or nil; the reason ends with example, such as https://example.com/webhook. The CLI shows the URLs it takes, such as in the output of webhooks get, so a password in one would show too.

func CheckIPAllowList added in v0.3.0

func CheckIPAllowList(entries []string) error

CheckIPAllowList checks the --ip-allow-list values of an API key: each is a CIDR block or an IP address other than an allow-all prefix, and they make at most 100 distinct blocks.

func InvalidValueError added in v0.3.0

func InvalidValueError(name, value, reason string) error

InvalidValueError is the validation error of value, the value of the input called name, a flag such as "--domain" or an argument such as "domain", with the reason it is invalid: `invalid --domain value "x": reason`.

func IsDomainName added in v0.3.0

func IsDomainName(domain string) bool

IsDomainName reports whether domain is a domain name: dot-separated labels of letters, digits and inner hyphens, 253 characters at most.

func IsEmailAddress added in v0.3.0

func IsEmailAddress(email string) bool

IsEmailAddress reports whether email is a bare email address: one that net/mail parses as itself, with no display name, angle brackets, quotes or surrounding spaces. It accepts addresses such as o'brien@example.com and non-ASCII ones, which the mail format allows, and leaves deliverability to the API.

func IsLoopbackHost added in v0.3.0

func IsLoopbackHost(host string) bool

IsLoopbackHost reports whether host, a host name or IP address without a port, is this machine: localhost or a loopback address such as 127.0.0.1 or ::1. Data sent to it never leaves the machine.

func IsParentOnlyScope added in v0.3.0

func IsParentOnlyScope(scope string) bool

IsParentOnlyScope reports whether scope manages sub-accounts or their keys, which the API refuses to a key that a sub-account owns.

func IsPrintableASCII added in v0.3.0

func IsPrintableASCII(s string) bool

IsPrintableASCII reports whether s holds only printable ASCII: letters, digits, punctuation and spaces, without control or non-ASCII characters.

func ParseMessageID added in v0.3.0

func ParseMessageID(fieldName, value string) (string, error)

ParseMessageID checks a message ID argument, which is a UUID or a UUID and a domain in angle brackets, such as <uuid@example.com>. It returns the ID to send to the API.

func ParseUUIDField added in v0.3.0

func ParseUUIDField(fieldName, value string) (uuid.UUID, error)

ParseUUIDField parses a named field that must contain a UUID, returning the same errors as ValidateUUIDField.

func ParseUUIDs added in v0.3.0

func ParseUUIDs(fieldName string, values []string) ([]uuid.UUID, error)

ParseUUIDs parses each of values, IDs of the field fieldName such as "list ID", like ParseUUIDField. It returns nil for no values.

func PlainHTTPWarning added in v0.3.0

func PlainHTTPWarning(subject, rawURL, data string) string

PlainHTTPWarning is the warning about rawURL, a URL that SendsInClear: subject names it, such as "the API URL" or "--url", and data is what it sends unencrypted, such as "the API key".

func SendsInClear added in v0.3.0

func SendsInClear(rawURL string) bool

SendsInClear reports whether rawURL sends data over plain HTTP to a host that is not a loopback host.

func ValidateDomainName

func ValidateDomainName(name, domain string) error

ValidateDomainName validates domain, the value of the input called name: a flag such as "--domain" or an argument such as "domain".

func ValidateDomainNames added in v0.3.0

func ValidateDomainNames(name string, domains []string) error

ValidateDomainNames validates each of domains like ValidateDomainName.

func ValidateEmail

func ValidateEmail(name, email string) error

ValidateEmail checks that email, the value of the input name, a flag such as "--from" or an argument such as "email", is an email address (see IsEmailAddress): `invalid --from value "x": not an email address`. An empty value, or one of only white space, fails with `--from cannot be empty`.

func ValidateOptionalDomainName added in v0.3.0

func ValidateOptionalDomainName(name string, domain *string) error

ValidateOptionalDomainName validates domain like ValidateDomainName, when there is one.

func ValidateOptionalEmail added in v0.3.0

func ValidateOptionalEmail(name string, email *string) error

ValidateOptionalEmail validates email like ValidateEmail, when there is one.

func ValidatePageLimit

func ValidatePageLimit(limit int32) error

ValidatePageLimit validates an optional page limit

func ValidatePathSegment added in v0.3.0

func ValidatePathSegment(fieldName, value string) error

ValidatePathSegment validates that the argument fieldName, such as "domain" or "contact ID or email", can address one path segment: not empty or blank, not a dot segment and free of control characters, the values the SDK refuses to put in a path or that address nothing. Its format is left to the API.

func ValidateScope

func ValidateScope(scope, helpCommand string) error

ValidateScope checks that scope, a value of --scope, is one of ScopeGroups or of scopeAliases, with a domain name in place of {domain}. An unknown scope's error points to the help of helpCommand, which lists the scopes.

func ValidateUUIDField

func ValidateUUIDField(fieldName, value string) error

ValidateUUIDField validates that a named field contains a valid UUID format

Types

type ScopeGroup added in v0.3.0

type ScopeGroup struct {
	Name   string
	Scopes []string
}

ScopeGroup is a group of API key scopes, such as the scopes of messages.

Jump to

Keyboard shortcuts

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