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

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
fuelen
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

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
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
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
sorenone
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
akoutmos
@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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews