View Source Vx.String (Vx v0.4.0)

The String type.

Summary

Types

t()

The string type.

Functions

Requires a string to match the given regex.

Requires a string to be at most length characters long.

Requires a string to be at least length characters long.

Requires a string to be non-empty.

Requires a string to be non-empty after stripping leading and trailing whitespace.

t()

Builds a new String type.

Types

@opaque t()

The string type.

Functions

Link to this function

format(schema \\ t(), regex)

View Source
@spec format(t(), Regex.t()) :: t()

Requires a string to match the given regex.

Examples

iex> Vx.String.t() |> Vx.String.format(~r/\d+/) |> Vx.validate!("123")
:ok

iex> Vx.String.t() |> Vx.String.format(~r/\d+/) |> Vx.validate!("foo")
** (Vx.Error) must match expected format
Link to this function

max_length(schema \\ t(), length)

View Source
@spec max_length(t(), non_neg_integer()) :: t()

Requires a string to be at most length characters long.

Examples

iex> Vx.String.t() |> Vx.String.max_length(3) |> Vx.validate!("foo")
:ok

iex> Vx.String.t() |> Vx.String.max_length(3) |> Vx.validate!("fo")
:ok

iex> Vx.String.t() |> Vx.String.max_length(3) |> Vx.validate!("fooo")
** (Vx.Error) must be at most 3 characters
Link to this function

min_length(schema \\ t(), length)

View Source
@spec min_length(t(), non_neg_integer()) :: t()

Requires a string to be at least length characters long.

Examples

iex> Vx.String.t() |> Vx.String.min_length(3) |> Vx.validate!("foo")
:ok

iex> Vx.String.t() |> Vx.String.min_length(3) |> Vx.validate!("foob")
:ok

iex> Vx.String.t() |> Vx.String.min_length(3) |> Vx.validate!("fo")
** (Vx.Error) must be at least 3 characters
Link to this function

non_empty(schema \\ t())

View Source
@spec non_empty(t()) :: t()

Requires a string to be non-empty.

Examples

iex> Vx.String.t() |> Vx.String.non_empty() |> Vx.validate!("foo")
:ok

iex> Vx.String.t() |> Vx.String.non_empty() |> Vx.validate!("   ")
:ok

iex> Vx.String.t() |> Vx.String.non_empty() |> Vx.validate!("")
** (Vx.Error) must not be empty
@spec present(t()) :: t()

Requires a string to be non-empty after stripping leading and trailing whitespace.

Examples

iex> Vx.String.t() |> Vx.String.present() |> Vx.validate!("foo")
:ok

iex> Vx.String.t() |> Vx.String.present() |> Vx.validate!("")
** (Vx.Error) must be present

iex> Vx.String.t() |> Vx.String.present() |> Vx.validate!("   ")
** (Vx.Error) must be present
@spec t() :: t()

Builds a new String type.

Examples

iex> Vx.String.t() |> Vx.validate!("foo")
:ok

iex> Vx.String.t() |> Vx.validate!(123)
** (Vx.Error) must be a string