phcurado
Zoi is a new schema validation library for Elixir.
It’s inspired by Zod from the JavaScript ecosystem, bringing a similar functional API for defining, validating, transforming and coercing data, using elixir pipes for the schema definition.
Basic Example
iex> schema = Zoi.string() |> Zoi.min(3)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, "hi")
{:error, [%Zoi.Error{message: "too small: must have at least 3 characters"}]}
Transformations
iex> schema = Zoi.string() |> Zoi.trim()
iex> Zoi.parse(schema, " world ")
{:ok, "world"}
Coercion
iex> Zoi.string() |> Zoi.parse(123)
{:error, [%Zoi.Error{message: "invalid type: must be a string"}]}
iex> Zoi.string(coerce: true) |> Zoi.parse(123)
{:ok, "123"}
Complex schemas
iex> user_schema =
...> Zoi.object(%{
...> name: Zoi.string() |> Zoi.min(2) |> Zoi.max(100),
...> age: Zoi.integer() |> Zoi.min(18) |> Zoi.max(120),
...> email: Zoi.email()
...> })
iex> Zoi.parse(user_schema, %{name: "Alice", age: 30, email: "alice@email.com"})
{:ok, %{name: "Alice", age: 30, email: "alice@email.com"}}
There are many built in types, validations and transformations. Check out the documentation for all the possibilities.
Docs: Zoi — Zoi v0.18.4
Hex: zoi | Hex
Repo:
https://github.com/phcurado/zoi
I would love to hear your feedback, thoughts and any additional features you’d like to see
Trending in Announcing
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
th3mus1cman
Will have to check this out. I really like Zod so this looks pretty nice. Thanks!!
phcurado
New 0.5 version released
Added
atom type schema
Useful for verifying atom types:
which can be also useful for validating maps with atom keys:
Union and intersection custom errors
Now we can give custom errors to unions (logical OR) and intersections (logical AND)
string boolean type schema
useful when parsing boolean from different sources, good for environment variables or integration with external APIs:
Now
Zoihave all typesZodhave, with some additions to fit in the elixir ecosystemphcurado
The recent release is focused on bug fixes, a new
Zoi.extend/2type and some guides.Zoi.extend/2type:Extends two object type schemas into one.
This can help you when creating composable data structures.
camelCasetosnakeCaseor completely change how the schema needs to be presented in your application.Zoi. Many applications will be fine by just using changesets directly, this is an alternative usingZoiandChangesets.Changesetsor plainStructsandMaps. It can be a tedius job to create the data structure and parsing logic, this guide focus on automatically generating a schema from a JSON response example.Release v0.5.4: zoi | Hex
olivermt
Would you be open to a parse!() PR?
Very convenient when you just want to blow up the world in runtime.exs on configuration errors.
phcurado
Yes that’s a great idea
dimitarvp
And then it’s your turn to open-source such a config library.
phcurado
New release
This release is focused on bringing the
keywordtype andtypespecinference from schema.Keyword type
Defines a keyword list type schema.
Typespec generation
Generates the Elixir type specification for a given schema.
All types in
Zoihave their own type specification, even more complex ones:Which will generate:
New parsing function
As per @olivermt recommendation, we now have the
Zoi.parse!/3function for anyone who wants to raise errors when parsing:Next steps
I believe with the current API, we can also do type inference for the new elixir type system and extend some features on top of the current types.
For example, I was experimenting automatically generating structs with defaults and typespecs using
ZoiAPI:This would create the
Userstruct as follows:I’m not sure if this is a good feature to support but in any case I can add a guide on how to achieve this and/or add it as a feature to the library.
Release v0.5.7: zoi | Hex
olivermt
What would be nice is to enforce either or type config
Essentially a zoi required of either one or another.
My specific usecase is required config for a library I am making that needs either an s3 config or a directory config.
phcurado
could you elaborate what you mean? If you have an example because I didn’t quite understand the problem you have
phcurado
New release
Two new types added to Zoi: struct and literal types
Struct type
Now Zoi supports struct types, to validate struct and it’s fields:
by default it will try to validate the struct and it’s contents but you can also
coercethe input (if it’s a map for example) to be converted to the struct:There are also some helpers for creating structs: struct fields, defaults and the
@enforce_keyshelper functions:Literal type
As the name suggest, this type represents a literal value. The input value should always match the defined literal:
Release v0.6.3: zoi | Hex