Argx Hex Versiondocs

DSLs for checking args.

Installation

Add argx to your list of dependencies in mix.exs:

defp deps do
[{:argx, ">= 0.0.0"}]
end

Install via mix deps.get and the happy check your args as described in Usage and Advanced.

Usage

Quick Start

defmodule YourProject do
# step 1: introduce check function by Argx module
use Argx
# step 2: define rule
defconfig(Rule, id(:string))
def get(args) do
# step 3: use check function to check args
check(args, [Rule])
|> case do
{:error, _} -> :error
_ -> :ok
end
end
end

Check Via DSL

# step 1: define your validator
defmodule YourProject.Argx do
use Argx.WithCheck
end
defmodule YourProject do
# step 2: import your validator
import YourProject.Argx
# step 3: use with_check macro to wrap your function
with_check configs(id(:string)) do
def get(id) do
{id}
end
end
end

Advanced

1. How to share arg configs?

step 1: create a module for define shared arg configs.

defmodule YourProject.ArgConfigs do
use Argx.Defconfig
defconfig(NumberRule, number(:string, :empty))
defconfig(PageSizeRule, page_size(:integer, :auto, 1..100) || 10)
end

step 2 : config share module to the following positions.

use Argx, share: YourProject.ArgConfigs
# or
use Argx.WithCheck, share: YourProject.ArgConfigs

step 3 : use arg config by name.

def get(args) do
check(args, [NumberRule, PageSizeRule])
|> case do
{:error, _} -> :error
_ -> :ok
end
end
# or
with_check configs(NumberRule, PageSizeRule) do
def get(id) do
{id}
end
end

2. Format errors

just implement callback fmt_errors/1, Argx invoke your custom format errors function, when check done.

There are 3 places to put it.

Highest priority: in the current module.

defmodule YourProject do
use Argx
def fmt_errors({:error, _errors}), do: :error
def fmt_errors(_new_args_or_result), do: :ok
...
end
# or
defmodule YourProject do
import YourProject.Argx
def fmt_errors({:error, _errors}), do: :error
def fmt_errors(_new_args_or_result), do: :ok
...
end

Second priority: in the share arg configs module.

defmodule YourProject.ArgConfigs do
use Argx.Defconfig
def fmt_errors({:error, _errors}), do: :error
def fmt_errors(_new_args_or_result), do: :ok
...
end

Lowest priority: if you use argx via with_check, also implement it in the definition module.

defmodule YourProject.Argx do
use Argx.WithCheck
def fmt_errors({:error, _errors}), do: :error
def fmt_errors(_new_args_or_result), do: :ok
...
end

Features

Support Data Type

check/2 function

DSL

defconfig

Reuse arg config rule by name.

with_check

Errors

There are 3 types.

  1. lacked some fields.
  2. some fields' type is error.
  3. some field's range/length/size is out of range.

Example:

{
:error,
[
error_type: ["cargoes:1:number", "cargoes:2:name"], # report nested data's error
lacked: [:mobile],
out_of_range: [:weight]
]
}

If you want to convert meta errors to readable message, just implement fmt_errors/1.

Configuration

config Argx or Argx.WithCheck module.

  1. set shared arg configs module.
  2. set warn flag.
use Argx.WithCheck, share: YourProject.ArgConfigs, warn: false

Benchee Report

Nameipsaveragedeviationmedian99th %Recommand
without Argx3090.90 K0.32 μs±13466.34%0 μs0.90 μs-
with_check DSL55.57 K17.99 μs±124.26%15.90 μs56.90 μsYES
check22.64 K44.18 μs±94.15%36.90 μs153.90 μsNO

Contributing

Contributions to Argx are very welcome!

Bug reports, documentation, spelling corrections... all of those (and probably more) are much appreciated contributions!