Current section

Files

Jump to
bingex lib bingex order validators.ex
Raw

lib/bingex/order/validators.ex

defmodule Bingex.Order.Validators do
@moduledoc false
alias Bingex.Order
@sides [:buy, :sell]
@position_sides [:short, :long, :both]
@types [:market, :trigger_market]
@working_types [:index_price, :mark_price, :contract_price]
@spec validate(:order_id, term()) ::
{:ok, order_id :: binary()} | {:error, reason :: binary()}
def validate(:order_id, x) when is_binary(x), do: {:ok, x}
def validate(:order_id, x) do
reason = "expected :order_id to be type of binary, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:symbol, term()) ::
{:ok, symbol :: binary()} | {:error, reason :: binary()}
def validate(:symbol, x) when is_binary(x), do: {:ok, x}
def validate(:symbol, x) do
reason = "expected :symbol to be type of binary, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:side, term()) ::
{:ok, Order.side()} | {:error, reason :: binary()}
def validate(:side, x) when x in @sides, do: {:ok, x}
def validate(:side, x) do
reason =
"expected :side to be one of #{inspect(@sides)}, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:position_side, term()) ::
{:ok, Order.position_side()} | {:error, reason :: binary()}
def validate(:position_side, x) when x in @position_sides, do: {:ok, x}
def validate(:position_side, x) do
reason =
"expected :position_side " <>
"to be one of #{inspect(@position_sides)}, " <>
"got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:type, term()) ::
{:ok, Order.type()} | {:error, reason :: binary()}
def validate(:type, x) when x in @types, do: {:ok, x}
def validate(:type, x) do
reason =
"expected :type to be one of #{inspect(@types)}, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:price, term()) ::
{:ok, price :: float()} | {:error, reason :: binary()}
def validate(:price, x) when is_number(x), do: {:ok, x}
def validate(:price, x) do
reason = "expected :price to be type of number, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:stop_price, term()) ::
{:ok, price :: float()} | {:error, reason :: binary()}
def validate(:stop_price, x) when is_number(x), do: {:ok, x}
def validate(:stop_price, x) do
reason =
"expected :stop_price to be type of number, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:quantity, term()) ::
{:ok, quantity :: float()} | {:error, reason :: binary()}
def validate(:quantity, x) when is_number(x), do: {:ok, x}
def validate(:quantity, x) do
reason =
"expected :quantity to be type of number, got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:working_type, term()) ::
{:ok, Order.working_type()} | {:error, reason :: binary()}
def validate(:working_type, x) when x in @working_types, do: {:ok, x}
def validate(:working_type, x) do
reason =
"expected :working_type " <>
"to be one of #{inspect(@working_types)}, " <>
"got: #{inspect(x)}"
{:error, reason}
end
@spec validate(:client_order_id, term()) ::
{:ok, client_order_id :: binary()} | {:error, reason :: binary()}
def validate(:client_order_id, x) when is_binary(x), do: {:ok, x}
def validate(:client_order_id, x) do
reason =
"expected :client_order_id to be type of binary, " <>
"got: #{inspect(x)}"
{:error, reason}
end
end