Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 README.md
Octocat-spinner-32 mix.exs
Octocat-spinner-32 package.exs
Octocat-spinner-32 package.head.exs
README.md

Validatex

Build Status

Validatex is a simple Elixir library for validating (input) data

Validation Primitives

Using records defined in Validatex one can check individual values against validators.

For example:

iex> alias Validatex, as: V
[]
iex> V.Validate.valid?(V.Range.new(to: 10), 1)
true
iex> V.Validate.valid?(V.Range.new(to: 0), 1) 
:greater

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

iex> V.Validate.valid?(1,1)                  
true
iex> V.Validate.valid?(2,1)
:lesser
iex> V.Validate.valid?(0, 1)
:greater

Custom Validators

One can define custom validators using by implementing Validatex.Validate protocol

  defrecord MyValidator, q: nil

  defimpl Validatex.Validate, for: MyValidator do
     alias MyValidator, as: V
     def valid?(V[], v), do: v
  end

Validation Sheets

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

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

{name, value, spec}

For example:

[
 {"user.name", username, V.Length.new(is: V.Range.new(from: 3, to: 16))},
 {"user.email", email, V.Length.new(is: V.Range.new(from: 3, to: 255))},
 {"user.age", age, V.Numericality.new(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:

iex> V.validate(plan)
[{"user.age","wrong",Validatex.Numericality[default: 0],:number_expected}]
Something went wrong with that request. Please try again.