Skip to content

Commit

Permalink
updated select to working state.
Browse files Browse the repository at this point in the history
  • Loading branch information
mogorman committed Feb 23, 2022
1 parent 9a2d862 commit 0238317
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
49 changes: 40 additions & 9 deletions lib/chore_runner/chore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ defmodule ChoreRunner.Chore do
expected_inputs = mod.inputs

Enum.reduce(input, {%{}, []}, fn {key, val}, {validated_inputs, errors_acc} ->
with {:ok, {type, name, opts}} <- verify_valid_input_name(expected_inputs, key),
{:ok, validated_value} <- validate_input(name, val, type, opts) do
with {:ok, input} <- verify_valid_input_name(expected_inputs, key),
name <- input |> Tuple.to_list() |> Enum.at(1),
{:ok, validated_value} <- do_validate_input(val, input) do
{Map.put(validated_inputs, name, validated_value), errors_acc}
else
{:error, :invalid_input_name} ->
Expand All @@ -109,20 +110,28 @@ defmodule ChoreRunner.Chore do
end

defp verify_valid_input_name(expected_inputs, key) do
Enum.find_value(expected_inputs, fn {type, name, opts} ->
if name == key or "#{name}" == key do
{:ok, {type, name, opts}}
else
false
end
Enum.find_value(expected_inputs, fn
{type, name, args, opts} ->
if name == key or "#{name}" == key do
{:ok, {type, name, args, opts}}
else
false
end

{type, name, opts} ->
if name == key or "#{name}" == key do
{:ok, {type, name, opts}}
else
false
end
end)
|> case do
nil -> {:error, :invalid_input_name}
{:ok, res} -> {:ok, res}
end
end

defp validate_input(name, value, type, opts) do
defp do_validate_input(value, {type, name, opts}) do
[(&Input.validate_field(type, &1)) | Keyword.get(opts, :validators, [])]
|> Enum.reduce({value, []}, fn validator, {val, errors} ->
case validator.(val) do
Expand All @@ -140,4 +149,26 @@ defmodule ChoreRunner.Chore do
{_invalid, errors} -> {:error, name, errors}
end
end

defp do_validate_input(value, {type, name, args, opts}) do
[(&Input.validate_field(type, &1, args)) | Keyword.get(opts, :validators, [])]
|> Enum.reduce({value, []}, fn validator, {val, errors} ->
case validator.(val) do
{:ok, validated_value} -> {validated_value, errors}
:ok -> {val, errors}
true -> {val, errors}
{:error, reason} -> {val, [reason | errors]}
false -> {val, ["invalid" | errors]}
nil -> {val, ["invalid" | errors]}
other -> {other, errors}
end
end)
|> case do
{final_value, [] = _no_errors} ->
{:ok, final_value}

{_invalid, errors} ->
{:error, name, errors}
end
end
end
1 change: 1 addition & 0 deletions lib/chore_runner/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule ChoreRunner.DSL do
float: 2,
file: 2,
bool: 2,
select: 2,
select: 3,
string: 1,
int: 1,
Expand Down
16 changes: 15 additions & 1 deletion lib/chore_runner/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ defmodule ChoreRunner.Input do
do_validate(type, do_cast(value, type))
end

def validate_field(type, value, args) when valid_type(type) do
do_validate(type, do_cast(value, type), args)
end

defp do_cast(value, :string), do: to_string(value)
defp do_cast(value, :select), do: value

Expand Down Expand Up @@ -68,12 +72,22 @@ defmodule ChoreRunner.Input do
defp do_validate(:int, value) when is_integer(value), do: {:ok, value}
defp do_validate(:float, value) when is_float(value), do: {:ok, value}
defp do_validate(:bool, value) when is_boolean(value), do: {:ok, value}
defp do_validate(:select, value), do: {:ok, value}

defp do_validate(:file, %module{} = value) when module == Plug.Upload, do: {:ok, value}

defp do_validate(:file, path) when is_binary(path) do
if File.exists?(path), do: {:ok, path}, else: {:error, :does_not_exist}
end

defp do_validate(_, _), do: {:error, :invalid}

defp do_validate(:select, value, args) do
if Enum.any?(args, fn {_key, select_value} -> value == select_value end) do
{:ok, value}
else
{:error, :not_in_options}
end
end

defp do_validate(_, _, _), do: {:error, :invalid}
end
2 changes: 1 addition & 1 deletion lib/chore_runner_ui/chore_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ defmodule ChoreRunnerUI.ChoreLive do

defp enable_file_inputs(%{assigns: %{inputs: inputs}} = socket) do
inputs
|> Enum.filter(fn {type, _key, _opts} -> type == :file end)
|> Enum.filter(fn input -> input |> Tuple.to_list() |> Enum.at(0) == :file end)
|> Enum.reduce(socket, fn {:file, key, _opts}, socket ->
allow_upload(socket, key, accept: :any, max_entries: 1)
end)
Expand Down
2 changes: 1 addition & 1 deletion lib/chore_runner_ui/templates/chore/chore_inputs.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<% :select -> %>
<div class="chore-form-input-wrapper chore-form-top-level-input-wrapper">
<%= label @form, @key, class: "chore-form-label", title: Keyword.get(@opts, :description, "") %>
<%= select @form, @key, Keyword.get(@opts, :options, []), class: "chore-form-input chore-form-select", phx_update: "ignore" %>
<%= select @form, @key, @args, class: "chore-form-input chore-form-select", phx_update: "ignore" %>
<%= render(__MODULE__, "chore_input_errors.html", chore_errors: @chore_errors, key: @key) %>
</div>
<% :file -> %>
Expand Down

0 comments on commit 0238317

Please sign in to comment.