optional

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any[T any] struct {
	Custom[T, validate.Any[T]]
}

Any accepts any value of T

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

type Charset[T constraint.Text, F charset.Filter] struct {
	Custom[T, validate.Charset0[T, F]]
}

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

type Custom[T any, V validate.Validator[T]] struct {
	// contains filtered or unexported fields
}

Custom optional type. When given non-null value it errors if validation fails

func (Custom[T, V]) Get

func (c Custom[T, V]) Get() (T, bool)

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]) HasValue

func (c Custom[T, V]) HasValue() bool

HasValue returns the presence of the contained value

func (Custom[T, V]) MarshalJSON

func (c Custom[T, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Custom[T, V]) MarshalText

func (c Custom[T, V]) MarshalText() ([]byte, error)

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

func (c *Custom[T, V]) Parse(value *T) error

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

func (c *Custom[T, V]) Scan(src any) error

Scan implements the sql.Scanner interface.

Use Custom.Parse instead if you need to construct this value manually

func (*Custom[T, V]) TypeValidate

func (c *Custom[T, V]) TypeValidate() error

TypeValidate implements the validate.TypeValidateable interface. You should not call this function directly.

func (*Custom[T, V]) UnmarshalJSON

func (c *Custom[T, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Custom[T, V]) UnmarshalText

func (c *Custom[T, V]) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface

func (Custom[T, V]) Value

func (c Custom[T, V]) Value() (driver.Value, error)

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

type NonEmpty[S ~[]T, T any] struct {
	Custom[S, validate.NonEmpty[S, T]]
}

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 NonZeroCharset[T constraint.Text, F charset.Filter] struct {
	Custom[T, validate.Charset[T, F]]
}

NonZeroCharset combines NonZero and Charset

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

Jump to

Keyboard shortcuts

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