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
- 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
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.
Changesetneet 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
3
Popular in Libraries
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure).
1.0-rc.1 is now available. To use it re...
New
Hello there,
I would like to share a feature toggles library (AKA feature flags) I’ve been working on.
The main package is FunWithFlags...
New
I’ve just released stable versions of my Prometheus Elixir libs:
Elixir client [docs];
Ecto collector [docs];
Plugs instrumenter/Export...
New
The latest release of Ace (0.10.0) includes serving content over HTTP/2.
I have started writing a webserver to teach my self more about...
New
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
Hi!
I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
Hi there,
for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
Other popular topics
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New








