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

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!

Last Post!

bluzky

bluzky

Thank you. Love to hear that. :smiling_face_with_three_hearts:

Where Next?

Popular in Announcing Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10809 141
New
josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement