Documentation
¶
Overview ¶
Package format provides the named string formats used by the schema package.
Each function here is a Generator: it takes the argument written in the schema (the part after the format name) and returns a StringFormat closure that validates one value. That closure returns the value it accepts, so a format may normalize as well as validate — trimming, case-folding, or rewriting into a canonical form on the way through.
The set covers the usual identifier and network formats (email, hostname, IPv4, IPv6, URI, URL, ObjectID, token, username, WebFinger), dates and times, constraints on the text itself (In, NotIn, MatchRegex, HasUppercase, HasLowercase, HasNumbers), and the content formats that decide how much markup survives (Text, NoHTML, HTML, Markdown, UnsafeAny).
Those content formats are a trust boundary: the format named on a string element is what decides whether a value is escaped, sanitized, or passed through untouched. Choose UnsafeAny only for values that are already trusted.
Index ¶
- type Generator
- type StringFormat
- func Color(arg string) StringFormat
- func Date(arg string) StringFormat
- func DateTime(arg string) StringFormat
- func Email(arg string) StringFormat
- func HTML(arg string) StringFormat
- func HasLowercase(arg string) StringFormat
- func HasNumbers(arg string) StringFormat
- func HasUppercase(arg string) StringFormat
- func Hostname(arg string) StringFormat
- func IPv4(arg string) StringFormat
- func IPv6(arg string) StringFormat
- func ISO8601(arg string) StringFormat
- func In(arg string) StringFormat
- func Markdown(arg string) StringFormat
- func MatchRegex(arg string) StringFormat
- func NoHTML(arg string) StringFormat
- func NotIn(arg string) StringFormat
- func ObjectID(arg string) StringFormat
- func Text(arg string) StringFormat
- func Time(arg string) StringFormat
- func Token(_ string) StringFormat
- func URI(arg string) StringFormat
- func URL(arg string) StringFormat
- func UnsafeAny(arg string) StringFormat
- func Username(_ string) StringFormat
- func WebFinger(arg string) StringFormat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Generator ¶ added in v0.10.0
type Generator func(string) StringFormat
Generator is a function that takes an optional parameter and generates a StringFormat function
type StringFormat ¶
StringFormat tries to force a value to fit the desired format. If it cannot safely convert the value into the specified format, then it returns empty string and an error message.
func Color ¶ added in v0.21.0
func Color(arg string) StringFormat
Color validates an email address using Go's built-in system email parser.
func Date ¶
func Date(arg string) StringFormat
Date returns a StringFormat that validates a value as an RFC-3339 full-date (e.g. "2026-03-04").
func DateTime ¶
func DateTime(arg string) StringFormat
DateTime returns a StringFormat that validates a value as an RFC-3339 date-time (e.g. "2026-03-04T13:02:00Z").
func Email ¶
func Email(arg string) StringFormat
Email validates an email address using Go's built-in system email parser.
func HTML ¶
func HTML(arg string) StringFormat
HTML allows basic HTML tags, but strips iframes, object, embed, style, script tags.
func HasLowercase ¶
func HasLowercase(arg string) StringFormat
HasLowercase returns a StringFormat requiring at least arg lowercase letters (default 1).
func HasNumbers ¶
func HasNumbers(arg string) StringFormat
HasNumbers returns a StringFormat requiring at least arg numeric digits (default 1).
func HasUppercase ¶
func HasUppercase(arg string) StringFormat
HasUppercase returns a StringFormat requiring at least arg uppercase letters (default 1).
func Hostname ¶
func Hostname(arg string) StringFormat
Hostname returns a StringFormat that validates a value as an RFC-1123 hostname.
func IPv4 ¶
func IPv4(arg string) StringFormat
IPv4 returns a StringFormat that validates a value as an IPv4 address.
func IPv6 ¶
func IPv6(arg string) StringFormat
IPv6 returns a StringFormat that validates a value as an IPv6 address.
func ISO8601 ¶ added in v0.25.25
func ISO8601(arg string) StringFormat
ISO8601 returns a StringFormat that validates a value as an ISO-8601 date string.
func In ¶
func In(arg string) StringFormat
In returns a StringFormat that requires the value to be one of the comma-separated options in arg.
func Markdown ¶ added in v0.35.0
func Markdown(arg string) StringFormat
Markdown accepts Markdown source that renders safely under any renderer, and rejects source carrying raw HTML or a dangerous URL.
func MatchRegex ¶
func MatchRegex(arg string) StringFormat
MatchRegex returns a StringFormat that requires the value to match the regular expression arg.
func NoHTML ¶
func NoHTML(arg string) StringFormat
NoHTML strips all HTML tags from a string and collapses whitespace into a single space character.
func NotIn ¶
func NotIn(arg string) StringFormat
NotIn returns a StringFormat that rejects the value if it is one of the comma-separated options in arg.
func ObjectID ¶
func ObjectID(arg string) StringFormat
ObjectID validates a mongodb-style identifier (24 hexadecimal characters)
func Text ¶ added in v0.29.0
func Text(arg string) StringFormat
Text strips all HTML tags from a string without collapsing whitespace.
func Time ¶
func Time(arg string) StringFormat
Time returns a StringFormat that validates a value as an RFC-3339 full-time (e.g. "13:02:00" or "13:02:00Z").
func Token ¶
func Token(_ string) StringFormat
Token validates a simple token string suitable for use as URL identifiers
func URI ¶
func URI(arg string) StringFormat
URI returns a StringFormat that validates a value as an absolute URI.
func URL ¶ added in v0.31.0
func URL(arg string) StringFormat
URL returns a StringFormat that validates a value as an absolute URL — one that parses with both a scheme and a host. This is stricter than URI, which also accepts opaque values like "mailto:" addresses and "urn:" references that have no host.
func UnsafeAny ¶ added in v0.7.0
func UnsafeAny(arg string) StringFormat
UnsafeAny leaves the string format untouched.
func Username ¶ added in v0.24.8
func Username(_ string) StringFormat
Username validates a simple token string suitable for use as URL identifiers
func WebFinger ¶ added in v0.25.9
func WebFinger(arg string) StringFormat
WebFinger validates an email address using Go's built-in system email parser.