Skip to content

Commit

Permalink
refactor: extract blocks to own methods
Browse files Browse the repository at this point in the history
  • Loading branch information
heywhy committed Feb 1, 2025
1 parent 7f5ac11 commit 2aea6ab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 72 deletions.
116 changes: 52 additions & 64 deletions lib/validator/rules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -475,39 +475,29 @@ defmodule Request.Validator.Rules do

def min(bound) when is_number(bound) do
# TODO: check for `Plug.Upload` size.
validator_fn = fn
bound, attr, value when is_number(value) ->
message =
gettext("The %{attribute} field must be at least %{min}.",
attribute: attr,
min: bound
)

check(value >= bound, message)

bound, attr, value when is_binary(value) ->
message =
gettext("The %{attribute} field must be at least %{min} characters.",
attribute: attr,
min: bound
)

value
|> String.length()
|> Kernel.>=(bound)
|> check(message)

bound, attr, value when is_list(value) ->
message =
gettext("The %{attribute} field must be at least %{min} items.",
attribute: attr,
min: bound
)
validator_fn = fn bound, attr, value ->
message =
case get_type(value) do
:numeric ->
gettext("The %{attribute} field must be at least %{min}.",
attribute: attr,
min: bound
)

:string ->
gettext("The %{attribute} field must be at least %{min} characters.",
attribute: attr,
min: bound
)

:list ->
gettext("The %{attribute} field must be at least %{min} items.",
attribute: attr,
min: bound
)
end

value
|> Enum.count()
|> Kernel.>=(bound)
|> check(message)
check(get_size(value) >= bound, message)
end

&validator_fn.(bound, &1, &2)
Expand Down Expand Up @@ -539,39 +529,29 @@ defmodule Request.Validator.Rules do

def max(bound) when is_number(bound) do
# TODO: check for `Plug.Upload` size.
validator_fn = fn
bound, attr, value when is_number(value) ->
message =
gettext("The %{attribute} field must not be greater than %{max}.",
attribute: attr,
max: bound
)

check(value <= bound, message)

bound, attr, value when is_binary(value) ->
message =
gettext("The %{attribute} field must not be greater than %{max} characters.",
attribute: attr,
max: bound
)

value
|> String.length()
|> Kernel.<=(bound)
|> check(message)

bound, attr, value when is_list(value) ->
message =
gettext("The %{attribute} field must not be greater than %{max} items.",
attribute: attr,
max: bound
)
validator_fn = fn bound, attr, value ->
message =
case get_type(value) do
:numeric ->
gettext("The %{attribute} field must not be greater than %{max}.",
attribute: attr,
max: bound
)

:list ->
gettext("The %{attribute} field must not be greater than %{max} items.",
attribute: attr,
max: bound
)

:string ->
gettext("The %{attribute} field must not be greater than %{max} characters.",
attribute: attr,
max: bound
)
end

value
|> Enum.count()
|> Kernel.<=(bound)
|> check(message)
check(get_size(value) <= bound, message)
end

&validator_fn.(bound, &1, &2)
Expand All @@ -583,4 +563,12 @@ defmodule Request.Validator.Rules do
false -> {:error, message}
end
end

defp get_size(num) when is_number(num), do: num
defp get_size(value) when is_binary(value), do: String.length(value)
defp get_size(list) when is_list(list), do: Enum.count(list)

defp get_type(num) when is_number(num), do: :numeric
defp get_type(value) when is_binary(value), do: :string
defp get_type(list) when is_list(list), do: :list
end
12 changes: 6 additions & 6 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,32 @@ msgstr ""
msgid "The selected %{attribute} is invalid."
msgstr ""

#: lib/validator/rules.ex:490
#: lib/validator/rules.ex:488
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must be at least %{min} characters."
msgstr ""

#: lib/validator/rules.ex:502
#: lib/validator/rules.ex:494
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must be at least %{min} items."
msgstr ""

#: lib/validator/rules.ex:481
#: lib/validator/rules.ex:482
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must be at least %{min}."
msgstr ""

#: lib/validator/rules.ex:545
#: lib/validator/rules.ex:536
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must not be greater than %{max}."
msgstr ""

#: lib/validator/rules.ex:554
#: lib/validator/rules.ex:548
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must not be greater than %{max} characters."
msgstr ""

#: lib/validator/rules.ex:566
#: lib/validator/rules.ex:542
#, elixir-autogen, elixir-format
msgid "The %{attribute} field must not be greater than %{max} items."
msgstr ""
5 changes: 3 additions & 2 deletions test/support/register_request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ defmodule RequestValidatorTest.RegisterRequest do

@impl Request.Validator
def rules(_) do
# TODO: update rules
%{
"email" => ~V[required|email],
"name" => ~V[required|string],
"password" => ~V[required|string|confirmed],
"gender" => ~V[required|allowed:male,female],
# "age" => ~V[required|numeric|min:2|max:32],
# "year" => ~V[required|numeric|min:1990|max:2000],
"age" => ~V[required|numeric|min:2|max:32],
"year" => ~V[required|numeric|min:1990|max:2000],
# "mother_age" => ~V[required|numeric|gt:age],
# "address" => ~V[required|map],
"address.line1" => ~V[required|string],
Expand Down

0 comments on commit 2aea6ab

Please sign in to comment.