bluzky

bluzky

Valdi another datavalidation

I know that Vex is a great and popular data validation library. But learn how to write a library is fun and I can learn lots of things. So I build my own library called Valdi

https://github.com/bluzky/valdi

  • It supports validate value against multiple validation:
iex(12)> Valdi.validate(10, type: :integer, number: [min: 10, max: 20])
:ok
iex(13)> Valdi.validate("email@g.c", type: :string, format: ~r/.+@.+\.[a-z]{2,10}/)
{:error, "format not matched"}
  • You can validate list of value
iex(51)> Valdi.validate_list([1,2,3], type: :integer, number: [min: 2])
{:error, [[0, "must be greater than or equal to 2"]]}
  • And validate map data too
iex(54)>  validation_spec = %{
...(54)>     email: [type: :string],
...(54)>     password: [type: :string, length: [min: 8]],
...(54)>     age: [type: :integer, number: [min: 16, max: 60]]
...(54)>   }
iex(56)> Valdi.validate_map(%{name: "dzung", password: "123456", emal: "ddd@example.com", age: 28}, validation_spec)
{:error, %{password: "length must be greater than or equal to 8"}}

Thank for reading.
Please comment if you have any suggestion.

Most Liked Responses

bluzky

bluzky

Thank you for your comment. I used Ecto.Changeset before and there are some reason why I build another one:

  • Some simple project or library, using Ecto is overkill
  • I need a simple and clean way. Changeset neet much code to do the same thing
    Using Ecto.Changeset
types = %{a: :integer}
data = %{a: "hello"}
default = %{}
changeset = 
    {default, types} 
    |> Ecto.Changeset.cast(data, Map.keys(types)) 
    |> Ecto.Changeset.validate_required([:a]) 
    |> Ecto.Changeset.validate_number(greater_than: 5)

Using Valdi

data_spec = %{a: [type: :integer, required: true, number: [greater_than: 5]}
Valdi.validate(%{a: "hello"}, data_spec)

I have tried wrapping Ecto before but I have to do some hack to support nested and dynamic default value

stefanchrobot

stefanchrobot

My comments would be pretty much the same as here :wink:

Mpanarin

Mpanarin

Love this answer!

Coming from python world, where things like cerberus exist, validating request params with Ecto felt like hunting a fly with AR.

Thanks for this minimalistic library!

Where Next?

Popular in Announcing Top

josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19228 141
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 12840 120
New
blatyo
The best overview for how things are tied together is this presentation. Modules and functions are pretty well documented at this point, ...
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamPipe ...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 14327 100
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

We're in Beta

About us Mission Statement