View Source Veli (Veli v0.2.0)

Veli is a simple validation library for elixir.

rules

Rules

simple-rules

Simple Rules

When you validate simple types (like a string or an integer), you must use simple rules. Which is a keyword list.

rule = [type: :string, run: fn value -> String.reverse(value) === value end]
Veli.valid("wow", rule) |> Veli.error() === nil

list-rules

List Rules

When you need to validate every item on a list, you must use Veli.Types.List struct so validator can know if it is validating a string or a value.

rule = %Veli.Types.List{rule: [type: :integer]}
Veli.valid([4, 2, 7, 1], rule) |> Veli.error() === nil

map-rules

Map Rules

When you need to validate a map (an object), you must use Veli.Types.Map struct so validator can know if it is validating a map or a value.

rule = %Veli.Types.Map{rule: %{
  username: [type: :string],
  age: [type: :string, min: 13]
}}
Veli.valid(%{username: "bob", age: 16}, rule) |> Veli.error() === nil

custom-errors

Custom Errors

By default, Any error returns false. You can specify custom errors with adding underscore (_) prefix.

rule = [type: :integer, _type: "Value must be an integer!"]
Veli.valid(10, rule) |> Veli.error() # nil
Veli.valid("invalid value", rule) |> Veli.error() # "Value must be an integer!"

custom-errors-for-map-or-list

Custom Errors for Map or List

As you can see in Veli.Types.Map or Veli.Types.List, they both have a field named "error" which is nil by default. You can specify custom errors with "error" field.

rule = %Veli.Types.Map{
  rule: %{
    username: [type: :string],
    age: [type: :string, min: 13]
  },
  error: "Not a valid object."
}
Veli.valid(%{username: "bob", age: 16}, rule) |> Veli.error() # nil
Veli.valid(96, rule) |> Veli.error() # "Not a valid object."

more-example

More Example

You can read library tests for more example.

Link to this section Summary

Functions

Returns first error from validate result. Returns nil if everything is valid.

Returns all false validates.

Validate a value with rules. Returns a keyword list which contains results. You should not process that result yourself. Use Veli.errors or Veli.error for processing results instead.

Link to this section Functions

@spec error(keyword()) :: tuple()

Returns first error from validate result. Returns nil if everything is valid.

example

Example

rule = %Veli.Types.List{rules: [type: :float]}
Veli.valid([5, 3.2, "how"], rule) |> Veli.error()
@spec errors(keyword()) :: keyword()

Returns all false validates.

example

Example

rule = %Veli.Types.List{rules: [type: :float]}
Veli.valid([5, 3.2, "how"], rule) |> Veli.errors()
@spec valid(any(), keyword() | Veli.Types.List | Veli.Types.Map) :: keyword()

Validate a value with rules. Returns a keyword list which contains results. You should not process that result yourself. Use Veli.errors or Veli.error for processing results instead.

example

Example

rule = [type: :string, match: ~r/^https?/]
Veli.valid("wow", rule) |> Veli.error() !== nil
Veli.valid("https://hex.pm", rule) |> Veli.error() === nil

More examples can be found in library tests.