strfmt

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package strfmt is the catalogue of named string formats `@format` accepts: for each, the OpenAPI `format` keyword, the Go imports the generated check needs and the check itself. It is a leaf below the analyser (which accepts exactly these names), codegen (which emits the check and the keyword) and the LSP (which offers the names), so a format is defined in exactly one place.

(Named `strfmt`, not `formats`, to stay clearly distinct from the `internal/format` printer package that renders DSL source.)

Index

Constants

This section is empty.

Variables

View Source
var All = []Spec{

	{Name: "email", Label: "email", Imports: []string{"net/mail"},
		Cond: `_, _err := mail.ParseAddress(%s); _err != nil`},

	{Name: "url", Label: "URL", Imports: []string{"net/url"},
		Cond: `_u, _err := url.Parse(%s); _err != nil || (_u.Scheme != "http" && _u.Scheme != "https")`},

	{Name: "uri", Label: "URI", Imports: []string{"net/url"},
		Cond: `_u, _err := url.Parse(%s); _err != nil || _u.Scheme == ""`},

	{Name: "uuid", Label: "UUID",
		Pattern: `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`},

	{Name: "datetime", Label: "RFC 3339 datetime", OAS: "date-time", Imports: []string{"time"},
		Cond: `_, _err := time.Parse(time.RFC3339, %s); _err != nil`},

	{Name: "date", Label: "date", Imports: []string{"time"},
		Cond: `_, _err := time.Parse(time.DateOnly, %s); _err != nil`},

	{Name: "time", Label: "time", Imports: []string{"time"},
		Cond: `_, _err := time.Parse(time.TimeOnly, %s); _err != nil`},

	{Name: "phone", Label: "phone", Pattern: `^\+?[0-9 ()-]{6,20}$`},

	{Name: "ipv4", Label: "IPv4", Imports: []string{"net"},
		Cond: `_ip := net.ParseIP(%s); _ip == nil || _ip.To4() == nil`},

	{Name: "ipv6", Label: "IPv6", Imports: []string{"net"},
		Cond: `_ip := net.ParseIP(%s); _ip == nil || _ip.To4() != nil`},

	{Name: "cidr", Label: "CIDR", Imports: []string{"net"},
		Cond: `_, _, _err := net.ParseCIDR(%s); _err != nil`},

	{Name: "mac", Label: "MAC address", Imports: []string{"net"},
		Cond: `_, _err := net.ParseMAC(%s); _err != nil`},

	{Name: "creditcard", Label: "credit card number", Pattern: `^[0-9]{12,19}$`},

	{Name: "base64", Label: "base64", Imports: []string{"encoding/base64"},
		Cond: `_, _err := base64.StdEncoding.DecodeString(%s); _err != nil`},

	{Name: "base64url", Label: "base64url", Imports: []string{"encoding/base64"},
		Cond: `_, _err := base64.URLEncoding.DecodeString(%s); _err != nil`},

	{Name: "hexcolor", Label: "hex color", Pattern: `^#?[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`},

	{Name: "json", Label: "JSON", Imports: []string{"encoding/json"},
		Cond: `!json.Valid([]byte(%s))`},
}

All lists every format in documentation order (README §"Decorators by level").

Functions

func Names

func Names() []string

Names returns every format name in documentation order.

func OpenAPIFormat added in v1.7.0

func OpenAPIFormat(name string) string

OpenAPIFormat returns the OpenAPI `format` keyword for name: the catalogue's keyword where it differs from the DSL spelling, the name itself otherwise.

Types

type Spec added in v1.7.0

type Spec struct {
	Name  string // the `@format` argument
	Label string // the human label of the validation message: "not a valid <Label>"
	OAS   string // the OpenAPI `format` keyword when it differs from Name
	// Imports are the Go packages the generated check needs.
	Imports []string
	// Cond is a Go condition template that is true when the value is
	// INVALID, with one %s for the value expression. It may open with an
	// init statement (`_, _err := f(%s); _err != nil`), which slots into
	// Go's `if init; cond` form. Empty for a regex-backed format.
	Cond string
	// Pattern is the regular expression the value must match; the
	// generated validator compiles it once per file. Empty for a
	// stdlib-backed format.
	Pattern string
}

Spec is one named string format.

func Lookup added in v1.7.0

func Lookup(name string) (Spec, bool)

Lookup returns the spec for name.

Jump to

Keyboard shortcuts

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