Skip to content

Commit

Permalink
chore: add validation example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnagydavid committed Jun 25, 2023
1 parent d4ea777 commit f82c882
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 32 deletions.
51 changes: 19 additions & 32 deletions lib/modules/create_invoice/invoice_data.ex
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
defmodule ExSzamlazzHu.Modules.CreateInvoice.InvoiceData do
alias ExSzamlazzHu.Modules.CreateInvoice.Customer
alias ExSzamlazzHu.Modules.CreateInvoice.Item
alias ExSzamlazzHu.Modules.CreateInvoice.Settings
alias ExSzamlazzHu.Utils.Validator

@derive {Inspect, except: [:agent_key]}

@type t :: %__MODULE__{
agent_key: String.t(),
completion_date: String.t(),
payment_deadline: String.t(),
method_of_payment: String.t(),
order_id: String.t(),
currency: String.t(),
language: String.t(),
comment: String.t(),
is_proforma: String.t(),
is_paid: String.t(),
customer: Customer.t(),
items: list(Item.t())
}
@type t :: %__MODULE__{}

@enforce_keys [:settings]
defstruct [
:agent_key,
:completion_date,
:payment_deadline,
:method_of_payment,
:order_id,
:currency,
:language,
:comment,
:is_proforma,
:is_paid,
:customer,
:settings,
:header,
:seller,
:buyer,
:waybill,
:items
]

def parse(params) do
struct(%__MODULE__{}, Map.drop(params, [:customer, :items]))
|> Map.put(:customer, Customer.parse(params[:customer]))
|> Map.put(:items, Enum.map(params[:items], &Item.parse/1))
%__MODULE__{
settings: params[:settings]
}
end

def validate(struct) do
%{
settings: &Settings.validate/1
}
|> Validator.validate(struct)
end
end
55 changes: 55 additions & 0 deletions lib/modules/create_invoice/settings.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule ExSzamlazzHu.Modules.CreateInvoice.Settings do
alias ExSzamlazzHu.Utils.Validator

@type t :: %__MODULE__{}

@enforce_keys [:e_invoice, :download_invoice]
defstruct [
:user,
:password,
:agent_key,
:e_invoice,
:download_invoice,
:download_invoice_number_of_copies,
:response_version,
:aggregator,
:guardian,
:article_identifier_invoice,
:external_invoice_identifier
]

@spec parse(map()) :: t()
def parse(params) do
%__MODULE__{
user: params[:user],
password: params[:password],
agent_key: params[:agent_key],
e_invoice: params[:e_invoice],
download_invoice: params[:download_invoice],
download_invoice_number_of_copies: params[:download_invoice_number_of_copies],
response_version: params[:response_version],
aggregator: params[:aggregator],
guardian: params[:guardian],
article_identifier_invoice: params[:article_identifier_invoice],
external_invoice_identifier: params[:external_invoice_identifier]
}
end

@spec validate(t()) :: boolean()
def validate(struct) do
%{
user: &(is_nil(&1) or is_binary(&1)),
password: &(is_nil(&1) or is_binary(&1)),
agent_key: &(is_nil(&1) or is_binary(&1)),
e_invoice: &is_boolean/1,
download_invoice: &is_boolean/1,
download_invoice_number_of_copies: &(is_nil(&1) or is_integer(&1)),
response_version: &(is_nil(&1) or is_integer(&1)),
aggregator: &(is_nil(&1) or is_binary(&1)),
guardian: &(is_nil(&1) or is_boolean(&1)),
article_identifier_invoice: &(is_nil(&1) or is_boolean(&1)),
external_invoice_identifier: &(is_nil(&1) or is_binary(&1))
}
|> Validator.validate(struct)
end
end
19 changes: 19 additions & 0 deletions lib/utils/validator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule ExSzamlazzHu.Utils.Validator do
def validate(rules, struct) do
Enum.reduce(rules, {true, %{}}, fn {key, validator}, {is_valid, errors} ->
case validator.(Map.get(struct, key)) do
true -> {is_valid, errors}
false -> {false, Map.put(errors, key, :invalid)}
:ok -> {is_valid, errors}
{:error, incoming_errors} -> {false, Map.put(errors, key, incoming_errors)}
end
end)
|> then(fn {is_valid, errors} ->
if is_valid do
:ok
else
{:error, errors}
end
end)
end
end

0 comments on commit f82c882

Please sign in to comment.