Valdi (Valdi v0.3.0) View Source

Some helpers function to do validate data

  • Validate type
  • validate inclusion/exclusion
  • validate length for string and enumerable types
  • validate number
  • validate string format/pattern
  • validate custom function
  • validate required (not nil) or not

Each of these validations can be used separatedly

iex> Valdi.validate_type(10, :integer)
:ok
iex> Valdi.validate_type(10, :string)
{:error, "is not a string"}
iex> Valdi.validate_number(9, [min: 10, max: 20])
{:error, "must be greater than or equal to 10"}

Or you can combine multiple condition at one

iex> Valdi.validate(10, type: :integer, number: [min: 10, max: 20])
:ok
iex> Valdi.validate("email@g.c", type: :string, format: ~r/.+@.+.[a-z]{2,10}/)
{:error, "does not match format"}

Link to this section Summary

Functions

Validate value against list of validations.

Apply validation for each array item

Validate embed types

Check if value is not included in the given enumerable. Similar to validate_inclusion/2

Checks whether a string match the given regex.

Check if value is included in the given enumerable.

Check if length of value match given conditions. Length condions are the same with validate_number/2

Validate list value aganst validator and return error if any item is not valid. In case of error {:error, errors}, errors is list of error detail for all error item includes [index, message]

Validate map value with given map specification. Validation spec is a map

Validate number value

Validate value if value is not nil. This function can receive a function to dynamicall calculate required or not.

Validate data types.

Link to this section Types

Specs

error() :: {:error, String.t()}
Link to this type

support_length_types()

View Source

Specs

support_length_types() :: String.t() | map() | list() | tuple()

Link to this section Functions

Link to this function

validate(value, validators)

View Source

Specs

validate(
  any(),
  keyword()
) :: :ok | error()

Validate value against list of validations.

iex> Valdi.validate("email@g.c", type: :string, format: ~r/.+@.+.[a-z]{2,10}/)
{:error, "does not match format"}

All supported validations:

  • type: validate datatype
  • format: check if binary value matched given regex
  • number: validate number value
  • length: validate length of supported types. See validate_length/2 for more details.
  • in: validate inclusion
  • not_in: validate exclusion
  • func: custom validation function follows spec func(any()):: :ok | {:error, message::String.t()}
  • each: validate each item in list with given validator. Supports all above validator
Link to this function

validate_each_item(list, validations)

View Source

Apply validation for each array item

Link to this function

validate_embed(value, embed_type)

View Source

Validate embed types

Link to this function

validate_exclusion(value, enum)

View Source

Check if value is not included in the given enumerable. Similar to validate_inclusion/2

Link to this function

validate_format(value, check)

View Source

Specs

validate_format(String.t(), Regex.t()) :: :ok | error()

Checks whether a string match the given regex.

iex> Valdi.validate_format("year: 2001", ~r/year:\s\d{4}/)
:ok
iex> Valdi.validate_format("hello", ~r/+/)
{:error, "does not match format"}
Link to this function

validate_inclusion(value, enum)

View Source

Check if value is included in the given enumerable.

iex> Valdi.validate_inclusion(1, [1, 2])
:ok
iex> Valdi.validate_inclusion(1, {1, 2})
{:error, "given condition does not implement protocol Enumerable"}
iex> Valdi.validate_inclusion(1, %{a: 1, b: 2})
{:error, "not be in the inclusion list"}
iex> Valdi.validate_inclusion({:a, 1}, %{a: 1, b: 2})
:ok
Link to this function

validate_length(value, checks)

View Source

Specs

validate_length(
  support_length_types(),
  keyword()
) :: :ok | error()

Check if length of value match given conditions. Length condions are the same with validate_number/2

iex> Valdi.validate_length([1], min: 2)
{:error, "length must be greater than or equal to 2"}
iex> Valdi.validate_length("hello", equal_to: 5)
:ok

Supported types

  • list
  • map
  • tuple
  • keyword
  • string
Link to this function

validate_list(items, validators)

View Source

Specs

validate_list(
  list(),
  keyword()
) :: :ok | {:error, list()}

Validate list value aganst validator and return error if any item is not valid. In case of error {:error, errors}, errors is list of error detail for all error item includes [index, message]

iex> Valdi.validate_list([1,2,3], type: :integer, number: [min: 2])
{:error, [[0, "must be greater than or equal to 2"]]}
Link to this function

validate_map(data, validations_spec)

View Source

Specs

validate_map(map(), map()) :: :ok | {:error, map()}

Validate map value with given map specification. Validation spec is a map

validate_map use the key from validation to extract value from input data map and then validate value against the validators for that key.

In case of error, the error detail is a map of error for each key.

iex> validation_spec = %{
...>  email: [type: :string, required: true],
...>  password: [type: :string, length: [min: 8]],
...>  age: [type: :integer, number: [min: 16, max: 60]]
...>  }
iex> Valdi.validate_map(%{name: "dzung", password: "123456", email: "ddd@example.com", age: 28}, validation_spec)
{:error, %{password: "length must be greater than or equal to 8"}}
Link to this function

validate_number(value, checks)

View Source

Specs

validate_number(
  integer() | float(),
  keyword()
) :: :ok | error()

Validate number value

iex> Valdi.validate_number(12, min: 10, max: 12)
:ok
iex> Valdi.validate_number(12, min: 15)
{:error, "must be greater than or equal to 15"}

Support conditions

  • equal_to

  • greater_than_or_equal_to | min

  • greater_than

  • less_than

  • less_than_or_equal_to | max

    validate_number(x, [min: 10, max: 20])

Link to this function

validate_required(value, func)

View Source

Validate value if value is not nil. This function can receive a function to dynamicall calculate required or not.

iex> Valdi.validate_required(nil, true)
{:error, "is required"}
iex> Valdi.validate_required(1, true)
:ok
iex> Valdi.validate_required(nil, false)
:ok
iex> Valdi.validate_required(nil, fn -> 2 == 2 end)
{:error, "is required"}
Link to this function

validate_type(value, struct_name)

View Source

Validate data types.

iex> Valdi.validate_type("a string", :string)
:ok
iex> Valdi.validate_type("a string", :number)
{:error, "is not a number"}

Support built-in types:

  • boolean
  • integer
  • float
  • number (integer or float)
  • string | binary
  • tuple
  • map
  • array
  • atom
  • function
  • keyword

It can also check extend types

  • struct Ex: User
  • {:array, type} : array of type