constructor v1.1.0 Constructor.Validate
Common validations for struct fields.
Link to this section Summary
Functions
Checks that value is an atom
Checks if a value is a boolean with Kernel.is_boolean/1
Checks if input is a Date.t/0
Checks that value is a float
Checks that value is an integer
Checks that value is a list
Checks that value is both a string, and not nonempty ("")
Checks if a value is a binary with Kernel.is_binary/1
Checks that value is a list and that each value is a string
The same as is_string/1, except that it will return {:ok, nil} if the value is nil
Checks that value is a uuid
Link to this section Types
error()
error() :: {:error, String.t()}
error() :: {:error, String.t()}
Link to this section Functions
is_atom(value)
Checks that value is an atom.
Examples
iex> Constructor.Validate.is_atom(:foo)
{:ok, :foo}
iex> Constructor.Validate.is_atom(%{foo: "bar"})
{:error, "must be an atom"}
is_boolean(value)
Checks if a value is a boolean with Kernel.is_boolean/1
Examples
iex> Constructor.Validate.is_boolean(true)
{:ok, true}
iex> Constructor.Validate.is_boolean("true")
{:error, "must be a boolean"}
is_date(value)
Checks if input is a Date.t/0
Examples
iex> Constructor.Validate.is_date(%Date{year: 1999, month: 1, day: 1})
{:ok, %Date{year: 1999, month: 1, day: 1}}
iex> Constructor.Validate.is_date("foo")
{:error, "must be a Date"}
is_float(value)
Checks that value is a float.
Examples
iex> Constructor.Validate.is_float(12.3)
{:ok, 12.3}
iex> Constructor.Validate.is_float(12)
{:error, "must be a float"}
is_integer(value)
Checks that value is an integer.
Examples
iex> Constructor.Validate.is_integer(12)
{:ok, 12}
iex> Constructor.Validate.is_integer("12")
{:error, "must be an integer"}
is_list(value)
Checks that value is a list.
Examples
iex> Constructor.Validate.is_list(["foo", "bar"])
{:ok, ["foo", "bar"]}
iex> Constructor.Validate.is_list(%{foo: "bar"})
{:error, "must be a list"}
is_not_blank(value)
Checks that value is both a string, and not nonempty ("").
Examples
iex> Constructor.Validate.is_not_blank("a")
{:ok, "a"}
iex> Constructor.Validate.is_not_blank("")
{:error, "must not be blank"}
iex> Constructor.Validate.is_not_blank(12)
{:error, "must be a string"}
is_string(value)
Checks if a value is a binary with Kernel.is_binary/1.
That means it will return {:ok, value} for both traditional Erlang binary and Elixir's
String.t/0. This is usually what people mean when they say "string" in Elixir. However,
string is a different type in Erlang, so make sure you make necessary conversions with another
function.
Examples
iex> Constructor.Validate.is_string("foo")
{:ok, "foo"}
iex> Constructor.Validate.is_string(12)
{:error, "must be a string"}
is_string_list(value)
Checks that value is a list and that each value is a string.
Examples
iex> Constructor.Validate.is_string_list(["foo", 12, "bar"])
{:error, "must be a list of strings"}
iex> Constructor.Validate.is_string_list(["foo", "bar", "baz"])
{:ok, ["foo", "bar", "baz"]}
is_string_list(arg1, value)
is_string_or_nil(value)
The same as is_string/1, except that it will return {:ok, nil} if the value is nil.
Examples
iex> Constructor.Validate.is_string_or_nil(nil)
{:ok, nil}
is_uuid(value)
Checks that value is a uuid.
Examples
iex> Constructor.Validate.is_uuid("8cd1939d-89ca-4927-9166-a221312a5712")
{:ok, "8cd1939d-89ca-4927-9166-a221312a5712"}
iex> Constructor.Validate.is_uuid("209klmas09k;")
{:error, "must be a UUID"}