format

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

schema/format

String-format validators for the schema package. Each exported function (Email, Color, ObjectID, IPv4, IPv6, Hostname, URI, Date, DateTime, Time, ISO8601, HTML, NoHTML, Markdown, In, NotIn, MatchRegex, Token, Username, WebFinger, UnsafeAny) is a Generator — it takes a configuration arg and returns a StringFormat closure that validates (and may rewrite) a string. See the parent schema README.

What matters here

  • A format is registered via schema.UseFormat, which is only safe during init/startup. The registry freezes the first time validation reads from it; registering a format afterward is rejected (reported, then ignored). Register all custom formats before any schema validation runs.
  • Empty string is always allowed. Every validator returns early on "" without error — "required" is enforced by the schema element (String.Required), not by the format. A format only constrains the shape of a non-empty value.
  • A StringFormat returns (value, error) and may REWRITE the value. Validators are part of a coercion pipeline (see validate_String_Formats in the parent package), so returning a changed string is normal, not a bug — the caller tracks the change.
  • Many Generators ignore their arg (e.g. Email, IPv4); the parameter exists to satisfy the uniform Generator signature. In/NotIn/MatchRegex are the ones that actually consume arg (the allowed set / pattern).
  • The validators here are the trust boundary for untrusted strings. NoHTML/HTML gate markup (and lean on ../../html + bluemonday); URI/Hostname/IP* constrain network identifiers. Tighten here, not at call sites.
  • Markup formats are guarded; unsafe-any is the only escape hatch. html sanitizes and markdown validates. A field whose safety is owned by a different trust boundary has to opt out with unsafe-any, which is deliberately generic and blunt — say why in a comment wherever you reach for it.
  • A second sanitize is not automatically safer. This package's HTML policy is deliberately strict (no iframes, no styling, rel="nofollow" added). Re-filtering markup that an application already sanitized under its own wider policy silently deletes content — embeds especially. When an application's renderer has already sanitized a value, do not run it through html again; stacking two policies is safe by accident, not by design.
  • Markdown cannot be made safe by rewriting its source. Sanitizing the source is a category error: it corrupts a < b in prose and fenced code blocks that quote markup, and an unregistered format name falls back to NoHTML, which strips tags and collapses the whitespace Markdown depends on. So markdown validates rather than rewrites. It renders the source twice, once honoring raw HTML and once not, and rejects the value when the two disagree — which happens exactly when the source carries raw HTML or a dangerous URL. That test needs no policy and no allow-list, so it can never argue with whatever sanitizer the application applies later: it accepts pure Markdown (links keep no added rel, fenced code keeps its language class) and rejects all embedded HTML, benign tags included. Cost is two renders, linear in length — pair it with a MaxLength, which the schema checks before any format function runs.

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

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

type StringFormat func(string) (string, error)

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.

Jump to

Keyboard shortcuts

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