Skip to content
Data validation library for Erlang
Pull request Compare This branch is even with spawngrid:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


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 })
>  validaterl:validate(1, #range{ to = 0 })

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

>  validaterl:validate(1, 1).
>  validaterl:validate(1, 2).
>  validaterl:validate(1, 0).

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:

 {'', Username, #length{ is = #range{ from = 3, to = 16 } }},
 {'', 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:

             #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},
Something went wrong with that request. Please try again.