View Source Dsv.Length (Dsv v0.2.1)
Dsv.Length module provides a functions to check if the length of an input value meets specified criteria.
Value can be of the type: String
, List
, Map
, Tuple
, Integer
, Float
. To use values of different type this type need to implement DataLength
protocol.
Summary
Functions
The valid?/2
function checks if the length of a given input value meets the specified criteria.
Parameters
value
- The input value to be checked for length.rules
- A list of validation rules. Each rule is represented as a keyword list containing::min
- The minimum allowed length.:max
- The maximum allowed length.:range
- A range of acceptable lengths.
Returns
A boolean value:
true
if the length ofvalue
meets all of the specified criteria.false
if the length ofvalue
does not meet any of the specified criteria.
Examples
:min
example:
iex> Dsv.Length.valid?("abcd", min: 3)
:true
iex> Dsv.Length.valid?("abcd", min: 4)
:true
iex> Dsv.Length.valid?("abcd", min: 5)
:false
:max
example:
iex> Dsv.Length.valid?("ab", max: 3)
:true
iex> Dsv.Length.valid?("abcd", max: 4)
:true
iex> Dsv.Length.valid?("abcdef", max: 5)
:false
:min & :max
example:
iex> Dsv.Length.valid?("abcd", min: 3, max: 5)
:true
iex> Dsv.Length.valid?("abcd", min: 4, max: 5)
:true
iex> Dsv.Length.valid?("abcd", min: 2, max: 4)
:true
iex> Dsv.Length.valid?("a", min: 2, max: 4)
:false
iex> Dsv.Length.valid?("abcde", min: 2, max: 4)
:false
:range
example:
iex> Dsv.Length.valid?("abcd", range: {3, 5})
:true
iex> Dsv.Length.valid?("abcd", range: {4, 5})
:true
iex> Dsv.Length.valid?("abcd", range: {2, 4})
:true
iex> Dsv.Length.valid?("a", range: {2, 4})
:false
iex> Dsv.Length.valid?("abcde", range: {2, 4})
:false
iex> Dsv.Length.valid?("abcde", range: {4, 2})
** (RuntimeError) Min length (4) can't be greater that max length (2).
The valid?/2
function checks if the length of a given input value meets the specified criteria.
Parameters
value
- The input value to be checked for length.rules
- A list of validation rules. Each rule is represented as a keyword list containing::min
- The minimum allowed length.:max
- The maximum allowed length.:range
- A range of acceptable lengths.
message
- An optional options to provide custom message that will be returned on error.
Returns
:ok
if the length ofvalue
meets all of the specified criteria.{:error, message}
if the length ofvalue
does not meet any of the specified criteria.
Examples
:min
example:
iex> Dsv.Length.validate("abcd", min: 3)
:ok
iex> Dsv.Length.validate("abcd", min: 4)
:ok
iex> Dsv.Length.validate("abcd", min: 5)
{:error, ~s(Value "abcd" is to short. Minimum lenght is 5)}
iex> Dsv.Length.validate("abcd", min: 5, message: "This value should not be shorter than 5.")
{:error, "This value should not be shorter than 5."}
:max
example:
iex> Dsv.Length.validate("ab", max: 3)
:ok
iex> Dsv.Length.validate("abcd", max: 4)
:ok
iex> Dsv.Length.validate("abcdef", max: 5)
{:error, ~s(Value "abcdef" is to long. Maximum length is 5)}
iex> Dsv.Length.validate("abcdef", max: 5, message: "This value should not be longer that 5.")
{:error, "This value should not be longer that 5."}
:min & :max
example:
iex> Dsv.Length.validate("abcd", min: 3, max: 5)
:ok
iex> Dsv.Length.validate("abcd", min: 4, max: 5)
:ok
iex> Dsv.Length.validate("abcd", min: 2, max: 4)
:ok
iex> Dsv.Length.validate("a", min: 2, max: 4)
{:error, ~s(Value "a" has wrong length. Minimum lenght is 2, maximum length is 4)}
iex> Dsv.Length.validate("abcde", min: 2, max: 4)
{:error, ~s(Value "abcde" has wrong length. Minimum lenght is 2, maximum length is 4)}
iex> Dsv.Length.validate("a", min: 2, max: 4, message: "This value must have length between 2 and 4.")
{:error, "This value must have length between 2 and 4."}
iex> Dsv.Length.validate("abcde", min: 2, max: 4, message: "This value must have length between 2 and 4.")
{:error, "This value must have length between 2 and 4."}
:range
example:
iex> Dsv.Length.validate("abcd", range: {3, 5})
:ok
iex> Dsv.Length.validate("abcd", range: {4, 5})
:ok
iex> Dsv.Length.validate("abcd", range: {2, 4})
:ok
iex> Dsv.Length.validate("a", range: {2, 4})
{:error, ~s(Value "a" has wrong length. Minimum lenght is 2, maximum length is 4)}
iex> Dsv.Length.validate("abcde", range: {2, 4})
{:error, ~s(Value "abcde" has wrong length. Minimum lenght is 2, maximum length is 4)}
iex> Dsv.Length.validate("a", range: {2, 4}, message: "This value must have length between 2 and 4.")
{:error, "This value must have length between 2 and 4."}
iex> Dsv.Length.validate("abcde", range: {2, 4}, message: "This value must have length between 2 and 4.")
{:error, "This value must have length between 2 and 4."}
iex> Dsv.Length.validate("abcde", range: {4, 2})
** (RuntimeError) Min length (4) can't be greater that max length (2).