Documentation
¶
Overview ¶
Optional types may be either empty (null) or contain a value that must pass validation.
Optional types support the following encoding/decoding formats: - json - sql - text
Index ¶
- type Any
- type Base64
- type CIDR
- type Charset
- type CountryAlpha
- type CountryAlpha2
- type CountryAlpha3
- type CurrencyAlpha
- type Custom
- func (c Custom[T, V]) Get() (T, bool)
- func (c Custom[T, V]) HasValue() bool
- func (c Custom[T, V]) MarshalJSON() ([]byte, error)
- func (c Custom[T, V]) MarshalText() ([]byte, error)
- func (c Custom[T, V]) Must() T
- func (c *Custom[T, V]) Parse(value *T) error
- func (c *Custom[T, V]) Scan(src any) error
- func (c *Custom[T, V]) TypeValidate() error
- func (c *Custom[T, V]) UnmarshalJSON(data []byte) error
- func (c *Custom[T, V]) UnmarshalText(data []byte) error
- func (c Custom[T, V]) Value() (driver.Value, error)
- type Email
- type Even
- type HTTPURL
- type IP
- type IPV4
- type IPV6
- type InFuture
- type InPast
- type JSON
- type LangAlpha
- type LangAlpha2
- type LangAlpha3
- type Latitude
- type Longitude
- type MAC
- type MIME
- type Negative
- type Negative0
- type NonEmpty
- type NonEmptySlice
- type NonZero
- type NonZeroCharset
- type Odd
- type Positive
- type Positive0
- type URL
- type UUID
- type Unique
- type UniqueSlice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base64 ¶
type Base64[T constraint.Text] struct { Custom[T, validate.Base64[T]] }
Base64 accepts valid base64 encoded strings
type CIDR ¶
type CIDR[T constraint.Text] struct { Custom[T, validate.CIDR[T]] }
CIDR accepts CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291
type Charset ¶
Charset accepts text which contains only runes acceptable by filter
NOTE: empty strings will also pass. Use NonZeroCharset if you need non-empty strings
type CountryAlpha ¶
type CountryAlpha[T constraint.Text] struct { Custom[T, validate.CountryAlpha[T]] }
CountryAlpha accepts either CountryAlpha2 or CountryAlpha3
type CountryAlpha2 ¶
type CountryAlpha2[T constraint.Text] struct { Custom[T, validate.CountryAlpha2[T]] }
CountryAlpha2 accepts case-insensitive ISO 3166 2-letter country code
type CountryAlpha3 ¶
type CountryAlpha3[T constraint.Text] struct { Custom[T, validate.CountryAlpha3[T]] }
CountryAlpha3 accepts case-insensitive ISO 3166 3-letter country code
type CurrencyAlpha ¶
type CurrencyAlpha[T constraint.Text] struct { Custom[T, validate.CurrencyAlpha[T]] }
CurrencyAlpha accepts case-insensitive ISO 4217 alphabetic currency code
type Custom ¶
Custom optional type. When given non-null value it errors if validation fails
func (Custom[T, V]) Get ¶
Get returns the contained value and a boolean stating its presence. True if value exists, false otherwise.
Panics if value was not validated yet
func (Custom[T, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Custom[T, V]) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface
func (Custom[T, V]) Must ¶
func (c Custom[T, V]) Must() T
Must returns the contained value and panics if it does not have one. You can check for its presence using Custom.HasValue or use a more safe alternative Custom.Get
func (*Custom[T, V]) Parse ¶
Parse checks if given value is valid. If it is, a value is used to initialize this type. Initialized type is validated, therefore it is safe to call Custom.Get afterwards
Passing nil pointer results a valid empty instance.
func (*Custom[T, V]) Scan ¶
Scan implements the sql.Scanner interface.
Use Custom.Parse instead if you need to construct this value manually
func (*Custom[T, V]) TypeValidate ¶
TypeValidate implements the validate.TypeValidateable interface. You should not call this function directly.
func (*Custom[T, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Custom[T, V]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface
func (Custom[T, V]) Value ¶
Value implements the driver.Valuer interface.
Use Custom.Get method instead for getting the go value
type Email ¶
type Email[T constraint.Text] struct { Custom[T, validate.Email[T]] }
Email accepts a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
type Even ¶
type Even[T constraint.Integer] struct { Custom[T, validate.Even[T]] }
Even accepts real numbers divisible by two
type HTTPURL ¶
type HTTPURL[T constraint.Text] struct { Custom[T, validate.HTTPURL[T]] }
HTTPURL accepts a single http(s) url.
See also URL
type IP ¶
type IP[T constraint.Text] struct { Custom[T, validate.IP[T]] }
IP accepts an IP address. The address can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
type IPV4 ¶
type IPV4[T constraint.Text] struct { Custom[T, validate.IPV4[T]] }
IP accepts an IP V4 address (e.g. "192.0.2.1").
type IPV6 ¶
type IPV6[T constraint.Text] struct { Custom[T, validate.IPV6[T]] }
IP accepts an IP V6 address, including IPv4-mapped IPv6 addresses. The address can be regular IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18")
type InFuture ¶
type InFuture[T constraint.Time] struct { Custom[T, validate.InFuture[T]] }
InFuture accepts any time after current timestamp
See also InPast
type InPast ¶
type InPast[T constraint.Time] struct { Custom[T, validate.InPast[T]] }
InPast accepts any time before current timestamp
See also InFuture
type JSON ¶
type JSON[T constraint.Text] struct { Custom[T, validate.JSON[T]] }
JSON accepts valid json encoded text
type LangAlpha ¶
type LangAlpha[T constraint.Text] struct { Custom[T, validate.LangAlpha[T]] }
LangAlpha accepts either LangAlpha2 or LangAlpha3
type LangAlpha2 ¶
type LangAlpha2[T constraint.Text] struct { Custom[T, validate.LangAlpha2[T]] }
LangAlpha2 accepts case-insesitive ISO 639 2-letter language code.
type LangAlpha3 ¶
type LangAlpha3[T constraint.Text] struct { Custom[T, validate.LangAlpha3[T]] }
LangAlpha2 accepts case-insesitive ISO 639 3-letter language code.
type Latitude ¶
type Latitude[T constraint.Real] struct { Custom[T, validate.Latitude[T]] }
Latitude accepts any number in the range [-90; 90]
See also Longitude
type Longitude ¶
type Longitude[T constraint.Real] struct { Custom[T, validate.Longitude[T]] }
Longitude accepts any number in the range [-180; 180]
See also Latitude
type MAC ¶
type MAC[T constraint.Text] struct { Custom[T, validate.MAC[T]] }
MAC accepts an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address
type MIME ¶
type MIME[T constraint.Text] struct { Custom[T, validate.MIME[T]] }
MIME accepts RFC 1521 mime type string
type Negative ¶
type Negative[T constraint.Real] struct { Custom[T, validate.Negative[T]] }
Negative accepts all negative real numbers and zero
See also Positive
type Negative0 ¶
type Negative0[T constraint.Real] struct { Custom[T, validate.Negative0[T]] }
Negative0 accepts all negative real numbers including zero.
See Negative for zero excluding variant.
type NonEmpty ¶
NonEmpty accepts a non-empty slice-like (len > 0)
See NonEmptySlice for a slice shortcut
type NonEmptySlice ¶
type NonEmptySlice[T any] struct { Custom[[]T, validate.NonEmptySlice[T]] }
NonEmpty accepts a non-empty slice (len > 0)
See NonEmpty for a more generic version
type NonZero ¶
type NonZero[T comparable] struct { Custom[T, validate.NonZero[T]] }
NonZero accepts all non-zero values
The zero value is: - 0 for numeric types, - false for the boolean type, and - "" (the empty string) for strings.
type NonZeroCharset ¶
type Odd ¶
type Odd[T constraint.Integer] struct { Custom[T, validate.Odd[T]] }
Odd accepts real numbers not divisible by two
type Positive ¶
type Positive[T constraint.Real] struct { Custom[T, validate.Positive[T]] }
Positive accepts all positive real numbers and zero
See also Negative
type Positive0 ¶
type Positive0[T constraint.Real] struct { Custom[T, validate.Positive0[T]] }
Positive0 accepts all positive real numbers including zero.
See Positive for zero excluding variant.
type URL ¶
type URL[T constraint.Text] struct { Custom[T, validate.URL[T]] }
URL accepts a single url. The url may be relative (a path, without a host) or absolute (starting with a scheme)
See also HTTPURL
type UUID ¶
type UUID[T constraint.Text] struct { Custom[T, validate.UUID[T]] }
UUID accepts a properly formatted UUID in one of the following formats:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
type Unique ¶
type Unique[S ~[]T, T comparable] struct { Custom[S, validate.Unique[S, T]] }
Unique accepts a slice-like of unique values
See UniqueSlice for a slice shortcut
type UniqueSlice ¶
type UniqueSlice[T comparable] struct { Custom[[]T, validate.UniqueSlice[T]] }
Unique accepts a slice of unique values
See Unique for a more generic version