litmus v0.5.0 Litmus.Type.String View Source
This type validates and converts values to strings It converts boolean and number values to strings.
Options
:min_length
- Specifies the minimum number of characters allowed in the string. Allowed values are non-negative integers.:max_length
- Specifies the maximum number of characters allowed in the string. Allowed values are non-negative integers.:length
- Specifies the exact number of characters allowed in the string. Allowed values are non-negative integers.:regex
- Specifies a Regular expression that a string must match. Use theLitmus.Type.String.Regex
struct with the options::pattern
- The regex to match:error_message
- An error message to use when the pattern does not match
:required
- Setting:required
totrue
will cause a validation error when a field is not present or the value isnil
. Allowed values for required aretrue
andfalse
. The default isfalse
.:trim
- Removes additional whitespace at the front and end of a string. Allowed values aretrue
andfalse
. The default isfalse
.
Examples
iex> schema = %{
...> "username" => %Litmus.Type.String{
...> min_length: 3,
...> max_length: 10,
...> trim: true
...> },
...> "password" => %Litmus.Type.String{
...> length: 6,
...> regex: %Litmus.Type.String.Regex{
...> pattern: ~r/^[a-zA-Z0-9_]*$/,
...> error_message: "password must be alphanumeric"
...> }
...> }
...> }
iex> params = %{"username" => " user123 ", "password" => "root01"}
iex> Litmus.validate(params, schema)
{:ok, %{"username" => "user123", "password" => "root01"}}
iex> Litmus.validate(%{"password" => "ro!_@1"}, schema)
{:error, "password must be alphanumeric"}
Link to this section Summary
Link to this section Types
t() :: %Litmus.Type.String{ length: non_neg_integer() | nil, max_length: non_neg_integer() | nil, min_length: non_neg_integer() | nil, regex: Litmus.Type.String.Regex.t(), required: boolean(), trim: boolean() }