View Source Valdi (Valdi v0.5.0)
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
- validate decimal
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"}
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.
Types
@type error() :: {:error, String.t()}
Functions
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 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()}
each
: validate each item in list with given validator. Supports all above validator
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.
iex> Valdi.validate_format("year: 2001", ~r/year:\s\d{4}/)
:ok
iex> Valdi.validate_format("hello", ~r/+/)
{:error, "does not match format"}
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
@spec 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
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"]]}
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"}}
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])
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"}
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
date
datetime
naive_datetime
time
It can also check extend types
struct
Ex:User
{:array, type}
: array of type