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()}
Specs
Link to this section Functions
Specs
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 datatypeformat
: check if binary value matched given regexnumber
: validate number valuelength
: validate length of supported types. Seevalidate_length/2
for more details.in
: validate inclusionnot_in
: validate exclusionfunc
: custom validation function follows specfunc(any()):: :ok | {:error, message::String.t()}
Validate embed types
Check if value is not included in the given enumerable. Similar to validate_inclusion/2
Specs
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"}
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
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
Specs
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])
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