-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new separate api for cast, validate and transform
- Loading branch information
Showing
7 changed files
with
647 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Tarams.Result do | ||
@moduledoc """ | ||
Result Struct for Tarams operations | ||
""" | ||
@enforce_keys [:schema] | ||
defstruct schema: %{}, | ||
valid_data: %{}, | ||
params: %{}, | ||
errors: %{}, | ||
valid?: true | ||
|
||
@doc """ | ||
Create a new Result struct with given schema map and params. | ||
""" | ||
@spec new(%{}) :: %Tarams.Result{} | ||
def new(attrs) do | ||
struct(__MODULE__, attrs) | ||
end | ||
|
||
@doc """ | ||
Put error to result. | ||
""" | ||
@spec put_error(%Tarams.Result{}, field :: atom, error :: String.t()) :: %Tarams.Result{} | ||
def put_error(result, field, error) do | ||
errors = | ||
case get_error(result, field) do | ||
nil -> error | ||
errors -> [error | errors] | ||
end | ||
|
||
%Tarams.Result{result | errors: Map.put(result.errors, field, errors), valid?: false} | ||
end | ||
|
||
@doc """ | ||
Put valid data to result. | ||
""" | ||
@spec put_data(%Tarams.Result{}, field :: atom, value :: any) :: %Tarams.Result{} | ||
def put_data(result, field, value) do | ||
%Tarams.Result{result | valid_data: Map.put(result.valid_data, field, value)} | ||
end | ||
|
||
@doc """ | ||
Get error from result for given field. | ||
""" | ||
@spec get_error(%Tarams.Result{}, field :: atom) :: String.t() | nil | ||
def get_error(result, field) do | ||
Map.get(result.errors, field) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.