All Projects → bluzky → tarams

bluzky / tarams

Licence: MIT License
Casting and validating external data and request parameters in Elixir and Phoenix

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to tarams

talos
Elixir parameter validation library. Simple and extensible
Stars: ✭ 23 (-42.5%)
Mutual labels:  params-validate, params-validator
easy podcasts
Agregador de podcasts y convertidor de audio para cubanos
Stars: ✭ 28 (-30%)
Mutual labels:  phoenix
phoenix-elm-template
A starter repo with Phoenix, Elm 0.19, and Parcel
Stars: ✭ 16 (-60%)
Mutual labels:  phoenix
store
The elementary OS merch store website
Stars: ✭ 20 (-50%)
Mutual labels:  phoenix
gleam compile
Tiny hex package to make the development experience of using gleam in elixir (and especially phoenix projects) better.
Stars: ✭ 29 (-27.5%)
Mutual labels:  phoenix
indicado
Technical indicator library for Elixir with no dependencies.
Stars: ✭ 15 (-62.5%)
Mutual labels:  phoenix
bird watch
Blog post demo application
Stars: ✭ 15 (-62.5%)
Mutual labels:  phoenix
np-flink
flink详细学习实践
Stars: ✭ 26 (-35%)
Mutual labels:  phoenix
mishka-cms
MishkaCms an open source and real time API base CMS Powered by Elixir and Phoenix
Stars: ✭ 37 (-7.5%)
Mutual labels:  phoenix
reph
Phoenix/React application starter kit
Stars: ✭ 15 (-62.5%)
Mutual labels:  phoenix
pretty print formatter
Pretty Print Formatter for Elixir Logger module -- Colorize Ecto's SQL ouput 🖌️
Stars: ✭ 22 (-45%)
Mutual labels:  phoenix
elixir jobs
A job board to publish and find Elixir offers.
Stars: ✭ 83 (+107.5%)
Mutual labels:  phoenix
mindwendel
Create a challenge. Ready? Brainstorm. mindwendel helps you to easily brainstorm and upvote ideas and thoughts within your team.
Stars: ✭ 22 (-45%)
Mutual labels:  phoenix
reason-phoenix
ReasonML/ReScript bindings for the Phoenix javascript library
Stars: ✭ 14 (-65%)
Mutual labels:  phoenix
hadoop-data-ingestion-tool
OLAP and ETL of Big Data
Stars: ✭ 17 (-57.5%)
Mutual labels:  phoenix
ticker-phoenix
Elixir Phoenix Stock Quotes API (IEX Trading)
Stars: ✭ 15 (-62.5%)
Mutual labels:  phoenix
.oOo.
dot files configuration (macOS & Linux), surfingkeys / tmux / screen / ideavimrc / phoenix / etc.
Stars: ✭ 51 (+27.5%)
Mutual labels:  phoenix
dart sass
An installer for sass
Stars: ✭ 54 (+35%)
Mutual labels:  phoenix
pryin
PryIn is an Application Performance Monitoring platform for your Elixir/Phoenix application.
Stars: ✭ 25 (-37.5%)
Mutual labels:  phoenix
phoenix oauth2 provider
Get an OAuth 2 provider running in your phoenix with controllers, views and models in just two minutes
Stars: ✭ 72 (+80%)
Mutual labels:  phoenix

Tarams

Phoenix request params validation library.

Build Status Coverage Status Hex Version docs

Warning: Tarams v1.0.0 APIs is not back compatible

Why Tarams

- Reduce code boilerplate 
- Shorter schema definition
- Default function which generate value each casting time
- Custom validation functions
- Custom parse functions

Installation

Available in Hex, the package can be installed by adding tarams to your list of dependencies in mix.exs:

def deps do
  [
    {:tarams, "~> 1.0.0"}
  ]
end

Usage

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

def index(conn, params) do
    with {:ok, better_params} <- Tarams.cast(params, @index_params_schema) do
        # do anything with your params
    else
        {:error, errors} -> # return params error
    end
end

Define schema

Schema is just a map and it can be nested. Each field is defined as

<field_name>: [<field_spec>, ...]

Or short form

<field_name>: <type>

Field specs is a keyword list thay may include:

  • type is required, Tarams support same data type as Ecto. I borrowed code from Ecto
  • default: default value or default function
  • cast_func: custom cast function
  • number, format, length, in, not_in, func, required are available validations
  • as: alias key you will receive from Tarams.cast if casting is succeeded

Default value

You can define a default value for a field if it's missing from the params.

schema = %{
    status: [type: :string, default: "pending"]
}

Or you can define a default value as a function. This function is evaluated when Tarams.cast gets invoked.

schema = %{
    date: [type: :utc_datetime, default: &Timex.now/0]
}

Custom cast function

You can define your own casting function, tarams provide cast_func option. Your cast_func must follows this spec

fn(any) :: {:ok, any} | {:error, binary} | :error
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.cast(%{user_id: "1,2,3"}, schema)

This is a demo parser function.

Nested schema

With Tarams you can parse and validate nested map and list easily

@my_schema %{
    status: :string,
    pagination: %{
        page: [type: :integer, number: [min: 1]],
        size: [type: :integer, number: [min: 10, max: 100"]]
    }
}

Or nested list schema

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

Validation

Tarams uses Valdi validation library. You can read more about Valdi here Basically it supports following validation

  • validate inclusion/exclusion

  • validate length for string and enumerable types

  • validate number

  • validate string format/pattern

  • validate custom function

  • validate required(not nil) or not

    product_schema = %{
      sku: [type: :string, required: true, length: [min: 6, max: 20]]
      name: [type: :string, required: true],
      quantity: [type: :integer, number: [min: 0]],
      type: [type: :string, in: ~w(physical digital)],
      expiration_date: [type: :naive_datetime, func: &my_validation_func/1]
    }

Contributors

If you find a bug or want to improve something, please send a pull request. Thank you!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].