Magik.Validator (Magik v0.10.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 allow_nil or not

Each of these validations can be used separatedly

iex(2)>   Magik.Validator.validate_type(10, :integer)
:ok
iex(3)>   Magik.Validator.validate_type(10, :string)
{:error, "is not a string"}
iex(3)>   Magik.Validator.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(12)> Magik.Validator.validate(10, type: :integer, number: [min: 10, max: 20])
:ok
iex(13)> Magik.Validator.validate("[email protected]", type: :string, format: ~r/.+@.+.[a-z]{2,10}/)
{:error, "format not matched"}

Link to this section Summary

Functions

Validate value against list of validations.

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 number value

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(13)> Magik.Validator.validate("[email protected]", type: :string, format: ~r/.+@.+.[a-z]{2,10}/)
{:error, "format not matched"}

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()}
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(11)> Magik.Validator.validate_format("year: 2001", ~r/year: {4}/)
:ok
iex(12)> Magik.Validator.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(21)> Magik.Validator.validate_inclusion(1, [1, 2])
:ok
iex(22)> Magik.Validator.validate_inclusion(1, {1, 2})
{:error, "given condition does not implement protocol Enumerable"}
iex(23)> Magik.Validator.validate_inclusion(1, %{a: 1, b: 2})
{:error, "not be in the inclusion list"}
iex(24)> Magik.Validator.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(15)> Magik.Validator.validate_length([1], min: 2)
{:error, "length must be greater than or equal to 2"}
iex(16)> Magik.Validator.validate_length("hello", equal_to: 5)
:ok

Supported types

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

validate_number(value, checks)

View Source

Specs

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

Validate number value

iex(3)> Magik.Validator.validate_number(12, min: 10, max: 12)
:ok
iex(4)> Magik.Validator.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_type(value, struct_name)

View Source

Validate data types.

iex(1)> Magik.Validator.validate_type("a string", :string)
:ok
iex(2)> Magik.Validator.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