bluzky

bluzky

Hi, I have just published a new package named tarams. It provide a simple way to define schema, parse and validate your request parameters.

Here is how to use Tarams

@index_params_schema  %{
    keyword: :string,
    status: [type: string, required: true],
    group_id: [type: :integer, validate: {:number, [greater_than: 0]}]
  }

def index(conn, params) do
    with {:ok, better_params} <- Tarams.parse(@index_params_schema, params) do
        IO.inspect(better_params.keyword}
        IO.inspect(better_params.status}
        IO.inspect(better_params.group_id}
        # do anything with your params
    else
        {:error, changset} -> # return params error
    end
end

Your feedback are welcome!

Showing Posts 1 to 6

ChrisYammine

ChrisYammine

So this is just wrapping Ecto changesets?

LostKobrakai

LostKobrakai

This looks quite similar to what nimble_options does.

bluzky

bluzky OP

Yes. It reduces the amount of code that I have to write for each param schema

bluzky

bluzky OP

It is quite similar, but Tarams wraps around Ecto.Changeset so you can use all validator that Ecto.Changeset supports.

bluzky

bluzky OP

In version 0.2.0, I added cast_func option which allow to define custom casting function for custom type.
In the previous version, tarams only supports built-in types and uses Ecto.Changeset.cast function to cast params value.

def my_array_parser(value) do
    if is_binary(value) do
        ids = 
            String.split(value, ",")
            |> Enum.map(&String.to_integer(&1))
        
        {:ok, ids}
    else
        {:error, "Invalid string"}
    end
end

schema = %{
    user_id: [type: {:array, :integer}, cast_func: &my_array_parser/1]
}

Tarams.parse(schema, %{user_id: "1,2,3"})
bluzky

bluzky OP

Release v1.0.0

Github repo

I try to support nested schema but using Ecto, it’s hard to support nested schema without some hack.
So I decide to rewrite Tarams and remove dependency on Ecto.

Now you can work with nested params easily like this:

@user_schema %{
  name: :string,
  email: [type: :string, required: true],
  address: %{
    street: :string,
    district: :string,
    city: :string
  }
}

Tarams.cast(params, @user_schema)
— All posts loaded —

Where Next? Top

Trending in Announcing Top

type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews