Skip to content

Commit 2aea6ab

Browse files
committed
refactor: extract blocks to own methods
1 parent 7f5ac11 commit 2aea6ab

File tree

3 files changed

+61
-72
lines changed

3 files changed

+61
-72
lines changed

lib/validator/rules.ex

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -475,39 +475,29 @@ defmodule Request.Validator.Rules do
475475

476476
def min(bound) when is_number(bound) do
477477
# TODO: check for `Plug.Upload` size.
478-
validator_fn = fn
479-
bound, attr, value when is_number(value) ->
480-
message =
481-
gettext("The %{attribute} field must be at least %{min}.",
482-
attribute: attr,
483-
min: bound
484-
)
485-
486-
check(value >= bound, message)
487-
488-
bound, attr, value when is_binary(value) ->
489-
message =
490-
gettext("The %{attribute} field must be at least %{min} characters.",
491-
attribute: attr,
492-
min: bound
493-
)
494-
495-
value
496-
|> String.length()
497-
|> Kernel.>=(bound)
498-
|> check(message)
499-
500-
bound, attr, value when is_list(value) ->
501-
message =
502-
gettext("The %{attribute} field must be at least %{min} items.",
503-
attribute: attr,
504-
min: bound
505-
)
478+
validator_fn = fn bound, attr, value ->
479+
message =
480+
case get_type(value) do
481+
:numeric ->
482+
gettext("The %{attribute} field must be at least %{min}.",
483+
attribute: attr,
484+
min: bound
485+
)
486+
487+
:string ->
488+
gettext("The %{attribute} field must be at least %{min} characters.",
489+
attribute: attr,
490+
min: bound
491+
)
492+
493+
:list ->
494+
gettext("The %{attribute} field must be at least %{min} items.",
495+
attribute: attr,
496+
min: bound
497+
)
498+
end
506499

507-
value
508-
|> Enum.count()
509-
|> Kernel.>=(bound)
510-
|> check(message)
500+
check(get_size(value) >= bound, message)
511501
end
512502

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

540530
def max(bound) when is_number(bound) do
541531
# TODO: check for `Plug.Upload` size.
542-
validator_fn = fn
543-
bound, attr, value when is_number(value) ->
544-
message =
545-
gettext("The %{attribute} field must not be greater than %{max}.",
546-
attribute: attr,
547-
max: bound
548-
)
549-
550-
check(value <= bound, message)
551-
552-
bound, attr, value when is_binary(value) ->
553-
message =
554-
gettext("The %{attribute} field must not be greater than %{max} characters.",
555-
attribute: attr,
556-
max: bound
557-
)
558-
559-
value
560-
|> String.length()
561-
|> Kernel.<=(bound)
562-
|> check(message)
563-
564-
bound, attr, value when is_list(value) ->
565-
message =
566-
gettext("The %{attribute} field must not be greater than %{max} items.",
567-
attribute: attr,
568-
max: bound
569-
)
532+
validator_fn = fn bound, attr, value ->
533+
message =
534+
case get_type(value) do
535+
:numeric ->
536+
gettext("The %{attribute} field must not be greater than %{max}.",
537+
attribute: attr,
538+
max: bound
539+
)
540+
541+
:list ->
542+
gettext("The %{attribute} field must not be greater than %{max} items.",
543+
attribute: attr,
544+
max: bound
545+
)
546+
547+
:string ->
548+
gettext("The %{attribute} field must not be greater than %{max} characters.",
549+
attribute: attr,
550+
max: bound
551+
)
552+
end
570553

571-
value
572-
|> Enum.count()
573-
|> Kernel.<=(bound)
574-
|> check(message)
554+
check(get_size(value) <= bound, message)
575555
end
576556

577557
&validator_fn.(bound, &1, &2)
@@ -583,4 +563,12 @@ defmodule Request.Validator.Rules do
583563
false -> {:error, message}
584564
end
585565
end
566+
567+
defp get_size(num) when is_number(num), do: num
568+
defp get_size(value) when is_binary(value), do: String.length(value)
569+
defp get_size(list) when is_list(list), do: Enum.count(list)
570+
571+
defp get_type(num) when is_number(num), do: :numeric
572+
defp get_type(value) when is_binary(value), do: :string
573+
defp get_type(list) when is_list(list), do: :list
586574
end

priv/gettext/default.pot

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,32 @@ msgstr ""
7171
msgid "The selected %{attribute} is invalid."
7272
msgstr ""
7373

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

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

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

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

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

99-
#: lib/validator/rules.ex:566
99+
#: lib/validator/rules.ex:542
100100
#, elixir-autogen, elixir-format
101101
msgid "The %{attribute} field must not be greater than %{max} items."
102102
msgstr ""

test/support/register_request.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ defmodule RequestValidatorTest.RegisterRequest do
55

66
@impl Request.Validator
77
def rules(_) do
8+
# TODO: update rules
89
%{
910
"email" => ~V[required|email],
1011
"name" => ~V[required|string],
1112
"password" => ~V[required|string|confirmed],
1213
"gender" => ~V[required|allowed:male,female],
13-
# "age" => ~V[required|numeric|min:2|max:32],
14-
# "year" => ~V[required|numeric|min:1990|max:2000],
14+
"age" => ~V[required|numeric|min:2|max:32],
15+
"year" => ~V[required|numeric|min:1990|max:2000],
1516
# "mother_age" => ~V[required|numeric|gt:age],
1617
# "address" => ~V[required|map],
1718
"address.line1" => ~V[required|string],

0 commit comments

Comments
 (0)