Giter Club home page Giter Club logo

ex_validator's Introduction

ExValidator

Hex.pm Hex.pm

Helpers for validating and normalizing Elixir data structures.

The validation works as simple composable functions, allowing simple validations...

iex> validator = integer(max: 2)
iex> validator.(2)
{:ok, 2}
iex> validator.(4)
{:error, "is greater than 2"}

...data casting...

iex> validator = map_of(%{name: string(required: true), age: integer(min: 1)})
iex> validator.(%{"name" => "Jhon", "age": "26"})
{:ok, %{name: "Jhon", age: 26}}
iex> validator.(%{"age": "a"})
{:error, %{name: "is blank", age: "is not a number"}}

...and more complex use cases:

iex> address = map_of(%{
...>   city: string(required: true),
...>   state: string(required: true, min: 2, max: 2)
...> })
iex> person = map_of(%{
...>   name: string(required: true),
...>   age: integer(min: 1),
...>   addresses: list_of(address)
...> })
iex> validator = list_of(person)
iex> data = [
...>   %{
...>     "name" => "Jhon",
...>     "age" => "aa",
...>     "addresses" => [
...>       %{"city" => "New York", "state" => "NY"},
...>       %{"city" => "Los Angeles", "state" => "LA"},
...>     ]
...>   },
...>   %{
...>     "name" => "Alex",
...>     "addresses" => [
...>       %{"city" => "Chicago", "states" => "IL"},
...>       %{"city" => "San Francisco", "state" => "CA"},
...>     ]
...>   }
...> ]
iex> validator.(data)
{:error, %{
  0 => %{age: "is not a number"},
  1 => %{addresses: %{0 => %{state: "is blank"}}}
}}

Installation

The package can be installed by adding ex_validator to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_validator, "~> 0.1.0"}
  ]
end

Documentation

Online documentation is available here.

Licence

The ExValidator source code is lecensed under the MIT License

ex_validator's People

Contributors

costaraphael avatar

Watchers

Bruno Dusausoy avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.