Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Data validation library for Erlang
Erlang
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
include
src
.gitignore Initial support for numericality validation
README.md
rebar.test.config Renames rebar.config to rebar.test.config so that validaterl doesn't …

README.md

Validaterl

Validaterl is a simple Erlang library for validating (input) data.

Validation Primitives

Using records defined in validaterl/include/validaterl.hrl one can check individual values against validators.

For example:

>  validaterl:validate(1, #range{ to = 10 })
true
>  validaterl:validate(1, #range{ to = 0 })
greater

Also, if the second argument is not a validator, it will be matched against the first argument:

>  validaterl:validate(1, 1).
true
>  validaterl:validate(1, 2).
lesser
>  validaterl:validate(1, 0).
greater

Custom Validators

One can define custom validators using this layout:

-record(my_validator, {
                        '$validator' = fun mymodule:myvalidator/2,
                        ... %% rest of arguments
                      }).

Validation Sheets

Instead of running individual validations, you can define so called "validation sheets" and test them using validaterl:validate/1.

Validation sheet is a list of validations in the following format:

{Name :: any(), Value :: any(), Spec :: spec()}

For example:

[
 {'user.name', Username, #length{ is = #range{ from = 3, to = 16 } }},
 {'user.email', Email, #length{ is = #range { from = 3, to = 255 } }},
 {'user.age', Age, #numericality{ allow_string = true }}
]

Just as an example, if you try to put a string with a non-numeric value into Age, you'll get this:

[{'user.age',"wrong",
             #numericality{'$validator' = #Fun<validaterl.validate.2>,
                           allow_undefined = false,allow_null = false,
                           allow_string = true,allow_empty = false,allow_rest = false,
                           allow_float = true,default = 0},
             number_expected}]
Something went wrong with that request. Please try again.