View Source Veli (Veli v0.1.0)
Veli (Validation in elixir) is a simple validation library for elixir.
rule
Rule
Rule is a keyword list that contains some rules for validation.
A rule has these options:
type: for validating types / affects some other rules likeminandmax. Supports following types:stringintegernumberboollistmap
min: Minimum value to accept.- for
numberandinteger, it checks the number. - for
string, it checks string length. - for
list, it checks list length. - for
map, it checks key length.
- for
max: Same withminrule but for maximum values.match: For matching a value.- for
string, it uses regex to match. - for any other type, it will just compare both values.
- for
run: Allows you to add custom filtering function inside it that returns a boolean. If it returns false, validation will fail.
Here is an example for rule defination.
username_rule = [type: :string, min: 3, max: 32, match: ~r/^[a-zA-Z0-9_]*$/]And for validating forms:
form_rules = %{
"username" => [type: :string, min: 3, max: 32, match: ~r/^[a-zA-Z0-9_]*$/],
"age" => [type: :integer, min: 13]
}
Link to this section Summary
Functions
An helper function for validate_form/2 that finds first error from form validation result.
Validate a value with rule.
Validate a form with rules.
Validate list elements with a rule
Link to this section Functions
An helper function for validate_form/2 that finds first error from form validation result.
Returns first error from form validation result. nil if success.
example
Example
iex(1)> form = %{"username" => "james", "age" => 10}
%{"age" => 10, "username" => "james"}
iex(2)> rules = %{"username" => [type: :string], "age" => [type: :integer, min: 13]}
%{
"age" => [type: :integer, min: 13],
"username" => [type: :string]
}
iex(3)> Veli.validate_form(form, rules)
[{"age", :min_error}, {"username", :ok}]
iex(4)> Veli.validate_form(form, rules) |> Veli.get_error()
{"age", :min_error}
iex(5)> form = %{form | "age" => 20}
%{"age" => 20, "username" => "james"}
iex(6)> Veli.validate_form(form, rules) |> Veli.get_error()
nil
Validate a value with rule.
Returns an atom.
examples
Examples
Simple usage
iex(1)> rule = [type: :integer, max: 100]
[type: :integer, max: 100]
iex(2)> Veli.validate(96, rule)
:ok
iex(3)> Veli.validate(101, rule)
:max_errorAdding custom filtering functions
iex(1)> rule = [type: :string, run: fn value -> String.reverse(value) === value end]
[type: :string, run: #Function<42.3316493/1 in :erl_eval.expr/6>]
iex(2)> Veli.validate("wow", rule)
:ok
iex(3)> Veli.validate("hello", rule)
:run_error
Validate a form with rules.
Returns list of results which contains a tuple that includes key with result.
example
Example
iex(1)> form = %{"username" => "john", "age" => 17}
%{"age" => 17, "username" => "john"}
iex(2)> rules = %{"username" => [type: :string, min: 3, max: 32, match: ~r/^[a-zA-Z0-9_]*$/], "age" => [type: :integer, min: 13]}
%{
"age" => [type: :integer, min: 13],
"username" => [type: :string, min: 3, max: 32, match: ~r/^[a-zA-Z0-9_]*$/]
}
iex(3)> Veli.validate_form(form, rules)
[{"age", :ok}, {"username", :ok}]
iex(4)> form = %{form | "age" => 10}
%{"age" => 10, "username" => "john"}
iex(5)> Veli.validate_form(form, rules)
[{"age", :min_error}, {"username", :ok}]
Validate list elements with a rule
Returns list of results which contains a tuple that includes index with result.
example
Example
iex(1)> rule = [type: :integer, run: fn value -> rem(value, 2) == 0 end]
[type: :integer, max: 100]
iex(2)> Veli.validate_list([4, 6, 8, 10], rule) |> Veli.get_error()
nil
iex(3)> Veli.validate([4, 3, 8], rule) |> Veli.get_error()
{1, :run_error}